What is Stack pointer?

The register that points to the current top of the stack; sp (x2) in RISC-V. In many ISAs the stack grows toward lower addresses.

Field: ISA & Assembly

Japanese name: スタックポインタ

What the stack pointer tracks

The stack is a last-in, first-out region of memory used for function calls, and the stack pointer is the register holding the address of its current top. In RISC-V it is register x2, named sp; on x86-64 it is RSP. In most ABIs the stack grows toward lower addresses, so allocating space means subtracting from the stack pointer and freeing it means adding back. x86 has dedicated push and pop instructions that adjust RSP automatically, while RISC-V uses ordinary instructions, such as addi sp, sp, -16 followed by stores at offsets from sp.

Stack frames and function calls

When a function is entered, its prologue moves the stack pointer down to reserve a stack frame. The frame holds the return address if the function makes further calls, callee-saved registers it will use, and local variables that do not fit in registers. In the standard RISC-V calling convention, the first eight integer arguments travel in registers a0–a7, and further arguments are placed on the stack at fixed offsets from sp. The epilogue restores registers and adds the frame size back. Because each call gets its own frame, recursion works naturally.

Stack overflow and stack attacks

The stack has a finite size, set by the operating system for each thread or, on a microcontroller, by the linker script and start-up code that initialize the stack pointer. Deep recursion or large local arrays can push the stack pointer past its limit, a stack overflow that corrupts other memory or triggers a fault. Because return addresses sit on the stack next to local buffers, writing past the end of a local array can overwrite a return address, a classic attack. Stack canaries and non-executable stacks are standard defenses.

Stack pointer vs. frame pointer

The stack pointer moves whenever space is allocated, so offsets to a given variable can change within a function. A frame pointer, s0/fp in RISC-V or RBP on x86-64, can be set once at function entry to a fixed position in the frame, making local and argument addresses constant and stack traces easy to walk; optimizing compilers often omit it to free a register. During a context switch, the stack pointer is saved along with the program counter and other registers, because each thread has its own stack and must resume on it.

Try it: questions about Stack pointer

When a program calls a subroutine (function), which area is generally used to temporarily save the return address and similar information?

  1. AThe stack
  2. BCache tags
  3. CA fixed region of ROM
Show answer and explanation

Answer: A. The stack

Calling a subroutine pushes the return address, registers to be preserved and local variables onto the stack. On ARM and RISC-V, the return address first goes into a link register and is saved to the stack when needed. Its last-in, first-out (LIFO) structure ensures correct returns even with nested or recursive calls, and the stack pointer marks its top. Excessive recursion exhausts it (stack overflow), and buffer overruns that overwrite return addresses are a classic attack.

In the standard RISC-V calling convention, when calling a function with ten integer arguments, where are the ninth and tenth passed?

  1. AIn s0 and s1
  2. BIn t0 and t1
  3. COn the stack
Show answer and explanation

Answer: C. On the stack

There are only eight argument registers, a0-a7, so from the ninth argument on the caller places them on the stack and the callee reads them at fixed positions relative to the stack pointer. The extra memory accesses make functions with very many arguments a little slower. Passing a pointer to a struct instead of many separate values is a common way to keep the argument count down.

FAQ

Which register is the stack pointer in RISC-V?

In RISC-V the stack pointer is x2, given the ABI name sp. The standard calling convention keeps it 16-byte aligned and the stack grows toward lower addresses.

Why does the stack grow downward?

It is a convention adopted by most ABIs, historically convenient because the heap can grow upward from low memory while the stack grows downward from high memory, using the free space between them. The hardware or ABI defines the direction, and grows-down is by far the most common.

What is the difference between the stack pointer and the frame pointer?

The stack pointer always marks the current top of the stack and moves as space is allocated. The frame pointer stays fixed for the duration of a function call, giving stable offsets to locals and making it easy to walk the chain of frames when debugging.

Questions that mention Stack pointer (1)

Stack pointer appears in 3 practice questions across 3 topics, including CPUs and architecture, ISA & Assembly, Hardware Security: Advanced Topics and Trends.

  1. Which correctly describes the mechanism of Arm pointer authentication (PAC)?

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary