They key here is add $a0, $a0, $t1
. Consider what that will do in the case of an array with N
elements: on the second iteration you'll add 1*4
, on the third iteration 2*4
, and so on. So on the third iteration you'd be trying to read from array + 1*4 + 2*4 == array + 3*4
instead of array + 2*4
. And on the N:th
iteration you'd be trying to read from array + 1*4 + 2*4 + ... + (N-1)*4
.
The sll + add
before the lw
should be removed. Updating the address can be done after the lw
with an addiu $a0, $a0, 4
.