2

I wanted to convert this C code into MIPS.

C Code:

f = A[B[h-g]]

We assume that h > g and B[h-g] > 0. h, g, f are integers.

Also assume that f is assigned to register $s0, g to $s1, h to $s2.

Base addresses of A -> $s6 and B -> $s7

Here is my attempt:

sub $t0, $s2, $s1                   
mult $t0, $t0, 4                     
lw $t0, $t0($s7)           
mult $t0, $t0, 4           
sw $s0, $t0($s6)
6
  • What is your question ? Have you tested the code ? Does it work ? If not then what is the problem exactly ? Commented Feb 6, 2012 at 9:46
  • 1
    its a school assignment to convert C into MIPS. I have just started MIPS so I am wondering whether I have done it correctly Commented Feb 6, 2012 at 9:51
  • You need to test the code and see if it works correctly. If not then you need to debug it. If you get stuck with any of this then come back with a specific question. Commented Feb 6, 2012 at 9:54
  • I guess you are using a simulator such as SPIM ? If you don't know how to use this yet then start with the user manual. Commented Feb 6, 2012 at 10:00
  • No i am not using any simulator for this course but I'll try SPIM. Anyway I thought maybe someone might help me in converting this statement. Its only a one simple C statement. Commented Feb 6, 2012 at 10:10

1 Answer 1

1

It looks good, apart from the last line, which should most likely be:

lw $s0, $t0($s6)

Note that you should always comment your code, particularly so when it's asm, e.g.

sub $t0, $s2, $s1         ; t0 = h - g          
mult $t0, $t0, 4          ; t0 = (h - g) * sizeof(int) = byte index into B
lw $t0, $t0($s7)          ; t0 = B[h - g]
mult $t0, $t0, 4          ; t0 = B[h - g] * sizeof(int) = byte index into A
lw $s0, $t0($s6)          ; s0 = A[B[h - g]]

Note also that you should always test your code - I would recommend using a simulator such as SPIM for this.

Sign up to request clarification or add additional context in comments.

3 Comments

I would definitely try it. Thanks a for your help
I am not sure what's wrong, but I think this is old MIPS. there are two problems with this. 1) you would have to use mul instead of mult. 2) lw $t0, $t0($s7) will give you an error. I don't think you can set the offset to be a register. It must be an integer (like 4($s7)). Is there an updated version of this?
@Babak: mostly these MIPS questions are from students who are being taught MIPS assembly programming using the SPIM simulator, so the instruction set will be whatever SPIM supports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.