ISA & Assembly

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.

  • 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.
Practice this topic Back to topic

More ISA & Assembly questions

  1. In RISC-V, what happens if a recursive function (one that calls itself) forgets to save ra on the stack?
  2. On little-endian RISC-V, a register holding 0x12345678 is stored with sw to address 0x100. What value does lbu then read from address 0x101?
  3. In RISC-V, what can happen when the address of a 4-byte lw is not a multiple of 4 (misaligned)?
  4. The RISC-V instruction "slli t0, a0, 3" makes t0 how many times the value of a0 (ignoring overflow)?
  5. a0 holds the start address of an int (4-byte) array a, and a1 holds an index i. What does the code above load into a2?