ISA & Assembly

What is the purpose of executing "addi sp, sp, -16" at the start of a RISC-V function?

  1. ATo grow the stack toward lower addresses and reserve a 16-byte work area
  2. BTo free 16 bytes on the stack
  3. 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.
Practice this topic Back to topic

More ISA & Assembly questions

  1. The standard RISC-V calling convention requires sp to be a multiple of 16 bytes on function entry. What is the main reason?
  2. The code above is the start (prologue) of a function. What does it do?
  3. What is an advantage of using a frame pointer (s0/fp in RISC-V) inside a function?
  4. In RISC-V, what happens if a recursive function (one that calls itself) forgets to save ra on the stack?
  5. On little-endian RISC-V, a register holding 0x12345678 is stored with sw to address 0x100. What value does lbu then read from address 0x101?