What is Frame pointer?

A register that keeps pointing at the stack position on function entry; s0 (fp) in RISC-V. Locals can be addressed at fixed offsets, and the call chain is easy to walk.

Field: ISA & Assembly

Japanese name: フレームポインタ

What a frame pointer does

When a function is called, an area on the stack called the stack frame is set aside for the return address, saved registers and local variables. The stack pointer (sp) may move inside the function, for example when allocating a variable-length array or pushing arguments, but the frame pointer (fp) keeps pointing at a position fixed on function entry and does not move until the function returns. Local variables and arguments can therefore be addressed at fixed offsets, as in so many bytes from fp. In RISC-V, s0 (x8) doubles as fp; x86-64 uses rbp and AArch64 uses x29 as the frame pointer.

Prologue and epilogue

The opening part of a function, the prologue, lowers sp to allocate the frame, saves the return address and the caller's fp in the frame, and then sets fp to its new value. In a typical 64-bit RISC-V example, the function allocates 16 bytes from sp, saves ra and s0, and then loads s0 with the value sp had on entry. The closing part, the epilogue, does the reverse: it reloads the saved ra and s0, restores sp and returns to the caller. On x86-64 the same thing takes two instructions: push rbp onto the stack, then copy rsp into rbp.

Walking the call chain

If each frame saves the caller's fp and the return address, the frames link together like a linked list. Following the saved fp values from the current fp reveals which function called which, a backtrace, quickly and without debug information. Debuggers use this to display the stack, as do crash logs and profilers that periodically stop a running program and record its callers. Because the walk is simple and fast, it is easy to use inside the kernel and in profilers that run continuously in production.

Omitting the frame pointer

A frame pointer ties up one register and adds instructions to the prologue and epilogue, so optimizing compilers often omit it. With optimization enabled, GCC and Clang default to -fomit-frame-pointer on many targets, and local variables are addressed relative to sp. Even then, a backtrace can be obtained from the DWARF call frame information (CFI) the compiler emits, but this is more expensive. To make profiling easier, some Linux distributions build their packages with frame pointers kept.

Try it: questions about Frame pointer

What is an advantage of using a frame pointer (s0/fp in RISC-V) inside a function?

  1. AIt avoids using an extra register, so performance always improves
  2. BLocals and arguments can be addressed at fixed offsets even if sp moves, and debuggers can walk the call chain easily
  3. CThe stack grows automatically and never overflows
Show answer and explanation

Answer: B. Locals and arguments can be addressed at fixed offsets even if sp moves, and debuggers can walk the call chain easily

A frame pointer is a register that keeps pointing at the stack position on function entry and does not move. Even if sp changes partway, for example with variable-length arrays, locals can be addressed as "so many bytes from fp". And if each frame stores the previous fp, following them yields the call chain (a backtrace). It does occupy one register, though, so optimized code often omits it.

FAQ

What is the difference between a frame pointer and a stack pointer?

The stack pointer points at the current top of the stack and moves whenever values are pushed or popped or a frame is allocated. The frame pointer keeps pointing at the position from function entry and does not move, so local variables can be addressed at fixed offsets, and it serves as the starting point for walking the call chain.

Is a frame pointer necessary?

No. Local variables can also be addressed relative to sp, so optimized code often omits it. However, functions in which sp changes partway through, for example because of variable-length arrays, use a frame pointer, and keeping one is also handy when you want backtraces easily.

Which register is the frame pointer in RISC-V?

x8. The callee-saved register s0 doubles as the frame pointer and can also be written under the alias fp. In functions that do not use a frame pointer, s0 serves as an ordinary saved register.

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