ISA & Assembly

A function that calls no other functions (a leaf function) often does not need to save ra on the stack. Why?

  1. ABecause leaf functions do not use the ret instruction
  2. BBecause the CPU automatically pushes the return address of leaf functions
  3. CBecause it calls no one, ra is never overwritten and the return address stays intact
Show answer and explanation

Answer: C. Because it calls no one, ra is never overwritten and the return address stays intact

Every jal overwrites ra, so a function that calls others must first save its own return address on the stack. A leaf function never executes jal, so ra stays intact and it can ret without saving it. If it also avoids callee-saved registers, it does not even need a stack frame. x86's call instruction always pushes the return address on the stack, which is a difference from RISC-V.

  • A. Leaf functions also return with ret.
  • B. RISC-V jal only writes a register; it does not push to the stack.
  • C. Correct. It never executes a jal that would overwrite ra.
Practice this topic Back to topic

More ISA & Assembly questions

  1. What is the purpose of executing "addi sp, sp, -16" at the start of a RISC-V function?
  2. The standard RISC-V calling convention requires sp to be a multiple of 16 bytes on function entry. What is the main reason?
  3. The code above is the start (prologue) of a function. What does it do?
  4. What is an advantage of using a frame pointer (s0/fp in RISC-V) inside a function?
  5. In RISC-V, what happens if a recursive function (one that calls itself) forgets to save ra on the stack?