09.07.2015 Views

CPSC 121: Models of Computation Module 10: A ... - Ugrad.cs.ubc.ca

CPSC 121: Models of Computation Module 10: A ... - Ugrad.cs.ubc.ca

CPSC 121: Models of Computation Module 10: A ... - Ugrad.cs.ubc.ca

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Module</strong> <strong>10</strong>: A Working ComputerHistori<strong>ca</strong>l notes:Early 19th century:Joseph Marie Charles dit Jacquard used punched paper<strong>ca</strong>rds to program looms.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 7<strong>Module</strong> <strong>10</strong>: A Working ComputerHistori<strong>ca</strong>l notes (early 19th century continued):Charles Babbage designed (1837) but could not build thefirst programmable (mechani<strong>ca</strong>l) computer, based onJacquard's idea.http://www.computerhistory.org/babbage/<strong>CPSC</strong> <strong>121</strong> – 2012W T2 8


<strong>Module</strong> <strong>10</strong>: A Working ComputerHistori<strong>ca</strong>l notes (continued):1941: Konrad Zuse builds the first electromechani<strong>ca</strong>lcomputer.It had binary arithmetic, including floating point.It was programmable.1946: the ENIAC was the first programmableelectronic computer.It used decimal arithmetic.Reprogramming meant rewiring.All its programmers were women.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 9<strong>Module</strong> <strong>10</strong>: A Working ComputerHistori<strong>ca</strong>l notes (mid 20 thcentury, continued)The first stored-program electronic computers weredeveloped from 1945 to 1950.Programs and data were stored on punched <strong>ca</strong>rds.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 <strong>10</strong>


<strong>Module</strong> <strong>10</strong>: A Working ComputerA quick roadmap through our courses:<strong>CPSC</strong> <strong>121</strong>: learn about gates, and how we <strong>ca</strong>n usethem to design a circuit that executes very simpleinstructions.<strong>CPSC</strong> 213: learn how the constructs available inlanguages such as Racket, C, C++ or Java areimplemented using these simple instructions.<strong>CPSC</strong> 313: learn how we <strong>ca</strong>n design computers thatexecute programs efficiently and meet the needs <strong>of</strong>modern operating systems.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 11<strong>Module</strong> <strong>10</strong>: A Working Computer<strong>Module</strong> Summary:A little bit <strong>of</strong> historyImplementing a working computer in LogisimAppendices<strong>CPSC</strong> <strong>121</strong> – 2012W T2 12


<strong>Module</strong> <strong>10</strong>: A Working ComputerVon-Neumann architectureMemory (contains both programs and data).Control UnitArithmetic & LogicUnitInput/OutputCPU<strong>CPSC</strong> <strong>121</strong> – 2012W T2 13<strong>Module</strong> <strong>10</strong>: A Working ComputerMemoryContains both instructions and data.Divided into a number <strong>of</strong> memory lo<strong>ca</strong>tionsThink <strong>of</strong> positions in a list: (list-ref mylist pos)Or in an array: myarray[pos] or arrayList arrayl.get(pos).0<strong>10</strong><strong>10</strong>111...0 1 2 3 4 5 6 7 8 9 <strong>10</strong> 11 ...<strong>CPSC</strong> <strong>121</strong> – 2012W T2 14


<strong>Module</strong> <strong>10</strong>: A Working ComputerMemoryEach memory lo<strong>ca</strong>tion contains a fixed number <strong>of</strong>bits.Most commonly this number is 8.Values that use more than 8 bits are stored in multipleconsecutive memory lo<strong>ca</strong>tions.Characters use 8 bits (ASCII) or 16/32 (Unicode).Integers use 32 or 64 bits.Floating point numbers use 32, 64 or 80 bits.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 15<strong>Module</strong> <strong>10</strong>: A Working ComputerArithmetic and Logic UnitPerforms arithmetic and logi<strong>ca</strong>l operations (+, -, *, /,and, or, etc).Control UnitDecides which instructions to execute.Executes these instructions sequentially.Not quite true, but this is how it appears to the user.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 16


<strong>Module</strong> <strong>10</strong>: A Working ComputerOur working computer:Implements the design presented in the textbook byBryant and O'Hallaron (used for <strong>CPSC</strong> 213/313).A small subset <strong>of</strong> the IA32 (Intel 32-bit) architecture.It has12 types <strong>of</strong> instructions.One program counter register (PC)contains the address <strong>of</strong> the next instruction.8 general-purpose 32-bits registersstoresstoresaasinglesinglemulti-bitmulti-bitvalue.value.each <strong>of</strong> them contains one 32 bit value.used for values that we are currently working with.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 17<strong>Module</strong> <strong>10</strong>: A Working ComputerExample instruction 1: subl %eax, %ebxThe subl instruction subtracts its arguments.The names %eax and %ebx refer to two registers.This instruction takes the value contained in %eax,subtracts it from the value contained in %ebx, andstores the result back in %ebx.Example instruction 2: irmovl $0x1A, %ecxThis instruction stores a constant in a register.In this <strong>ca</strong>se, the value 1A (hexadecimal) is stored in%ecx.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 18


<strong>Module</strong> <strong>10</strong>: A Working ComputerExample instruction 3: rmmovl %ecx, $8(%ebx)The rmmovl instruction stores a value into memory(Register to Memory Move).In this <strong>ca</strong>se it takes the value in register %ecx.And stores it in the memory lo<strong>ca</strong>tion whose addressis:The constant 8PLUS the current value <strong>of</strong> register %ebx.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 19<strong>Module</strong> <strong>10</strong>: A Working ComputerExample instruction 4: jge $<strong>10</strong>00This is a conditional jump instruction.It checks to see if the result <strong>of</strong> the last arithmetic orlogic operation was zero or positive (Greater than orEqual to 0).If so, the next instruction is the instruction stored inmemory address <strong>10</strong>00 (hexadecimal).If not, the next instruction is the instruction thatfollows the jge instruction.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 20


<strong>Module</strong> <strong>10</strong>: A Working ComputerHow does the computer know which instructiondoes what?Each instruction is a sequence <strong>of</strong> 16 to 48 bits†Some <strong>of</strong> the bits tell it which instruction it is.Other bits tell it what operands to use.These bits are used as select inputs for severalmultiplexers.Modified slightly from the Y86 presented in the textbook by Bryant and O'Hallaron<strong>CPSC</strong> <strong>121</strong> – 2012W T2 21<strong>Module</strong> <strong>10</strong>: A Working ComputerExample:<strong>CPSC</strong> <strong>121</strong> – 2012W T2 22


<strong>Module</strong> <strong>10</strong>: A Working ComputerExample 1: subl %eax, %ebxRepresented by6<strong>10</strong>3 (hexadecimal)%ebx%eaxsubtractionarithmetic or logic operation (note: the use <strong>of</strong> “6” to representthem instead <strong>of</strong> 0 or F or any other value is completely arbitrary)..<strong>CPSC</strong> <strong>121</strong> – 2012W T2 23<strong>Module</strong> <strong>10</strong>: A Working ComputerExample 2: rmmovl %ecx, $8(%ebx)Represented by401300000008 (hexadecimal)$8%ebx%ecxignoredregister to memory move<strong>CPSC</strong> <strong>121</strong> – 2012W T2 24


<strong>Module</strong> <strong>10</strong>: A Working ComputerHow is an instruction executed?This CPU divides the execution into 6 stages:Fetch: read instruction and decide on new PC value<strong>CPSC</strong> <strong>121</strong> – 2012W T2 25<strong>Module</strong> <strong>10</strong>: A Working ComputerSix stages <strong>of</strong> execution (continued)Decode: read values from registersExecute: use the ALU to perform computationsSome <strong>of</strong> them are obvious from the instruction (e.g. subl)Other instructions use the ALU as well (e.g. rmmovl)Memory: read data from or write data to memoryWrite-back: store value(s) into register(s).PC update: store the new PC value.Not all stages do something for every instruction.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 26


<strong>Module</strong> <strong>10</strong>: A Working ComputerSample program:irmovl $3,%eaxirmovl $35, %ebxirmovl $fa<strong>ca</strong>de, %ecxsubl %eax, %ebxrmmovl %ecx, $8(%ebx)halt<strong>CPSC</strong> <strong>121</strong> – 2012W T2 27<strong>Module</strong> <strong>10</strong>: A Working ComputerExample 1: subl %eax, %ebxFetch: current instruction ← 6<strong>10</strong>3next PC value ← current PC value + 2Decode: valA ← value <strong>of</strong> %eaxvalB ← value <strong>of</strong> %ebxExecute: valE ← valB - valAMemory: nothing needs to be done.Write-back: %ebx ← valEPC update: PC ← next PC value<strong>CPSC</strong> <strong>121</strong> – 2012W T2 28


<strong>Module</strong> <strong>10</strong>: A Working ComputerExample 2: rmmovl %ecx, $8(%ebx)Fetch: current instruction ← 401300000008next PC value ← current PC value + 6Decode: valA ← value <strong>of</strong> %ecxvalB ← value <strong>of</strong> %ebxExecute: valE ← valB + 00000008Memory: M[valE] ← valBWrite-back: nothing needs to be donePC update: PC ← next PC value<strong>CPSC</strong> <strong>121</strong> – 2012W T2 29<strong>Module</strong> <strong>10</strong>: A Working Computer<strong>Module</strong> Summary:A little bit <strong>of</strong> historyImplementing a working computer in LogisimAppendices<strong>CPSC</strong> <strong>121</strong> – 2012W T2 30


<strong>Module</strong> <strong>10</strong>: A Working ComputerRegisters (32 bits each):0123%eax%ecx%edx%ebx%esp%ebp%esi%ediInstructions that only need one register use 8 or Ffor the second register.%esp is used as stack pointer.Memory contains 2 32 bytes; all memory accessesload/store 32 bit words.<strong>CPSC</strong> <strong>121</strong> – 2012W T2 314567<strong>Module</strong> <strong>10</strong>: A Working ComputerInstruction types:register/memory transfers:rmmovl rA, D(rB) M[D + R[rB]] ← R[rA]Example: rmmovl %edx, 20(%esi)mrmovl D(rB), rAR[rA] ← M[D + R[rB]]<strong>CPSC</strong> <strong>121</strong> – 2012W T2 32


<strong>Module</strong> <strong>10</strong>: A Working ComputerInstruction types:Other data transfer instructionsrrmovl rA, rBirmovl V, rBArithmetic instructionsaddl rA, rBsubl rA, rBandl rA, rBxorl rA, rBR[rB] ← R[rA]R[rB] ← VR[rB] ← R[rB] + R[rA]R[rB] ← R[rB] − R[rA]R[rB] ← R[rB] ∧ R[rA]R[rB] ← R[rB] R[rA]<strong>CPSC</strong> <strong>121</strong> – 2012W T2 33<strong>Module</strong> <strong>10</strong>: A Working ComputerInstruction types:Unconditional jumpsjmp DestConditional jumpsPC ← Destjle Dest PC ← Dest if last result ≤ 0jl Dest PC ← Dest if last result < 0je Dest PC ← Dest if last result = 0jne Dest PC ← Dest if last result ≠ 0jge Dest PC ← Dest if last result ≥ 0jg Dest PC ← Dest if last result > 0<strong>CPSC</strong> <strong>121</strong> – 2012W T2 34


<strong>Module</strong> <strong>10</strong>: A Working ComputerInstruction types:Conditional movescmovle rA, rB R[rB] ← R[rA] if last result ≤ 0cmovl rA, rB R[rB] ← R[rA] if last result < 0cmove rA, rB R[rB] ← R[rA] if last result = 0cmovne rA, rB R[rB] ← R[rA] if last result ≠ 0cmovge rA, rB R[rB] ← R[rA] if last result ≥ 0cmovg rA, rB R[rB] ← R[rA] if last result > 0<strong>CPSC</strong> <strong>121</strong> – 2012W T2 35<strong>Module</strong> <strong>10</strong>: A Working ComputerInstruction types:Procedure <strong>ca</strong>lls and return support<strong>ca</strong>ll Dest R[%esp]←R[%esp]-4; M[R[%esp]]←PC; PC←Dest;ret PC←M[R[%esp]]; R[%esp]←R[%esp]+4pushl rA R[%esp]←R[%esp]-4; M[R[%esp]]←R[rA]popl rA R[rA]←M[R[%esp]]; R[%esp]←R[%esp]+4Othershaltnop<strong>CPSC</strong> <strong>121</strong> – 2012W T2 36


<strong>Module</strong> <strong>10</strong>: A Working ComputerInstructions formatnophaltcmovXX rA, rBirmovl V, rBrmmovl rA, D(rB)mrmovl D(rB), rAOPI rA, rBjXX Dest<strong>ca</strong>ll Destretpushl rA<strong>CPSC</strong> <strong>121</strong> – 2012W T2 37popl rA0 1 2 3 4 50 0 0 01 0 0 02 fn rA rB3 0 F rB V4 0 rA rB D5 0 rA rB D6 fn rA rB7 fn 0 0 Dest8 0 0 0 Dest9 0 0 0A 0 rA FB 0 rAF<strong>Module</strong> <strong>10</strong>: A Working ComputerInstructions format:Arithmetic instructions:addl → fn = 0 subl → fn = 1andl → fn = 2 xorl → fn = 3Conditional jumps and moves:jump → fn = 0 jle → fn = 1jl → fn = 2 je → fn = 3jne → fn = 4 jge → fn = 5je → fn = 6<strong>CPSC</strong> <strong>121</strong> – 2012W T2 38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!