ECE331: Hardware Organization and Design Lecture 7: MIPs Decision-Making Instructions Working with Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
Overview Computers need to make decisions Microprocessors support conditional operations Operations typically called branch operations Connections with common Java and C language constructs If, else, switch, for loops, do loops Conditional instructions have a specific format To accomplish complicated tasks, we use the concept of procedures ECE331: Decision Making Instructions 2
Basic Blocks A basic block is a sequence of instructions with No embedded branches (except at end) No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks ECE331: Decision Making Instructions 3
Conditional and Unconditional branches Conditional branch beq $t0, $t1, L1 bne $t0, $zero, -25 Number of instructions to jump (can be negative) Unconditional branch: j L1 jr $s5 (useful for large case statements and big jumps) Jump register ECE331: Decision Making Instructions 4
while statement while ( condition ) { statements } while_start_label: # MIPS code for the # condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, while_end_label # MIPS code for the statements j while_start_label while_end_label: ECE331: Decision Making Instructions 5
do-while statement do { statements } while ( condition ); do_start_label: # MIPS code for the statements do_cond_label: # MIPS code for the # condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, do_end_label j do_start_label do_end_label: ECE331: Decision Making Instructions 6
for loop for ( init ; condition ; incr ) { statements } # MIPS code for the init expression for_start_label: # MIPS code for the condition # expression for_end_label: # (if condition satisfied set $t0=1) beq $t0, $zero, for_end_label # MIPS code for the statements # MIPS code for the incr expression j for_start_label ECE331: Decision Making Instructions 7
switch statement switch ( expr ) { } case const1: statement1 case const2: statement2... case constn: statementn default: default-statement ECE331: Decision Making Instructions 8
MIPS code for switch statement # MIPS code for $t0=expr beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constn, switch_label_n j switch_default switch_label_1: # MIPS code to compute statement1 switch_label_2: # MIPS code to compute statement2... switch_default: # MIPS code to compute default-statement switch_end_label: ECE331: Decision Making Instructions 9
Comparisons - What about <,, >,? bne, beq provide equality comparison slt (set on less than) provides magnitude comparison slt $t0,$s3,$s4 # if $s3<$s4 $t0=1; # else $t0=0; Combine with bne or beq to branch: slt $t0,$s3,$s4 # if (a<b) condition register bne $t0,$zero,less # goto Less; Why not include a blt instruction in hardware? Supporting in hardware would lower performance Assembler provides this function if desired (by generating the two instructions) ECE331: Decision Making Instructions 10
Switch Example switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add $t0, $zero, $zero # $t0 = 0, temp. variable beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j default # go to default case case0: addi $s2, $zero, 3 # j = 3 j finish # exit switch block ECE331: Decision Making Instructions 11
SPIM Example switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add $t0, $zero, $zero # $t0 = 0, temp. variable used in switch beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j deflt # go to default case case0: addi $s2, $zero, 3 # j = 3 j fin # exit switch block ECE331: Decision Making Instructions 12
Working with Procedures: Overview To achieve complex tasks, increase efficiency and to modularize computation, we rely on procedures Procedures have different names in different languages Java: methods C: procedures FORTRAN: subroutines Control flow passes to a new section of code, then returns to after the call MIPs includes registers to support data structures for procedures Goal: Understand what happens when a procedure is called/returns ECE331: Decision Making Instructions 13
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 14
Procedures (aka subroutines) Complexity is added to programming through the use of subroutines. Subroutines can serve as intermediaries that branch into other subroutines Or they can be a terminal computation that returns a result to the main trunk of the program Within MIPS, we must adhere to are conventions as to how data is preserved in the 32 registers that make up the computational engine of the processor ECE331: Decision Making Instructions 15
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 16
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 17