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.
Japanese name: フレームポインタ
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.
Japanese name: フレームポインタ
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.
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.
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.
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.
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.
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.
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.
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.
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.