Basic operations
source code in C:
array[300] = a + array[300];
compiled to MIPS, suppose array in $t1 and a in $s2:
lw $t0, 1200($t1)
add $t0, $s2, $t0
sw $t0, 1200($t1)
then to machine code (Base 10):
op | rs | rt | rd | shamt | funct | address |
---|---|---|---|---|---|---|
35 | 9 | 8 | / | / | / | 1200 |
0 | 18 | 8 | 8 | 0 | 32 | / |
43 | 9 | 8 | / | / | / | 1200 |
notes that 3510 = 1000112, 4310 = 1010112.
Conditional Branches
source code in C:
if (i == j)
f = g + h;
else
f = g - h;
compiled to MIPS, suppose f to j in $s0 to $s4:
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit:
Loops
source code in C:
while (array[i] == k)
i += 1;
compiled to MIPS, suppose i in $s3, k in $s5 and array in $s6:
Loop: sll $t1, $s3 , 2 # Temp reg $t1 = i * 4
add $t1, $t1, $s6 # Thus we want $t1($s6)
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:
Reference
P&H Computer Architecture and Design