Questions tagged [stack-pointer]

The register that points to the current location in the call-stack. Details vary by CPU architecture, but implicit use by push/pop instructions is common. (Please also include an architecture tag!)

CPU architectures that use a call-stack usually have an integer register dedicated to holding a pointer to the boundary between in-use and free stack space.

It's common to call this the "top" of the stack, even though it's the lowest/bottom address on most systems. (Having the stack grow downward while the heap grows upward is a very common convention (see also this Q&A). Diagrams of stack layouts get drawn either way—some with the high address at the top, and others with the low address at the top—so double-check that your terminology matches what you're reading or modifying.

NOTE: The term "stack pointer" only applies to a call-stack used as part of function call/return and/or saving of call-preserved registers for nested function calls, and making space (aka a stack frame) for local variables in a function.
It does not refer to to pointers into other stack data-structures used more generally.

The use of a stack pointer conveniently enables recursion and re-entrant functions (compared to static storage). See this MIPS Q&A.

Some architectures (e.g. ) hard-wire the choice into the design by having interrupt-handlers use the stack-pointer register implicitly to push context onto the stack. x86 also has many instructions that implicitly use the stack pointer (like push / pop, call / ret), but those could be avoided if desired. However, there's no way around having a valid value in at least the kernel's [e/r]sp for interrupts.

Other architectures (notably ) only use a specific register as the stack pointer by convention (i.e., the ABI/calling convention), and a different ABI could use a different register as the stack pointer with no loss of efficiency. Or even use no traditional stack at all, even for interrupt handling.


The stack pointer on various architectures:

In general, questions should also be tagged with one of these architecture-specific tags!

156 questions
10
votes
2 answers

Stack alignment in x64 assembly

how is the value of 28h (decimal 40) that is subtracted from rsp calculated in the following: option casemap:none includelib kernel32.lib includelib user32.lib externdef MessageBoxA : near externdef ExitProcess : near .data text …
vandale
  • 3,600
  • 3
  • 22
  • 39
9
votes
1 answer

what is the difference between ESP register and SS register?

I'm just a beginner in Assembly language. As I know, ESP and SS both refer to stack registers but not quite understand the differences between them.
user188276
7
votes
1 answer

Why GCC generates strange way to move stack pointer

I have observed that GCC's C++ compiler generates the following assembler code: sub $0xffffffffffffff80,%rsp This is equivalent to add $0x80,%rsp i.e. remove 128 bytes from the stack. Why does GCC generate the first sub variant and not the…
Heygard Flisch
  • 269
  • 2
  • 5
7
votes
2 answers

What's the Difference Between Stack Pointer and Frame Pointer in Assembly ARM

I was wondering if someone could please explain to me what's the difference between the Stack Pointer and the Frame Pointer in Assembly ARM
6
votes
2 answers

Can I POP a value from the stack, but put it nowhere in NASM Assembly?

NASM Assembly, Ubuntu, 32-bit program. Normally, when popping a value from the stack, I'll do POP somewhere Into a register or a variable. But sometimes, I simply don't want to put it anywhere - I just want to get rid of the next element in the…
Saturn
  • 17,888
  • 49
  • 145
  • 271
5
votes
2 answers

Any assumption in the RISC-V ISA preventing the stack from growing up rather than down?

I was wondering whether anything in the ISA would make a stack growing up (a push increases sp, a pop decreases it) less performant or otherwise inadvisable? I am aware that this is not how present day tooling works, including Linux and GCC ports,…
Schwanritter
  • 137
  • 8
5
votes
2 answers

Why does C not decrement the stack pointer if a variable leaves the scope?

#include void main() { { int x; printf("%p\n", &x); } { int x; printf("%p\n", &x); } } I would think running this that it would output the same thing twice. When it declares the first…
amihart
  • 171
  • 1
  • 6
5
votes
2 answers

Who defines the stack pointer address

In terms of microcontrollers and embedded systems with C startup code, one of the functions of the C startup code is to initialize the stack pointer. Is this initial stack pointer address and C startup code generally defined and provided by the…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
5
votes
1 answer

Which Stack Is Used Coming Out of Reset In ARM Cortex-M, MSP or PSP?

I've been reading various sections in the ARM Information Center to try and find my answer, however I came to a point where the documentation confused me so I'm hoping someone here can help. I understand that there are two stacks in Cortex-M…
cDreamer
  • 385
  • 5
  • 18
5
votes
1 answer

Operand type mismatch when using "jmp *%esp"

I have this snippet in my code void jmp_esp() { __asm__("jmp *%esp"); } when compiling with gcc gcc aslr.c -o aslr -ggdb -fno-stack-protector -z execstack i get this error. aslr.c: Assembler messages: aslr.c:6: Error: operand type mismatch…
hannibal
  • 266
  • 4
  • 15
4
votes
2 answers

Why does "push ebp" change the value of ebp?

I have a simple code: void func() { func2(); } I omit the implementation of func2 and main since they are irrelevant. Then I used windbg to trace the assembly, following is the output of assembly code when executing "func2()": eax=cccccccc…
wangshuaijie
  • 1,821
  • 3
  • 21
  • 37
4
votes
3 answers

What does "aligning the stack" mean in assembly?

How does stack alignment work in ASMx64? When do you need to align the stack before a function call and how much do you need to subtract? I didn't understand what was the purpose of it. I know there are other posts about this but it wasn't clear…
4
votes
0 answers

Is reading stack at bootloader start safe?

Background I am trying to make a bootloader that would work for two architectures: x86 and PDP-11. The main OS is written for a PDP-11-compatible machine, but booting from x86 should work too, starting an emulator. AFAIK, x86 loads the first disk…
Ivanq
  • 141
  • 2
  • 11
4
votes
1 answer

Why reset the stack pointer register in FreeBSD?

I'm trying to get a grip on assembler in FreeBSD. In the handbook's code example for a UNIX filter the register esp is reset after each system call. The code in question is: %include 'system.inc' section .data hex db '0123456789ABCDEF' buffer …
4
votes
2 answers

What are the x86 instructions that affect ESP as a side effect?

I know that call and ret will modify the value of esp and that push and pop have a number of variants, but are there other instructions that will affect the stack pointer ?
iodbh
  • 688
  • 1
  • 7
  • 14
1
2
3
10 11