(a) What the prolog and epilogue function does for f for the registers $ sp, $ ra and $ fp, assuming $ ra and $ fp, are the only user-registered registers modified by the function.
( $ fp is the "frame pointer", also known as the "base pointer", $ sp is the stack pointer, and $ ra is the "return address")
To explain how " int x " is available, it is important to know how and where it is stored. Since " int x " is a local variable, MIPS will allocate the appropriate space (or if the space used by the markgz method was used) for the integer on the stack, subtracting the number of bytes (4) for the 32-bit integer from the stack pointer. The callerโs return address is also saved (another 4 bytes) so that the function can refer to the caller:
sub $sp, $sp, 8
OR
addi $sp, $sp, -8
Similarly, at the end of a function call, the function restores registers to the caller, freeing up space on the stack:
lw [int x], 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8
I donโt have much experience using the frame pointer ( $ fp ), but if the stack pointer ( $ sp ) changes the value during the procedure, it can no longer be used as a breakpoint, so ( $ fp ) takes its place ($ sp is still single register).
(b) How the MIPS assembly code for f accesses the variable x.
To access int x , the ' f ' function can load the variable into a temporary register.
lw $t0, 0($sp)
Since local variables are not stored in all function calls, they can be stored in temporary registers. Essentially, the push command will be โ sw โ (โsave the wordโ), and the โ pop โ command will be โ lw '(' load word ').
Also, I know that MIPS can be painful, and this fact sheet really helped me.