ISA & Assembly
The code above is the start (prologue) of a function. What does it do?
addi sp, sp, -16
sw ra, 12(sp)
sw s0, 8(sp)
- AIt reserves stack space and saves the return address (ra) and the callee-saved s0
- BIt loads the function arguments ra and s0 from the stack
- CIt frees the stack in order to return from the function
Show answer and explanation
Answer: A. It reserves stack space and saves the return address (ra) and the callee-saved s0
Line 1 subtracts 16 from sp to allocate a stack frame, line 2 writes the return address ra into it, and line 3 writes the callee-saved s0. The function calls other functions, so it must protect ra, and it uses s0, so it must preserve s0's old value. The epilogue at the end reverses this with "lw s0, 8(sp)", "lw ra, 12(sp)", "addi sp, sp, 16" and "ret".
- A. Correct. The epilogue undoes this in reverse.
- B. sw is a store, and neither ra nor s0 is an argument.
- C. sp is decreased, so this is allocation.