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. In the standard ABI, the stack grows toward lower addresses; sp must be aligned to a 128-bit boundary on procedure entry and remain aligned throughout 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 Preserved across calls

The psABI marks sp as preserved across calls: its entry value must be restored on return. Adjusting sp to build a stack frame must still satisfy the standard ABI requirement for 128-bit alignment on procedure entry and throughout execution.

Role
Stack pointer
Convention
Preserved across calls
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.

An RV64 Example With 16-Byte Alignment

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 s0 to sp plus 16; this is the example's frame-pointer layout, not a requirement for every procedure
5.Function body; code and ABI rules determine the concrete layout of locals and arguments
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.

Stack Pointer Constraints

Do not use sp as an ordinary scratch register. The standard ABI requires 128-bit alignment on procedure entry and continued alignment during execution; handwritten code that adjusts sp must maintain those conditions.

FAQ

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.