What is Caller-saved register?

A register the called function may overwrite freely; if the caller needs its value after the call, the caller saves it. In RISC-V these include t0-t6 and a0-a7.

Field: ISA & Assembly

Japanese name: 呼び出し元保存レジスタ

What the agreement says

The value of such a register is treated as destroyed by a function call. The called function may use it freely without saving it. In RISC-V these are t0 through t6, used for temporary values, a0 through a7, used for arguments and return values, and ra, which holds the return address. If the caller wants to use the value after the call, it copies it to the stack or to another register before calling.

Why there are two kinds

If the rule were that the callee saves every register, even a small function would have to save all the registers it uses. If instead the caller saved everything, every value in use would have to be saved on every call. Providing a fair number of each kind lets the compiler choose where to place a value according to how it is used, which reduces the total number of saves and restores. A function that calls no other function can use only caller-saved registers and avoid saving anything.

Argument registers and interrupts

Arguments are passed in a0 through a7, and the return value comes back in a0. After a call, therefore, a0 holds the return value, not the argument that was passed. If the argument value is still needed after the call, it is copied elsewhere. Interrupt handling is different. An interrupt can occur anywhere in a program, and the interrupted code has no chance to save its values. The interrupt handler therefore saves every register it uses, caller-saved registers included.

Try it: questions about Caller-saved register

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.

FAQ

Does the caller always save them on every call?

No. Only registers holding values that will be used after the call are saved. Values that are finished with are left as they are.

Is a volatile register the same thing?

Yes. That name is also used, in the sense that the value may be lost across a function call.

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary