Skip to content
Registersx86-64

RSP Register

RSP is the x86-64 stack pointer: 16-byte alignment, the System V red zone, Windows shadow space, why sub rsp, 0x28 is everywhere, and how stack pivots abuse it.

RSP holds the address of the top of the stack, which on x86 is the lowest address in use. push subtracts 8 and writes; pop reads and adds 8; call pushes the return address; ret pops it into RIP. Everything else about the register is convention, and the conventions are where the bugs and the tricks live.

What sits at [rsp]

At the first instruction of any function, [rsp] is the return address. That single fact is enough to find your caller in a debugger with no symbols and no frame pointers:

x64dbg: dq rsp gdb: x/gx $rsp

In frameless code (most optimized code) locals and outgoing arguments are addressed from RSP directly, and the offsets shift every time the function pushes something. Ghidra and IDA track this as the stack delta. When that tracking goes wrong, usually after a call to a function with an unusual convention or a call used as a push, every local after it is mislabelled. IDA marks the function with "sp-analysis failed"; fix the delta at the offending instruction (Alt+K) and the locals fall back into place.

Alignment

Both ABIs require RSP to be a multiple of 16 at the point of a call. The call pushes 8 bytes, so at function entry RSP is 8 off, and the prologue has to fix that before calling anything else.

This is why hand-written shellcode and ROP chains crash inside system() or printf on a movaps instruction: glibc uses aligned SSE stores on the stack, and a chain that arrives with RSP misaligned faults there. The usual fix is one extra ret gadget to shift the stack by 8.

Shadow space and the red zone

The two ABIs disagree about the memory on either side of RSP.

On Windows, the caller reserves 32 bytes above the return address for the callee to spill its four register arguments. Combined with alignment, that produces the most common instruction pair in any Windows binary:

asm
sub  rsp, 0x28      ; 0x20 shadow space + 8 to realign
...
add  rsp, 0x28
ret

On System V there is no shadow space, but the 128 bytes below RSP are a red zone that signal handlers will not touch. Leaf functions use it without moving RSP at all, so mov [rsp-0x8], rdi with no sub rsp before it is legitimate. Kernel code is built with -mno-red-zone, and Windows has no red zone: anything below RSP there can be overwritten at any moment.

Large frames

A function that needs more than a page of stack cannot just subtract. On Windows it calls __chkstk with the size in rax, which touches each page in turn so the guard page is hit in order. GCC and Clang do the same inline with -fstack-clash-protection, as a loop of sub rsp, 0x1000; or qword ptr [rsp], 0. A call __chkstk near the top of a function tells you it has a large local buffer, which is often the interesting one.

Encoding

RSP as a base register always needs a SIB byte, because its ModRM slot is the escape that announces one:

asm
48 8b 04 24    mov rax, [rsp]      ; 24 is the SIB byte
48 8b 03       mov rax, [rbx]

It can never be used as an index register. R12 shares the SIB requirement.

Sub-registers

ESP, SP and SPL. You will almost never see them in 64-bit code. A write to ESP zeroes the upper half of RSP, which in a 64-bit process is nearly always a crash, and occasionally a deliberate stack pivot into the low 4 GB.

Reverse-engineering notes

A stack pivot is any gadget that loads RSP from something the attacker controls: xchg rax, rsp; ret, mov rsp, rbp; pop rbp; ret (which is leave; ret), or pop rsp; ret. Seeing RSP pointing into the heap in a crash dump means one already ran.

push <value>; ret is a jump that avoids a jmp instruction, and call $+5; pop rax reads RIP. Both are common in packers and shellcode, and both confuse stack tracking in disassemblers.

Related: RBP, the stack, push and pop, call and ret, and the register map.