for (i = 0; i < 64; i++) {
A[i] = B[i] + C[i];
}
The MIPS instructions for the above C code is:
add $t4, $zero, $zero # I1 i is initialized to 0, $t4 = 0
Loop:
add $t5, $t4, $t1 # I2 temp reg $t5 = address of b[i]
lw $t6, 0($t5) # I3 temp reg $t6 = b[i]
add $t5, $t4, $t2 # I4 temp reg $t5 = address of c[i]
lw $t7, 0($t5) # I5 temp reg $t7 = c[i]
add $t6, $t6, $t7 # I6 temp reg $t6 = b[i] + c[i]
add $t5, $t4, $t0 # I7 temp reg $t5 = address of a[i]
sw $t6, 0($t5) # I8 a[i] = b[i] + c[i]
addi $t4, $t4, 4 # I9 i = i + 1
slti $t5, $t4, 256 # I10 $t5 = 1 if $t4 < 256, i.e. i < 64
bne $t5, $zero, Loop # I11 go to Loop if $t4 < 256
For I8, could the sw
instruction not be replaced with an addi
instruction? i.e addi $t5, $t6, 0
Wouldn't it achieve the same task of copying the address of $t6
into $t5
? I would like to know the difference and when to use either of them. Same could be said about the lw
instruction.
Also, maybe a related question, how does MIPS handle pointers?
edit: changed addi $t6, $t5, 0.