Function calls

Suppose we study the MIPS assembly code for function f with only one local variable x:

void f(void) { int x; ... } 

I have two questions:

  • What the prolog and epilogue function for f does for the registers $ sp, $ ra and $ fp, assuming $ ra and $ fp, are the only user-registered registers modified by the function.

  • How MIPS assembly code for f accesses the variable x.

My attempt: The prolog function saves the registers $ ra and $ fp to the call stack. The epilogue function restores these registers by popping them from the stack and returning control to the address in $ ra. You do not know how MIPS can access the variable x, but I know that local variables are also stored on the stack.

+4
source share
2 answers

(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 #4 bytes for $ra + 4 bytes for 'int x' = 8 bytes allocated sw $ra, 4($sp) #note the order, $ra is always first sw [int x], 0($sp) 

OR

 addi $sp, $sp, -8 #an alternate to the code above sw $ra, 4($sp) sw [int x], 0($sp) 

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) #it can be any temporary register 

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.

+6
source

Take a look at the MIPS calling convention here . Usually a local variable in a function is stored in a temporary (stored) register $ t0 - $ t9. If the function itself calls the function, local variables are stored on the stack.

MIPS does not have push or pop instructions, so the prolog function reduces the stack pointer by enough words to allow all the needs of the stack to be stored in the function, and the epilogue function cancels this.

0
source

All Articles