0
\$\begingroup\$

The following problems deal with translating from C to MIPS. 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 registers $s6 and $s7, respectively. Write the MIPS code for the following C lines of code: I am trying to mimic the following code snippet from a C program and write it in MIPS.

if(g != h){
      f = A[3];
   } else{
      f = B[4];
   }

Below is the MIPS I have so far

bne $s1, $s2, Tru        
lw  $s0, 16($s7)         
j Done                   
Tru: lw  $t0, 12($s6)   
     add $s0, $t0, $zero
Done:

If I was unclear please comment below.

\$\endgroup\$
1
\$\begingroup\$

You used lw $s0, 16($s7) for your f=B[4]; line.

As long as that line works, why are you overcomplicating f=A[3]; with the additional ASM line add $s0, $t0, $zero?
Just lw $s0, 12($s6) & be done with it.

\$\endgroup\$
  • \$\begingroup\$ ok so as long as you lw into the $s0 register, your fine? \$\endgroup\$ – Brandon Woodard Mar 1 '16 at 5:47
  • \$\begingroup\$ @BrandonWoodard If you were to assume that you could not lw into the $s0 register directly, how would using lw into $t0; then adding $t0+$zero into $s0 circumvent anything? - To my knowlege, there's no intance where you could gain anything (besides increased CPU lag) by adding the second operation there. .. So yes, as long as you lw the correct value into $s0, that sets f=__ correctly. \$\endgroup\$ – Robherc KV5ROB Mar 1 '16 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.