Caller Saved v.s. Callee Saved

Isshiki🐈
2 min readDec 26, 2020

Based on this Stack Exchange answer.

While most of the time we store our data in the memory (stack frame), it’s also natural to use registers for fast access. According to CS:APPv3, there are generally sixteen interger registers in a CPU: Eight of them are used for more specific purposes, and the remaining eight can be used for storage. Among these eight registers then, two are labelled as caller saved and six are labelled as callee saved. But why, I wonder. What can be the difference between them?

As I just mentioned above, there are only sixteen registers in total in a CPU and functions share the same set of registers with their subroutines and if these subroutines can easily overwrite data stored in these registers, registers will then be useless for storage. To solve this problem, people have made some rules as to how to use these registers. For callee saved registers, if a function wishes to use them, it’s also responsible for saving existing content to the memory and restoring them after use and thesefore data stored in callee saved registers can safely be considered permanant; for caller saved registers, however, if a function wishes to use them, it should be aware that data in these registers may be overwritten by subroutines it calls just like, for example, an index register, and therefore if it wants to keep its data, it’s responsible for saving them somewhere.

Long story short, caller saved and callee saved are a mechanism designed to protect against possible register storage pollution by subroutines: caller saved means callers should save their stuff before calling a subroutine or risk losing them all while callee saved means callers don’t need to take these extra steps since subroutines will take care of these data for them.

--

--