Clemson University -- CPSC 231 -- Fall 2009 Here is a diagram of a simple computer system: (this diagram will be the one needed for exams) +-----+ | CPU | +-----+ |cache| +-----+ | +===================================================+ bus | | | | +------+ +----------+ +----------+ +----------+ | | |controller| |controller| |controller| | | +----------+ +----------+ +----------+ |memory| | | | | | +--------+ +-------+ +----+ | | |keyboard| |display| |disk| +------+ +--------+ +-------+ +----+ Computer components input - keyboard, mouse, scanner, ... output - display, printer, sound, ... CPU == central processing unit == processor composed of two parts: datapath (temporary memory, called registers, and function units) control logic (sequencing of datapath actions) different instruction sets: Intel IA32 (x86), Apple/IBM/Motorola PowerPC, Sun SPARC, ... common instructions include add, subtract, jump, ... memory multilevel hierarchy due to cost vs. speed tradeoffs fastest and most expensive --> CPU registers cache (perhaps multiple levels) main memory slowest and least expensive -> long-term storage cache and main memory are made of RAM - random access memory DRAM - dynamic RAM - most main memories SRAM - static RAM - fast and expensive, used for caches ROM - read-only memory (holds initial "bootstrap" loader program and basic I/O programs) long-term storage - so slow that it is treated as I/O floppy disk hard disk CD-ROM DVD Prefixes for speed, time, and capacity K (kilo-) = one thousand = 10**3 ~ 2**10 m (milli-) = 10**(-3) M (mega-) = one million = 10**6 ~ 2**20 u (micro-) = 10**(-6) G (giga-) = one billion = 10**9 ~ 2**30 n (nano-) = 10**(-9) T (tera-) = one trillion = 10**12 ~ 2**40 p (pico-) = 10**(-12) P (peta-) = one quadrillon = 10**15 ~ 2**50 f (femto-) = 10**(-15) E (exa-) = one quintillion = 10**18 ~ 2**60 a (atto-) = 10**(-18) main memory size is measured in powers of two, while speed is in powers of 10 (the capacity of most hard disks are measured in powers of ten) note that some folks are now using special binary prefixes to prevent any confusion (http://physics.nist.gov/cuu/Units/binary.html) Ki (kibi-) = kilobinary = 2**10 Mi (mebi-) = megabinary = 2**20 Gi (gibi-) = gigabinary = 2**30 Ti (tebi-) = terabinary = 2**40 Pi (pebi-) = petabinary = 2**50 Ei (exbi-) = exabinary = 2**60 a current consumer PC uses multiple buses (this diagram is *not* required for exams) +------------+ | DRAM | | memory | processor +------------+ e.g., Pentium 4 || || +-----------+ +-----------------+ +------------+ | graphics | +-------+ |+--------+ +----+| FSB | |=======|accelerator|===|monitor| ||backside|=|CPU ||=========|North-Bridge| AGP |(with local| +-------+ ||L2 cache|=|core|| 800 MHz | controller | | memory) | |+--------+ +----+| | | +-----------+ +-----------------+ |(memory hub)| | |====== 1 GBit Ethernet +------------+ || +------------+ serial ATA | | South-Bridge has dedicated ports disk ================| | for legacy devices likeQ mouse |South-Bridge| keyboard, floppy; also has the parallel ATA | controller | serial and parallel ports CD/DVD ===============| | | (I/O hub) | South-Bridge typically contains 10 MBit Ethernet =====| | flash-eprom holding the BIOS, | | real-time clock, CMOS memory PCI bus ==============| | (w/ independent battery backup) +------------+ Program translation symbolic (i.e., human readable) languages high-level language (HLL) assembly language - one-to-one (approx.) correspondence with machine insts. machine instructions - represented inside the computer in bit (0/1) patterns HLL assembly lang machine code (object file or executable) --- ------------- ---------------------------------------- A = B + C; --> load(B) --> 0000 0010 0010 0100 add(C) 0000 0001 0010 0101 store(A) 0000 0011 0010 0011 translators from one language level to a lower level compiler - HLL to assembly assembler - assembly to object file linker - bind multiple object files into one executable file each moves closer to the bit representation needed by hardware for execution a translator that translates and executes all at a single time is called an interpreter Assembly a statement in assembly language is called an instruction an instruction is composed of - operation code (opcode) - operands (names of registers and/or information needed to generate a memory address) example: SPARC assembler SPARC symbolic code -------> address machine code (in hexadecimal) ------------------- ------- ------------ main: save %sp,-104,%sp 0x10a50 0x9de3bf98 mov 9,%l0 0x10a54 0xa0102009 add %l0,-1,%l1 0x10a58 0xa2043fff add %l0,-7,%o1 0x10a5c 0x92043ff9 mov %l1,%o0 0x10a60 0x90100011 call .umul,0 0x10a64 0x400042fd nop 0x10a68 0x01000000 add %l0,-11,%o1 0x10a6c 0x92043ff5 call .div,0 0x10a70 0x400042fd nop 0x10a74 0x01000000 mov %o0,%l1 0x10a78 0xa2100008 ret 0x10a7c 0x81c7e008 restore 0x10a80 0x81e80000 labels (like "main" above) represent symbolic addresses of data and jump targets in the program each label must be unique (i.e., it must be defined only once) assembly symbolic program (human readable) ----------> machine code (binary) symbolic labels -> memory addresses opcodes -> bit patterns in instructions operand identification (registers -> bit patterns in instructions and memory address info) immediate operand values (constants) -> bit patterns in instructions Assemblers have a two-pass structure instructions can have forward or backward references to labels note that a forward reference requires a two-pass assembly structure since you encounter a "use" before its "definition" and thus cannot immediately translate the label into its memory address thus: pass 1 - increment a location counter as you read each assembly language statement and collect any label definitions into a symbol table with the corresponding location counter values pass 2 - translate the assembly language statements using the symbol table example: the instruction "jmp next" - forward reference to jump target label "next" the instruction "add x" - forward reference to data label "x" pass 1 pass 2 ----------> ----------> symbolic code assign addresses translated code using a location (will be represented counter in binary) ... 100: ... ... jmp next 101: jmp next ... 102: ... ... next: add x 103: add x ... 104: ... ... halt 105: halt x: .word 15 106: 15 | _symbol_table_ A | | | label addr | V +------+-----+ | new entry | next | 103 | table lookup when you +------+-----+ yielding address encounter | x | 106 | when you encounter a label +------+-----+ a symbolic label | ... | ... | +------+-----+ (note that the symbol table for the assembler holds the address 106 of "x", not its initial value of 15) alternatively, if you keep all the translated code in memory, you can translate in one pass over the input -- but you must keep a record of all unresolved uses of a label (e.g., the symbol table entry for an as-yet-undefined label points to a linked list of all forward references) and then you backtrack and fixup those uses whenever the definition is encountered