I am trying to understand how convert C code to MIPS code and I have having trouble understanding why the stack pointer( $sp ) needs to be manipulated before and after the procedural code.Isn't the program supposed to automatically increment the stack pointer after every instruction?
C CODE :
int myMethod (int g, h, i, j)
{
int f;
f = (g + h) - (i + j);
return f;
}
If we let g, h, i, j = $a0, $a1, $a2, $a3 and f = $s0 and result = $v0 then, MIPS CODE :
addi $sp, $sp, -4
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1 #SUB STATEMENT
add $v0, $s0, $zero
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
I don't really know why the stack pointer is decremented by 1 word size and then it's value loaded into the f variable, if you look at the subtraction statement you will notice that the value of f is then overwritten by the result of the subtraction so what was the use?