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?
- ANothing; the called function always restores t0
- BNothing; other functions cannot modify t0
- 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.