86

I'm having some trouble understanding the difference between caller and callee saved registers and when to use what.

I am using the MSP430 :

procedure:

mov.w #0,R7 
mov.w #0,R6 
add.w R6,R7 
inc.w R6 
cmp.w R12,R6 
jl l$loop 
mov.w R7,R12
ret

the above code is a callee and was used in a textbook example so it follows the convention. R6 and R7 are callee saved and R12 is caller saved. My understanding is that the callee saved regs aren't "global" in the sense that changing its value in a procedure will not affect it's value outside the procedure. This is why you have to save a new value into the callee reg at the beginning.

R12, the caller saved is "global", for lack of better words. What the procedure does has a lasting effect on R12 after the call.

Is my understanding correct? Am I missing other things?

6 Answers 6

172

Caller-saved registers (AKA volatile registers, or call-clobbered) are used to hold temporary quantities that need not be preserved across calls.

For that reason, it is the caller's responsibility to push these registers onto the stack or copy them somewhere else if it wants to restore this value after a procedure call.

It's normal to let a call destroy temporary values in these registers, though.

Callee-saved registers (AKA non-volatile registers, or call-preserved) are used to hold long-lived values that should be preserved across calls.

When the caller makes a procedure call, it can expect that those registers will hold the same value after the callee returns, making it the responsibility of the callee to save them and restore them before returning to the caller. Or to not touch them.

1
  • 32
    Another term I like to use is "call-clobbered" vs. "call-preserved". caller vs. callee only differs by one letter, and compilers don't actually save/restore call-clobbered regs across calls (they just put values in call-preserved regs instead.) volatile vs. non-volatile can cause confusion with C's volatile keyword. So "call-clobbered" accurately describes what a function needs to assume about other functions, rather than how it might implement the calling convention / ABI. Commented Aug 10, 2016 at 17:21
32

The caller-saved / callee-saved terminology is based on a simplistic model of register variables where callers actually do save/restore (back into the same register) around each call for any call-clobbered registers where they were keeping a value they want later. (Instead of keeping long-term-useful values elsewhere, or starting fresh with register allocation to reload as needed.)

You have to understand that "caller-saved" means "saved somehow if you want the value later". Not that all registers are always saved/restored around every function call by either the caller or callee.

In reality, efficient code lets values get destroyed when they're no longer needed. Compilers typically make functions that save a few call-preserved registers at the start (prologue) of a function (and restore them at the end, in the epilogue). Inside the function, they use those regs for values that need to survive across function calls. (If they run out of call-preserved registers, they might indeed need to spill something before a call, but it often doesn't need to get reloaded again right away, maybe not until a few calls later, or after a loop, and maybe not necessarily into the same register it was in before.)


I prefer "call-preserved" vs. "call-clobbered", which are unambiguous and self-describing once you've heard of the basic concept, and don't require any serious mental gymnastics to think about from the caller's perspective or the callee's perspective. (Both terms are from the same perspective.)

Plus, these terms differ by more than one letter.

The terms volatile / non-volatile are pretty good, by analogy with storage which loses its value on power-loss or not, (like DRAM vs. Flash). But the C volatile keyword has a totally different technical meaning, so that's a downside to "(non)-volatile" when describing C calling conventions.


  • Call-clobbered, aka caller-saved or volatile registers are good for scratch / temporary values that aren't needed after the next function call.

From the callee's perspective, your function can freely overwrite (aka clobber) these registers without saving/restoring.

From a caller's perspective, call foo destroys (aka clobbers) all the call-clobbered registers, or at least you have to assume it does.

You can write private helper functions that have a custom calling convention, e.g. you know they don't modify a certain register. But if all you know (or want to assume or depend on) is that the target function follows the normal calling convention, then you have to treat a function call as if it does destroy all the call-clobbered registers. That's literally what the name comes from: a call clobbers those registers.

Some compilers that do inter-procedural optimization can also create internal-use-only definitions of functions that don't follow the ABI, using a custom calling convention.

  • Call-preserved, aka callee-saved or non-volatile registers keep their values across function calls. This is useful for loop variables in a loop that makes function calls, or basically anything in a non-leaf function in general.

From a callee's perspective, these registers can't be modified unless you save the original value somewhere so you can restore it before returning. Or for registers like the stack pointer (which is almost always call-preserved), you can subtract a known offset and add it back again before returning, instead of actually saving the old value anywhere. I.e. you can restore it by dead reckoning, unless you allocate a runtime-variable amount of stack space. Then typically you restore the stack pointer from another register.

A function that can benefit from using a lot of registers can save/restore some call-preserved registers just so it can use them as more temporaries, even if it doesn't make any function calls. Normally you'd only do this after running out of call-clobbered registers to use, because save/restore typically costs a push/pop at the start/end of the function. (Or if your function has multiple exit paths, a pop in each of them.)


The name "caller-saved" is misleading: you don't have to specifically save/restore them. Normally you arrange your code to have values that need to survive a function call in call-preserved registers, or somewhere on the stack, or somewhere else that you can reload from. It's normal to let a call destroy temporary values.


An ABI or calling convention defines which are which

See for example What registers are preserved through a linux x86-64 function call for the x86-64 System V ABI.

Also, arg-passing registers are always call-clobbered in all function-calling conventions I'm aware of. See Are rdi and rsi caller saved or callee saved registers?

But system-call calling conventions typically make all the registers except the return value call-preserved. (Usually including even condition-codes / flags.) See What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64

14
  • How about a new terminology: caller-preserved/callee-preserved (where preservation may implicitly include not using)? I like having the one responsible for action being declared, but declaring who can use the registers without special action may be okay. Call-clobbered may weakly imply that the saving happens at the call site (which is kind of true for XTensa and some ISAs include stack pointer adjust instructions that also save/restore registers). Naming is hard. Mentioning that interprocedural optimization can bypass the ABI might have been worthwhile.
    – user2467198
    Commented May 17, 2019 at 2:29
  • 1
    Obviously, this nomenclature was used in the education system. It took me a while to work out why the 'saved' scenario wasn't back to front. It's counter intuitive because 'caller saved' to me constantly was being interpreted as 'saved by the caller in a register that won't be clobbered in the call' Commented Mar 28, 2020 at 15:45
  • 1
    @doug65536: ok sure, in that special case. But a compiler still wouldn't blindly save/restore all the "caller saved" registers, just the ones holding values that will be needed by code after the call. Maybe I'm making a strawman with my interpretation of the term "caller-saved", but I still find it much easier to think about the terms "call clobbered" and "call preserved", which are at least as meaningful. I think my answer makes it clear what I actually mean. I guess it's fair to argue that I'm over-simplifying by not mentioning the possibility of enregistering vars in call-clobbered regs Commented Sep 16, 2023 at 17:32
  • 1
    I'm not saying I like the caller saved terminology, I just want to show how I made peace with it.
    – doug65536
    Commented Sep 16, 2023 at 17:38
  • 1
    I added the terms "call-clobbered" and "call-preserved" to the CHG section of my Assembly Comments Explained: Guide for Advanced Learning and Style. I also listed this change in a blog post.
    – ecm
    Commented Apr 28 at 15:50
23

Callee vs caller saved is a convention for who is responsible for saving and restoring the value in a register across a call. ALL registers are "global" in that any code anywhere can see (or modify) a register and those modifications will be seen by any later code anywhere. The point of register saving conventions is that code is not supposed to modify certain registers, as other code assumes that the value is not modified.

In your example code, NONE of the registers are callee save, as it makes no attempt to save or restore the register values. However, it would seem to not be an entire procedure, as it contains a branch to an undefined label (l$loop). So it might be a fragment of code from the middle of a procedure that treats some registers as callee save; you're just missing the save/restore instructions.

2
  • Does static variable in c use registers like t in MIPS?
    – NAND
    Commented Jan 23, 2021 at 21:47
  • 2
    @NAND: static variables are generally stored in memory. On MIPS they'll need to be loaded in to registers (temporarily) to be used, but the generally "live" in memory
    – Chris Dodd
    Commented Jan 24, 2021 at 22:52
3

I'm not really sure if this adds anything but,

Caller saved means that the caller has to save the registers because they will be clobbered in the call and have no choice but to be left in a clobbered state after the call returns (for instance, the return value being in eax for cdecl. It makes no sense for the return value to be restored to the value before the call by the callee, because it is a return value).

Callee saved means that the callee has to save the registers and then restore them at the end of the call because they have the guarantee to the caller of containing the same values after the function returns, and it is possible to restore them, even if they are clobbered at some point during the call.

The issue with the above definition though is that for instance on Wikipedia cdecl, it says eax, ecx and edx are caller saved and rest are callee saved, this suggests that the caller must save all 3 of these registers, when it might not if none of these registers were used by the caller in the first place. In which case caller 'saved' becomes a misnomer, but 'call clobbered' still correctly applies. This is the same with 'the rest' being called callee saved. It implies that all other x86 registers will be saved and restored by the callee when this is not the case if some of the registers are never used in the call anyway. With cdecl, eax:edx may be used to return a 64 bit value. I'm not sure why ecx is also caller saved if needed, but it is.

1
  • 1
    ecx was a good choice of a 3rd register to be call-clobbered because it's needed for variable-count shifts, as well as for rep stos/movs. It's easily the 3rd most-needed after EAX and EDX (for div, widening multiply, and compact encodings for AL/EAX). In general you want a balance of call-preserved and call-clobbered registers so leaf functions don't need too much push/pop to get some scratch space to work with, vs. functions that use call in a loop not needing too much spill/reload inside a loop. Commented Mar 28, 2020 at 17:45
3

Caller-Saved (AKA volatile or call-clobbered) Registers

  • The values in caller-saved registers are short term and are not preserved from call to call  
  • It holds temporary (i.e. short term) data

Callee-Saved (AKA non-volatile or call-preserved) Registers

  • The callee-saved registers hold values across calls and are long term
  • It holds non-temporary (i.e. long term) data that is used through multiple functions/calls
3
  • 1
    It saves values before calling a function - no it doesn't. Values in volatile aka call-clobbered registers just get destroyed. The whole idea of actually wasting instructions copying that data somewhere else and back before/after a call is part of the brain-death of the "caller-saved" terminology. Real code doesn't work that way. Commented Aug 22, 2020 at 20:13
  • 1
    Also, your phrasing of "it saves" implies that the register itself does that for you automatically, which is of course not the case. (Same for the "it saves" in describing call-preserved regs. You have to save the caller's value around your use of it.) With those 2 "it saves" bullet points removed, it would be a complete and accurate summary of those terms. Commented Aug 22, 2020 at 20:16
  • 1
    Thanks for the clarification, @PeterCordes. I'm going to modify it by removing the second bullet points for both registers.
    – Abu Shoeb
    Commented Aug 23, 2020 at 2:24
0

I am adding to this after almost 3 years from the questions asked. In short caller saves following registers --> rdi, rsi, rdx, rcx, r8, r9. Callee saves following registers --> rbx, rbp, r12, r13, r14.

Note : I made answer from x86-64 Architecture perspective.

Reference: Computer Systems: A Programmer's Perspective by Randal Bryant (Author), David O'Hallaron (Author)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.