CS212, Summer 2003 Lecture 1: Computer Architecture ------------------------------------------------------------------------------- 0) Announcements: Overview: + How a computer works + how a program executes your code + stack machines + Part 1 overview ------------------------------------------------------------------------------- 1) History of computing A computer is.....? + "a programming electronic device that can store, retrieve, and process data" + See chapter 1 from pretty much any programming book (abacus, looms, mechanical calculators,...) Information as signals: + ANALOG: device stores any value from a continuous range + DIGITAL: device stores limited number of digits + why digital over analog? - signals: as signal moves down a wire, it gets weaker analog signals lose information digital easier to decode because values are closer to "extremes" - so, digital less sensitive to errors than analog + digit computers use of BINARY digits - BINARY: base-2 number system with two digits (0,1) - why? "less expensive and more reliable" (think of easy of construction, portability, standards, and info loss) + information converted to BITS: binary digit (0,1) references: See Chapter 1 of Lewis&Loftus from CS100 ------------------------------------------------------------------------------- 2) Components of Circuits and Memory + BOOLEAN operations: operations that manipulate true/false values) - need to operate on bits - use AND, OR, XOR, NOT - think of bits as false (0) and true (1) ex) AND(0,0) -> 0 OR(1,0) -> 1 XOR(1,1) -> 1 How to store bits? + GATE: device that produces the output of a Boolean operation - computers implement gates as small electronic circuits in which bits are represented as voltage values - AND, OR, XOR, NOT have gates - see http://fitness.howstuffworks.com/boolean1.htm for pictures - gates provide "building-blocks" to create computers - make circuits by connecting gates together + FLIP-FLOPS: circuit that produces a bit that remains constant until a temporary pulse from another circuit causes a new bit to be produced - Since flip-flops can hold a steady value, we can use them to store bits + MAIN MEMORY - integrated circuits on a chip. (millions of capacitors and circuits can be placed on a chip) - see http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?integrated+circuit for more information + other storage techniques to store bits - capacitor, which can be charged or uncharged to represent 0 or 1 ------------------------------------------------------------------------------- 3) Memory + A computer contains a large collection of circuits that can store data as bits + MAIN MEMORY: high-speed storage area of bits - mostly stores programs and data - others? VRAM (video memory for screen display), CACHE (mostly recently read and frequently read data), and more + main memory spit into CELLS (or WORDS) that usually store 1 byte (1 BYTE = 8 bits) + each memory cell has an ADDRESS that stores one byte - start with 0 and work up to storage capacity (see below) + STORAGE CAPACITY: how many bytes a device (like main memory) can hold - KB: 2^10 bytes (1024) - MB: 2^20 bytes (1048576) - and more.... + RAM: random access memory (also think of as "read-write" memory) - the organization of main memory into cells with addresses allows for individual access - access of any cell takes a constant time (as opposed to a tape, which stores data in sequence (called Sequential Access Memory, or SAM) + main memory is VOLATILE: - running programs and using data means putting the info into the cells - when power "shuts off" the infomation is lost + mass/Secondary Storage: - "permanent" or non-volatile storage - magnetic disks, CDs, magnetic tape + ROM: read only memory - permanently stored programs and data - cannot be changed - FIRMWARE: programs stored in ROM ("something between hardware and software") See Computer Science: An Overview, J.G. Brookshear,Addison-Wesley, 2002. ------------------------------------------------------------------------------- 4) Computer Architecture + main components: - CPU: central processing unit - storage: main memory, secondary memory - external devices: input, output + input: keyboard, mouse + output: printers, monitors + main memory (internal memory) (summary from above) - stores current program instructions - holds data to be processed by program - holds intermediate results from executing program - continuous cells store information as bytes and have location + external storage: magnetic disks, CDs, tape CPU + interacts with main memory - CPU performs operations - main memory stores the information + 3 basic components - ALU: Arithmetic/Logic Unit, which performs mathematical calculations and boolean operations - CONTROL UNIT: coodinates the processing steps - REGISTERS: provide small amount of storage space for things like counters + BUS: collection of conductors, like wires, that connects the components and carries data between them + microprocessor: a CPU constructed on a chip ------------------------------------------------------------------------------- 5) Fetch-Decode-Execute Cycle: + also called the MACHINE CYCLE + "von Neumann" architecture: - Main idea :store both program instructions and data in main memory. This was a MAJOR advance in the construction of computers. The 1st von Neumann computer was the EDVAC, first conceived by John von Neumann in 1944. - follows the "Fetch-Decode-Execute Cycle" - Before the EDVAC came the ENIAC, which was programmed by plugging cables and setting switches. This was not a "von Neumann" computer. + the cycle: - the Control Unit (CU) requests that the main memory provide the next instruction: so, the next instruction is fetched from main memory at the address in the program counter (PC) - the CU places the instruction into the instruction register - the CU increments the PC to prepare for the next cycle - the CU fetches any data needed for the operation from memory - the CU decodes the instruction to see what to do - the CU activates the correct circuitry to carry out the instruction (such as getting the ALU to perform an operation) - loop back to the beginning of the cycle + to coordinate the activities, an internal clock oscillates back and forth - speed of the clock gives the speed for the CPU to perform the FDE cycle ------------------------------------------------------------------------------- 6) Machine Instructions: + the instructions that CPU follows (see machine cycle) + 3 catagories: - data transfer instructions - arithmetic/logic instructions - control instructions + data transfer: - copy/move data from one location in memory to another - LOAD: fill a general-purpose register with contents of a memory cell (cell contents go to register) - STORE: transfer contents of register *to* a memory cell (register contents go to cell) - I/O instructions + arithmetic/logic: - tell the CU to request an operation in the ALU - arithmetic (ADD, SUB, ....) - logic (AND, OR, XOR, NOT) - SHIFT, ROTATE (move contents of registers within the registers) + control: - direct the execution of the program - JUMP/BRANCH as in selection statements ------------------------------------------------------------------------------- 7) Stored-Program Concept: + rather than rewiring a control unit, store a program in main memory, as with data + certain instructions get assigned to certain bit patterns (in other words, each instruction is represented by a different number) + MACHINE LANGUAGE: the instructions and coding system that the computer will act upon (not usually something in which humans write programs) - has a number of fields: op-code field (operation code) and operand fields - each field has a pattern of bits - the op-code has the operation, like STORE, ADD, JUMP, etc - each operand field has info associated with the operation (like an address or data to manipulate) - the complete set of instructions for a computer is the INSTRUCTION SET + ASSEMBLY LANGUAGE - Programming by binary numbers is so tedious (and inefficient!) - Symbolic notation for binary machine instructions. So, to add two numbers, a programmer would write "add A, B" instead of 10001110010 - An "assembler" converts assembly language into machine code - Only one instruction per line, however... still inefficient. That's why we have "high-level programming languages", which are easier for a human to read and write, and which are more concise. ------------------------------------------------------------------------------- 8) Stack Machine Model + think of main memory as a stack of cells that can hold information as bits + instructions and data can be translated as bit patterns + Stack: - data structure for storing items which are to be accessed in last-in first-out order - Instructions: - PUSH: place an item onto the top of the stack - POP: remove an item from the top of the stack - instruction set architectures typically use stacks to store arguments and return addresses + think of Java - Java code gets compiled into class files which contain sequences of bits, not usually readable characters ("binary files") - a byte-code interpreter runs each instruction of byte-code in a similar fashion as machine code - advantage? as long as an interpreter is built for "all" instruction sets, the same byte-code should be able to run on any computer without recompiling ("write once, run everywhere" slogan) - the Java Virtual Machince (JVM) is like the "average" of many computers, a sort-of CPU that takes each byte-code instruction and activates it - the JVM uses a STACK ------------------------------------------------------------------------------- 9) OOP Program +-------------------------------------------+ | | | +--------------+ +--------------+ | | | static area | | program area | | | +--------------+ +--------------+ | | | | +--------------+ +--------------+ | | | stack | | heap | | | +--------------+ +--------------+ | | | +-------------------------------------------+ + static area: static data exists only once in memory. In C, any variable declared outside of a function is static. In C and Java, any variable declared with the "static" keyword is (obviously :) static + program area: + stack: holds data used in functions + heap: holds "objects", which consist of fields of data. The heap and stack share a portion of memory. Typically, the heap grows "up" and the stack grows "down" ------------------------------------------------------------------------------- 10) Compilers + translation: write one programming language in another another - translator: - compiler: - process of translation: + our next focus: - CS100/CS211: source program (.java), object program (.class) - current CS211/CS212 work: code generation for Sam - up next: lexical analysis and parser + LEXICAL ANALYSIS: recognizing which strings of symbols from the source program represent a single entity + PARSING: process of identifying the grammatical structure of program and recognizing roles of each component -------------------------------------------------------------------------------