ISA & Assembly

In the RISC-V calling convention, t0-t6 are caller-saved. If a function wants to keep using a value in t0 after calling another function, what should it do?

  1. ANothing; the called function always restores t0
  2. BNothing; other functions cannot modify t0
  3. CSave t0 to the stack or elsewhere before the call and restore it after
Show answer and explanation

Answer: C. Save t0 to the stack or elsewhere before the call and restore it after

The callee may use t registers freely without saving them. So if the value is needed after the call, the caller must save it itself. Splitting the saving responsibility between caller and callee, register by register, lets most functions use temporaries with no saving work and reduces the total number of saves and restores.

  • A. The callee may freely overwrite t registers.
  • B. Any function may use t0 freely.
  • C. Correct. Or keep it in an s register in the first place.
Practice this topic Back to topic

More ISA & Assembly questions

  1. A function that calls no other functions (a leaf function) often does not need to save ra on the stack. Why?
  2. What is the purpose of executing "addi sp, sp, -16" at the start of a RISC-V function?
  3. The standard RISC-V calling convention requires sp to be a multiple of 16 bytes on function entry. What is the main reason?
  4. The code above is the start (prologue) of a function. What does it do?
  5. What is an advantage of using a frame pointer (s0/fp in RISC-V) inside a function?