Im trying to program a function to use extra arguments besides 4 (since my version of mips only supports $a0-$a3) by pushing them on the stack, but my code is incorrect. Here is my code in main (snippet):
li $t0,40 #temp value for our 5th arg.
addi $sp, $sp, -4 #decrement stack pointer by 4
sw $t0, 0($sp) #save the value of $t0 on the stack.
jal printf
which sets a temporary value of 40, gives space on the stack, and saves it. My function is then called. As a test to see if this worked, inside the function when i move these temp arguments $a0-$a3 to their saved register counterparts, I have this code:
lw $t0, 0($sp)
addi $sp, $sp, 4
move $a0,$t0
li $v0, 1
syscall
...but it only prints out a 0 and not 40, so im doing something incorrect. Any help would be greatly appreciated (and upvoted)
In the most common 32 bit MIPS calling convention, space is reserved on the stack for $a0,$a1,$a2
and $a3
, so the called function should expect to find the 5th argument at 16($sp)
.
The easiest way to figure these things out is to write an empty version of your function in C, and dissassemble the .o
file to figure out how the arguments are passed by the compiler.