Translating single C line to MIPS Assembly

Carlos Barreto picture Carlos Barreto · Oct 12, 2013 · Viewed 7k times · Source

I am taking a university course in computer architecture so I am new to assembly language. My question is, how do I translate the following code from C to MIPS assembly language without using pseudoinstructions?

B [8] = A [i-j]

i and j are assigned to registers $s3 and $s4 and the base address of A and B are in registers $s6 and $s7. So far I have:

sub $t0, $s3, $s4
sll $t0, $t0, 2
lw $t1, $t0($s6)   #Is this line allright?
add $s5, $t1, $zero
sw $s5, 32($s7)

I'm not sure about using register $t0 as an offset to address memory in the third line. Thanks in advance.

Answer

Konrad Lindenbach picture Konrad Lindenbach · Oct 12, 2013

No. Instead calculate the address needed by adding $t0 to $s6.

sub $t0, $s3, $s4
sll $t0, $t0, 2
add $t0, $t0, $s6
lw  $t1, 0($t0)
sw  $t1, 32($s7)