6 S 078 Computer Architecture A Constructive Approach



![Multiple Levels of Representation • • High Level Language Program temp = v[k]; • Multiple Levels of Representation • • High Level Language Program temp = v[k]; •](https://slidetodoc.com/presentation_image_h2/71d0518a2cfa5ec677f57c949da36381/image-4.jpg)









![Example: Compiling using a Variable Array Index C code: g = h + A[i] Example: Compiling using a Variable Array Index C code: g = h + A[i]](https://slidetodoc.com/presentation_image_h2/71d0518a2cfa5ec677f57c949da36381/image-14.jpg)





![Jumps JR rs n Jumps to address in register PC <- Reg[rs] JR vs. Jumps JR rs n Jumps to address in register PC <- Reg[rs] JR vs.](https://slidetodoc.com/presentation_image_h2/71d0518a2cfa5ec677f57c949da36381/image-20.jpg)
















- Slides: 36
6. S 078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -1
What I do: Many-core chip architectures Core Area 900 x 1800 um 2 Rtr NIC 264 x 264 um 2 120 x 120 um 2 • Router/ NIC and other Logic • 1 GHz 16 -node No. C in 45 nm Aggressively pipelined into a single cycle • 0. 8, 1. 1 V datapath for low power February 22, 2012 16 Freescale Power. PC processor cores (2 -issue in-order), 2 level caches, cache-coherent shared memory within the No. C http: //csg. csail. mit. edu/6. S 078 L 5 -2
Stored Program Concept Instructions are bits (i. e. , as numbers) Programs are stored in memory n to be read or written just like data • Treating Instructions in the same way as Data • memory for data, programs, • compilers, editors, etc. Fetch & Execute Cycle n n n Instructions are fetched and put into a special register Bits in the register "control" the subsequent actions Fetch the next instruction and continue
Multiple Levels of Representation • • High Level Language Program temp = v[k]; • v[k] = v[k+1]; • v[k+1] = temp; • Compiler • lw lw sw$16, sw$15, Assembly Language Program • Assembler • Machine Language Program • 0000 • 1010 • 1100 • 0101 1001 1111 0110 1000 1100 0101 1010 0000 $15, 0($2) $16, 4($2) 0($2) 4($2) 0110 1000 1111 1001 1010 0000 0101 1100 1111 1000 0110 0101 1100 0000 1010 1000 0110 1001 1111 • Machine Interpretation • Control Signal Specification • ALUOP[0: 3] <= Inst. Reg[9: 11] & MASK • High and low signals on control lines
Instruction Set Architecture (ISA) Programmer’s view of the computer n February 22, 2012 Instructions, operands http: //csg. csail. mit. edu/6. S 078 L 5 -5
Example ISAs Intel 80 x 86 ARM IBM/Motorola Power. PC HP PA-RISC Oracle/Sun Sparc 6. 004’s Beta
Why MIPS? It’s simple! Most taught ISA
MIPS I Instruction Set Architecture Instruction Categories n n n • Registers • r 0 - r 31 Load/Store Computational Jump and Branch Floating Point w coprocessor Memory Management Special • PC • HI • LO • 3 Instruction Formats: all 32 bits wide • OP • rs • rt • OP • rd • sa • funct • immediate • jump target • SMIPS: a subset of the full MIPS 32 ISA
SMIPS Registers: Fast Locations for Data 32 32 -bit registers: $0, $1, … , $31 n n operands for integer arithmetic address calculations temporary locations special-purpose functions defined by convention 1 32 -bit Program Counter (PC) 2 32 -bit registers HI & LO: n used for multiply & divide
MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: • C code: A=B+C • MIPS code: add $s 0, $s 1, $s 2 • C code: A = B + C + D; E = F - A; • MIPS code: add $t 0, $s 1, $s 2 add $s 0, $t 0, $s 3 sub $s 4, $s 5, $s 0
MIPS Load-Store Architecture Every operand must be in a register (a few exceptions) Variables have to be loaded in registers. Results have to be stored in memory. • a = b + c • d = a + b • load b in register Rx • load c in register Ry • Rz = Rx + Ry • store Rz in a • Rt = Rz + Rx • store Rt in d • more variables than registers, so need explicit load and stores.
Memory Organization Viewed as a large, single-dimension array, with an address. A memory address is an index into the array "Byte addressing" means that the index points to a byte of memory. • 0 • 8 bits of data • 1 • 8 bits of data • 2 • 8 bits of data • 3 • 8 bits of data • 4 • 8 bits of data • 5 • 8 bits of data • 6 • 8 bits of data • . . . • Q: How to specify a memory location?
Load & Store Instructions Base+Offset addressing mode: offset(base register) e. g. , 32($s 3) Example: n C code: A[8] = h + A[8]; w A: an array of 100 words w the base address of the array A is in $s 3 n • base register: $s 3 • offset: 32 MIPS code: lw $t 0, 32($s 3) add $t 0, $s 2, $t 0 sw $t 0, 32($s 3) Store word has destination last
Example: Compiling using a Variable Array Index C code: g = h + A[i] $s 3: base register for A g, h, i: $s 1, $s 2, $s 4 MIPS code: add $t 1, $s 4 add $t 1, $t 1 # $t 1 = 2 * i # $t 1 = 4 * i add $t 1, $s 3 lw $t 0, 0($t 1) # $t 1 = address of A[i] # $t 0 = A[i] add $s 1, $s 2, $t 0 # g = h + A[i]
LUI instruction Load upper immediate LUI rt, zero-ext-imm Shifts 16 -bit immediate into highorder 16 bits, with 16 zeros in low order bits -> rt How is LUI useful? February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -15
Control: bne & beq Decision making instructions n n alter the control flow, i. e. , change the "next" instruction to be executed MIPS conditional branch instructions: bne $t 0, $t 1, Label beq $t 0, $t 1, Label Example: if (i==j) h = i + j; bne $s 0, $s 1, Label add $s 3, $s 0, $s 1 Label: . .
SLT instructions • Set if less than • E. g. SLTI rt, rs, signed-imm If (rs < imm), rt=1, else rt=0 What is SLT used for?
Jumps (J, JAL, JR, JALR) MIPS unconditional branch instructions: j label • Example: if (i!=j) h=i+j; else h=i-j; beq $s 4, $s 5, Lab 1 add $s 3, $s 4, $s 5 j Lab 2 Lab 1: sub $s 3, $s 4, $s 5 Lab 2: . . . • PC <- PC + 4*SEXT(literal)
Jumps JAL target (jump and link) n n n February 22, 2012 PC+8 -> R 31 Why PC+8? How is JAL useful? http: //csg. csail. mit. edu/6. S 078 L 5 -19
Jumps JR rs n Jumps to address in register PC <- Reg[rs] JR vs. J or JAL? February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -20
Jumps JALR – Jump and link register JALR rd, rs n n Jumps to rs Writes link address into rd Why JALR vs. JAL? February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -21
MIPS: Stack detective! Call procedure: jump and link (jal) Return from procedure: jump register (jr) Argument values: $a 0 - $a 3 Return value: $v 0 Template: n n February 22, 2012 Call setup Prologue Epilogue Return cleanup http: //csg. csail. mit. edu/6. S 078 L 5 -22
February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -23
February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -24
Procedure call setup 1. Place current parameters into stack (space already allocated by caller of this procedure) 2. Save any TEMPORARY registers that need to be preserved across the procedure call 3. Place first 4 parameters to procedure into $a 0 -$a 3 4. Place remainder of parameters to procedure into allocated space within the stack frame February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -25
Procedure call setup February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -26
Prologue 1. allocate space for stack frame 2. save return address in stack frame 3. copy needed parameters from stack frame into registers 4. save any needed SAVED registers into current stack frame February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -27
Time to actually call function! February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -28
Return cleanup 1. copy needed return values and parameters from $v 0 -v 1, $a 0 -a 3, or stack frame to correct places 2. restore any temporary registers from stack frame (saved in call setup) February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -29
Epilogue 1. restore (copy) return address from stack frame into $ra 2. restore from stack frame any saved registers (saved in prologue) 3. de-allocate stack frame (move $sp so the space for the procedure's frame is gone) February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -30
MIPS coprocessor 0 instructions: mfc 0, mtc 0 interrupts, exceptions, resets Beta vs MIPS February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -31
Exception Registers Not part of the register file. n Cause w Records the cause of the exception n EPC (Exception PC) w Records the PC where the exception occurred EPC and Cause: part of Coprocessor 0 Move from Coprocessor 0 n n mfc 0 $t 0, EPC Moves the contents of EPC into $t 0
Exceptions • Save cause and exception PC • Jump to exception handler (0 x 0000_1100) • Exception handler: – – • – – – Saves registers on stack Reads the Cause register mfc 0 Cause, $t 0 Handles the exception Restores registers Returns to program • • mfc 0 EPC, $k 0 jr $k 0
Exception Causes February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -34
Cause register February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -35
Reset mtc 0 zero, $9 #init counter mtc 0 zero, $11 #timer interrupt : : J kernel_init February 22, 2012 http: //csg. csail. mit. edu/6. S 078 L 5 -36