0
votes

Just started learning assembly for one of my classes and I am a bit confused over this code segment. This is from a textbook question which asks you to translate from MIPS instructions to C. The rest of the question is in the attached image.

For the MIPS assembly instructions above, what is the corresponding C statement? Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.

sll  $t0, $s0, 2    # $t0 = f * 4
add  $t0, $s6, $t0  # $t0 = &A[f]
sll  $t1, $s1, 2    # $t1 = g * 4
add  $t0, $s6, $t0  # $t1 = &B[g]
lw   $s0, 0($t0)    # f = A[f]
addi $t2, $t0, 4
lw   $t0, 0($t2)
add  $t0, $t0, $s0
sw   $t0, 0($t1)

I have a basic understanding of some MIPS instructions but frankly, the stuff with arrays confuses me a bit. Could anyone here point me in the right direction? Thanks!

1
So can you describe to yourself what this code does? - Eugene Sh.
I could be completely wrong here, but is the first add immediate command after the last commented line simply assigning temp register t2 the value of A[f+4] ? - purdoo
Since we don't really know the datatype of A, it is hard to tell.. Looks like it is 4 bytes wide, if there is no mistake in the question - Eugene Sh.
Yes I believe that is correct and one of the assumptions we are supposed to make in this course. Basically, I don't understand the last lw command and I am a bit unsure what the program does a whole. I know it performs operations on an array and then stores a value back into memory, but that is about it. - purdoo
Ok, I am not familiar with MIPS assembly, and I am only using my intuition here. So the addi command adds 4 to t0 storing the result into t2. next lw, i guess standing for "load word", it is loading t0 with a value from memory location pointed by t2, which is A[f+1] (since we have incremented f*4 by another 4. so t2=A[f+1]. sw is an opposite of lw, and it is transferring the value from the register to memory. From here continue by yourself. - Eugene Sh.

1 Answers

0
votes

It's been a while since I last wrote MIPS assembly. However, from what I can understand from the first few instructions:

sll $t0, $s0, 2     # t0 = 4 * f
add $t0, $s6, $t0   # t0 = &A[f]

s0 holds the index f at which you want to access array A. Since you multiply f by 4, A is an array of some datatype with 4 bytes length (probably an int). s6 is holding the array address, because to access the address of A[f] you would essentially do (in pseudocode)

address_of_A[f] = base_address_of(A) + offset_of_type_int(f)

The same stuff in principle happens in the next 2 instructions, but this time for array B. After that:

lw $s0, 0($t0)      # f = A[f]
addi $t2, $t0, 4    # t2 = t0 + 4 

The first load is obvious, s0 gets the value at address t0, which is of course A[f]. The second increments t0 by 4 and stores it to t2, which means that t2 now contains the address &A[f+1], since we know that array A contains 4-byte data.

The last lw command:

lw $t0, 0($t2)

stores the value at address $t2 on $t0, so $t0 is now A[f+1].