What does the function prologue and epilogue for f do to the registers $sp, $ra and $fp assuming $ra and $fp are the only callee-saved registers modified by the function.
$sp - stack pointer
$fp - frame pointer
$ra - return address
The function prologue and epilogue refer to you saving the return address register (and any other registers as needed) to the stack at the beginning of the function and returning the values to their respective registers at the end of the function.
As you might have noticed, the MIPS arquitecture does not have a stack, you have to create it (you may want to check the manual for the simulator or specific processor you are using as the convened address for this varies).
You would save the address specified in the $sp in the main program:
daddi $sp, $sp, 0x400 # this is the convention address for WinMIPS64's stack
You may want to use the stack for several reasons, for instance:
- to save the return address register (
$ra) in case of nested functions
- for the
$fp, as per the question
- to save the
$s0-$s7 registers if you use them in a function. By convention these should always be saved to the stack when used (it's not applicable in this case, but someone else may be reading this, so).
To do this you would use code as the user above did (code may vary slightly depending on which MIPS version you are using, check the manual).
To PUSH to the stack you must emulate stack function using code as per above (make space for data, save data on stack). Remember that when you PUSH to the stack as above, you must also POP the equal amount to return the $sp to it's original position. You do this in the inverse order (again, as the user above shows).
Also: Because the call instruction for functions in MIPS (jal f - jump and link) saves the return address in a single register automatically ($ra - return address, remember?), you always needs to do this if you are using recursion or if you are calling a second function from a first function, otherwise you lose your return address to the main program, because it will get "stepped on" by the next call.
$fp, the frame pointer, would be used for instance if you have saved have many register values to the stack, and you would like to access the different operands without moving the $sp which would remain pointing to the top of the stack. You would load the value of $sp onto $fp in order to do this, and then add a displacement value to move around.
It permits you to load values from the stack while keeping the $sp in the same place, therefore permitting you to keep track of the push and pop operations.
How does the MIPS assembly code for f accesses the variable x.
By convention, MIPS has specific registers for arguments and for return values. In MIPS, arguments to be passed to the function are saved in registers $a0-$a3, return values are saved in $v0-$v1. Temporary registers which can be used within functions are $t0-$t9 but they do not preserver their value on return from function. However, as you may run out of registers, you may need to use the stack. By convention the $s0-$s7 are saved registers whose values are preserved by using the stack.
P.S. Important: I am using a WinMIPS64 simulator. MIPS assembly language varies in register size, instruction set & stack address, please reference your particular version manual and check which values apply in your case as this will affect your code.