Reputation: 409
I'm having trouble converting a C code, that contains a function call to another function, into MIPS.
Here is the info:
The function declaration for go is int now(int i, j). The C code for the function now is:
int now(int i, int j, int k, int l){
return go( go(i, j), k+l);
}
Here is my MIPS code so far:
now:
addi $sp, $sp, -8 # adjust the stack to store 2 items
sw $ra, 4($sp) # save return address of func(i, j)
sw $t0, 0($sp) # save register for use afterwards
move $a0, $a0
move $a1, $a1
jal go
add $t0, $a2, $a3 # register $t0 = k + l
move $a1, $t0
jal go
lw $t0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
I'm having trouble with go(i, j) being passed as a parameter for the function go
Any inputs would be appreciated.
Thanks!
Upvotes: 2
Views: 149
Reputation: 39551
This question probably doesn't belong on this site, but here's my comments.
sw $t0, 0($sp) # save register for use afterwards
You don't need to save $t0
, as it's assumed to be clobbered by functions.
move $a0, $a0
move $a1, $a1
These two moves do nothing so remove them.
jal go
The call may clobber a2
and a3
.
add $t0, $a2, $a3 # register $t0 = k + l
Move this statement before the call and change the register to $s0
and save that register instead of saving $t0
. If you do that it doesn't matter if a2
or a3
get clobbered because you don't need their values anymore.
move $a1, $t0
jal go
You didn't give anything as the first argument in the call. Move the return value of the previous call from $v0
to $a0
. Load $a1
with the value stored in $s0
as recommended above.
lw $t0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 4
jr $ra
You saved 2 registers for a total of 8 byte but you're only adding 4 to $sp
.
Upvotes: 2