-
Convert C into MIPS
I wanted to convert this C code into MIPS.
C Code:
f = A[B[h-g]]
We assume that h > g & 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)
-
Why do you post in a C forum if you want to write MIPS assembly?
-
oh...then where should I post this??
-
I would compile with the -S option (or whatever it is for your compiler) to see what the compiler generates. at least as an example.
-
It should probably be posted in the Tech Board. If a mod wants to move it...
You're almost there. I think that last line ought to be a lw as well. The way you have it written, you're storing $s0 in $t0($s6), i.e. reversing the assignment, putting f into A[B[h-g]]. You want to load A[B[h-g]] into $s0 (f). Other than that, it looks right, but it's been a while since I had anything to do with MIPS, so I wouldn't notice more subtle problems.
-
@anduril462 Yes it should be lw. Thanks a lot guys!
The corrected code is shown below:
Code:
sub $t0, $s2, $s1
mult $t0, $t0, 4
lw $t0, $t0($s7)
mult $t0, $t0, 4
lw $s0, $t0($s6)
-
Just my 2c, but you might shave off some cycles by replacing the mult with sll:
Code:
sub $t0,$s2,$s1
sll $t0,$t0,2
lw $t0,$t0($s7)
sll $t0,$t0,2
lw $s0,$t0($s6)