Logical and Decision Operations Chapter 2
Logical operations Shift left Shift right Bit-wise AND Bit-wise OR Example: sll $t2,$so,4 Reg t2 = $so << 4 Shift right Example: srl $t2,$so,4 Reg t2 = $so >> 4 Bit-wise AND Example: and $t0,$t1, $t2 Reg t0 = reg t1 & reg t2 Bit-wise OR Example: or $t0,$t1,$t2 Reg $t0 = $t1 | $t2
Instructions for Selection (if..else) If (i == j) then f = g + h; else f = g – h; bne $s3,$s4, else add $s0,$s1,$s2 j done else: sub $s0,$s1,$s2 done:
Instructions for Iteration (while) while (save[i] == k) i = i + 1; Let i be in reg $s3 Let k be in reg $s5 Let $t1 have the address of Save array element Loop: sll $t1,$s3,2 add $t1,$t1$s6 lw $t0,0($t1) bne $t0,$s5,Exit addi $s3,$s3,1 j Loop
Compiling C procedures int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } How do you pass the parameters? How does compiler transport the parameters?
Passing Parameters/arguments Special registers for arguments: a0, a1, a2, a3 Save temp register on the stack Perform operations And return value Restore values stored on the stack Jump back to return address