Register Guide

RISC-V sp / x2 Register: Stack Pointer, Stack Frames, and ABI Alignment

sp is the stack pointer used by the RISC-V standard calling convention. Functions build stack frames around sp, save values that must be restored, and receive stack-passed arguments; the standard ABI requires sp to be 128-bit aligned on procedure entry and remain aligned during execution.

psABI: x2 is named sp and is preserved across calls.
psABI: in the standard ABI, the stack grows downward and sp has 128-bit alignment.
Beginner rule: move sp only as part of stack-frame management and restore it before return; do not use it as a scratch register.
Physical Name x2ABI Name spSave Rule Callee

The psABI marks sp as preserved across calls, but it is special: a function may move sp during its prologue to build a stack frame, as long as it is exactly restored in the epilogue. This differs fundamentally from ordinary s* registers, which must be bitwise restored if borrowed; sp is allowed to change mid-function for stack frame construction.

Role
Stack pointer
Convention
Callee-saved
Remember This First
The standard ABI requires the stack pointer to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.
psABI Reference

Stack pointer

The psABI names x2 as sp and marks it preserved across calls. A function may move sp while building its frame, but must restore it before returning.

Preserved across calls: Yes
RISC-V psABI integer register convention
Quick Understanding & Search Notes

sp / x2 is the stack pointer in the standard calling convention. The psABI marks it preserved across calls; a function may move sp to build a frame, but must restore it and maintain the standard ABI's 128-bit alignment rule.

The psABI names x2 as sp and marks it preserved across calls.
In the standard ABI, the stack grows downward and sp is 128-bit aligned on procedure entry.

When It Fits Best

  • - Build the current stack frame and save ra, s* registers, or locals.
  • - Carry extra arguments on the stack when argument registers are not enough.
  • - Move sp down on function entry and restore it before returning.

When Not To Use It This Way

  • - Do not use sp as a normal scratch register in arithmetic.
  • - Do not adjust sp without restoring it, or the call chain and local accesses will break.

What Happens Around A Call

1

The standard ABI requires the stack pointer to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.

2

A callee may move sp to build a frame, but must restore it before return.

3

Before calling another function, the current function must leave the stack in an ABI-compliant state.

Typical RV64 Stack-Frame Entry And Exit

Examples explain the rule, not a complete program
addi sp, sp, -16
sd   ra, 8(sp)
sd   s0, 0(sp)
addi s0, sp, 16
...
ld   s0, 0(sp)
ld   ra, 8(sp)
addi sp, sp, 16
ret
1.RV64 example: allocate 16 bytes of stack space (sp moves toward lower addresses)
2.Save return address ra at sp+8
3.Save old frame pointer s0 at sp+0
4.Set up current frame pointer; s0 points to caller's stack frame bottom
5.Function body; at this point, locals and args are accessible via s0-relative addressing
6.Restore old s0 from sp+0
7.Restore ra from sp+8
8.Deallocate stack space (sp returns to caller's position)
9.Return to caller via ra

Relationship With Other Registers

sp vs s0/fp (Frame Pointer)

sp is the x2 stack pointer; in the standard ABI the stack grows downward and sp is 128-bit aligned on procedure entry. s0/fp is x8; the psABI says a frame pointer is optional, but if one exists it must reside in x8/s0. Both are preserved ABI state, but sp tracks the stack position while s0/fp has a frame-pointer role only when that convention is used.

Why You Cannot Use It As A Scratch Register

Using sp as an ordinary scratch register is extremely dangerous. If sp is corrupted, saved registers, the return address, local variables, incoming/outgoing arguments, and the entire call chain's stack frames will be damaged. The most common failure mode is ret jumping to a wrong address, or the caller's local variables being silently overwritten after a function returns. sp may only be moved according to ABI rules (down in prologue, back up in epilogue) and must never participate in general-purpose arithmetic.

FAQ

Can sp / x2 be used as a temporary register?

It should not. sp is the stack pointer; corrupting it can break stack frames, saved registers, and return addresses.

What stack alignment does the RISC-V standard ABI require?

The standard ABI requires sp to be aligned to a 128-bit boundary on procedure entry and remain aligned during execution.