ISA & Assembly
What is an advantage of using a frame pointer (s0/fp in RISC-V) inside a function?
- AIt avoids using an extra register, so performance always improves
- BLocals and arguments can be addressed at fixed offsets even if sp moves, and debuggers can walk the call chain easily
- 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.
- A. On the contrary, it dedicates one register.
- B. Correct. It serves as the frame's fixed reference point.
- C. It does nothing to prevent stack overflow.