ECE 331 Hardware Organization and Design Professor Jay Taneja UMass ECE - jtaneja@umass.edu Discussion 3 2/8/2018
Study Jams Leader: Chris Bartoli Tuesday 5:30-6:45pm Elab 325 Wednesday 8:30-9:45pm Elab 325
Today s Discussion Topics Conditional Operations Branches + Jumps Loops (if, while, etc.) Procedures
Conditional Operations! Branch to a labeled instruction if a condition is true Otherwise, continue sequentially! beq rs, rt, L1 if (rs == rt) branch to instruction labeled L1;! bne rs, rt, L1 if (rs!= rt) branch to instruction labeled L1;! j L1 unconditional jump to instruction labeled L1 ECE331: Logic Operations + Intro to Conditionals
MIPS Conditional Branch Instructions! Conditional branches allow decision making beq R1, R2, LABEL if R1==R2 goto LABEL bne R3, R4, LABEL if R3!=R4 goto LABEL beq $s1, $s2, 25 if ($s1==$s2) PC = PC + 4 + 4*25 else PC = PC + 4 {! Example C Code if (i==j) goto L1; f = g + h; L1: f = f - i; Address of next sequential instruction Offset in bytes! Assembly beq $s3, $s4, L1 add $s0, $s1, $s2 L1: sub $s0, $s0, $s3 ECE331: Logic Operations + Intro to Conditionals
Binary Representation - Branch 6 bits 5 bits 5 bits 16 bits op rs rt offset I-Format! Branch instructions use I-Format! offset is added to PC when branch is taken beq r0, r1, offset has the effect: Conversion to byte offset if (r0==r1) pc = pc + 4 + (offset << 2) else pc = pc + 4; If offset is a positive number! Offset is specified in instruction words ECE331: Logic Operations + Intro to Conditionals
Binary Representation - Jump 6 bits 26 bits op address! Jump Instruction uses J-Format (op=2)! What happens during execution? PC = PC[31:28] : (IR[25:0] << 2) Concatenate upper 4 bits of PC to form complete 32-bit address Conversion to byte offset ECE331: Logic Operations + Intro to Conditionals
Compiling If Statements! C code: if (i==j) f = g+h; else f = g-h; f, g, in $s0, $s1,! Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: Assembler calculates addresses ECE331: Logic Operations + Intro to Conditionals
Compiling Loop Statements! C code: while (save[i] == k) i += 1; i in $s3, k in $s5, address of save in $s6! Compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: Array base address, $s6 Address of save[i], $t1 32 bits " 4 bytes Save[0] Save[1] Save[2] Save[3] Save[4] ECE331: Logic Operations + Intro to Conditionals
Procedures Also known as methods, subroutines, functions, Make a call, give control, return Split responsibilities for the data Calling function Called function
Software Conventions for MIPS Registers Register Names Usage by Software Convention $0 $zero Hardwired to zero $1 $at Reserved by assembler $2 - $3 $v0 - $v1 Function return result registers $4 - $7 $a0 - $a3 Function passing argument value registers $8 - $15 $t0 - $t7 Temporary registers, caller saved $16 - $23 $s0 - $s7 Saved registers, callee saved $24 - $25 $t8 - $t9 Temporary registers, caller saved $26 - $27 $k0 - $k1 Reserved for OS kernel $28 $gp Global pointer $29 $sp Stack pointer $30 $fp Frame pointer $31 $ra Return address (pushed by call instruction) $hi $hi High result register (remainder/div, high word/ mult) $lo $lo Low result register (quotient/div, low word/mult) ECE331: Decision Making Instructions
Procedure Calling! Steps required 1. The calling program places parameters in registers 2. The calling program transfers control to the procedure (callee) 3. The called procedure acquire storage that it needs from memory 4. The called procedure executes its operations 5. The called procedure places results in registers for the calling program to retrieve. 6. The called procedure reverts the appropriate MIPS registers to their original or correct state. 7. The called procedure returns control to the the next word in memory from which it was called. 8. The calling program proceeds with its calculations ECE331: Decision Making Instructions
Leaf Procedure Example! C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } MIPs assumes that arguments g,, j are in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 ECE331: Decision Making Instructions
1 M I P S Reference Data CORE INSTRUCTION SET OPCODE FOR- / FUNCT NAME, MNEMONIC MAT OPERATION (in Verilog) (Hex) Add add R R[rd] = R[rs] + R[rt] (1) 0 / 20 hex Add Immediate addi I R[rt] = R[rs] + SignExtImm (1,2) 8 hex Add Imm. Unsigned addiu I R[rt] = R[rs] + SignExtImm (2) 9 hex Add Unsigned addu R R[rd] = R[rs] + R[rt] 0 / 21 hex And and R R[rd] = R[rs] & R[rt] 0 / 24 hex And Immediate andi I R[rt] = R[rs] & ZeroExtImm (3) c hex if(r[rs]==r[rt]) Branch On Equal beq I 4 PC=PC+4+BranchAddr (4) hex if(r[rs]!=r[rt]) Branch On Not Equal bne I 5 PC=PC+4+BranchAddr (4) hex Jump j J PC=JumpAddr (5) 2 hex Jump And Link jal J R[31]=PC+8;PC=JumpAddr (5) 3 hex Jump Register jr R PC=R[rs] 0 / 08 hex R[rt]={24 b0,m[r[rs] Load Byte Unsigned lbu I 24 +SignExtImm](7:0)} (2) hex Load Halfword R[rt]={16 b0,m[r[rs] lhu I 25 Unsigned +SignExtImm](15:0)} (2) hex Load Linked ll I R[rt] = M[R[rs]+SignExtImm] (2,7) 30 hex NAME NUMBER USE $zero 0 The Constant Value 0 $at 1 Assembler Temporary $v0-$v1 2-3 Values for Function Results and Expression Evaluation $a0-$a3 4-7 Arguments $t0-$t7 8-15 Temporaries $s0-$s7 16-23 Saved Temporaries $t8-$t9 24-25 Temporaries $k0-$k1 26-27 Reserved for OS Kernel $gp 28 Global Pointer $sp 29 Stack Pointer $fp 30 Frame Pointer $ra 31 Return Address Load Upper Imm. lui I R[rt] = {imm, 16 b0} f hex Load Word lw I R[rt] = M[R[rs]+SignExtImm] (2) 23 hex Nor nor R R[rd] = ~ (R[rs] R[rt]) 0 / 27 hex Or or R R[rd] = R[rs] R[rt] R[rt](15:0) (2) 0 / 25 hex Or Store Immediate Word sw I R[rt] M[R[rs]+SignExtImm] = ZeroExtImm = R[rt] (3) (2) 2b d hex Subtract sub R R[rd] = R[rs] - R[rt] (1) 0 / 22 hex 0 / 23