variables f,g,h,i and j are assigned to registers $s0, $s1, $s2, $s3, and $s4 respectively

The base addresses for the arrays A and B are in registers $s6 and $s7

addi $t0, $s6, 4    #load the forth element of array A into $t0
add  $t1, $s6, $0   #load the first element of array A into $t1
sw   $t1, 0($t0)    #store $t0(the forth element of array A) into $t1
lw   $t0, 0($t0)    #load $t0 into $t0
add  $s0, $t1, $t0  #add $t1 and $t0 into $s0 

comments are added by me and are my attempt at understanding the code

I really don't understand lines three and four. How did these temporary registers "become arrays"?
specifically the part '0($t0)' is what's confusing me. Wouldn't this imply that $t0 is an array and we're trying to access the first element of that array?

P.S. I have to convert this code to C after, so I'll post my conversion after I've gained a better understanding of this code.

Edited by steven.myers.3726: cleaning up code

4 Years
Discussion Span
Last Post by sepp2k

The first two lines don't load anything. You'd need a load instruction for that. They simply perform pointer arithmetic.

The first line makes $t0 point to the fifth byte of the A array. If A is an array of 32-bit values, that means that $t0 points to the second element of the array - not the fourth or fifth.

The third line takes the value that's in the register $t1 (i.e. the address of A) and stores it in the memory location that $t0 points to, i.e. it stores it as the first element of A.

The fourth lines takes the second element of A (which has been set on the previous line to be the address of A) and stores it in the register $t0 (which previously stored the address of that element).

You seem to understand the fifth line fine.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.