ISA & Assembly
What is the purpose of executing "addi sp, sp, -16" at the start of a RISC-V function?
- ATo grow the stack toward lower addresses and reserve a 16-byte work area
- BTo free 16 bytes on the stack
- CTo subtract 16 from sp and return it as the result
Show answer and explanation
Answer: A. To grow the stack toward lower addresses and reserve a 16-byte work area
By the convention of many ISAs, RISC-V included, the stack grows from higher toward lower addresses. Subtracting 16 from sp makes the 16 bytes between the new sp and the old one this function's stack frame, where the return address, saved registers and local variables can be kept. At the end, "addi sp, sp, 16" restores sp and frees the area, returning to the state before the call.
- A. Correct. The stack grows toward lower addresses.
- B. Freeing is the final "addi sp, sp, 16".
- C. Return values go in a0.