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
280
votes
6 answers

What is exactly the base pointer and stack pointer? To what do they point?

Using this example coming from wikipedia, in which DrawSquare() calls DrawLine(), (Note that this diagram has high addresses at the bottom and low addresses at the top.) Could anyone explain me what ebp and esp are in this context? From what I see,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
144
votes
5 answers

What is the function of the push / pop instructions used on registers in x86 assembly?

When reading about assembler I often come across people writing that they push a certain register of the processor and pop it again later to restore it's previous state. How can you push a register? Where is it pushed on? Why is this needed? Does…
Ars emble
  • 1,459
  • 2
  • 10
  • 4
140
votes
2 answers

What is the purpose of the RBP register in x86_64 assembler?

So I'm trying to learn a little bit of assembly, because I need it for Computer Architecture class. I wrote a few programs, like printing the Fibonacci sequence. I recognized that whenever I write a function I use those 3 lines (as I learned from…
user6827707
35
votes
2 answers

Base pointer and stack pointer

Given this piece of code: swap: push ebp ; back up the base pointer, mov ebp, esp ; push the context of the registers on the stack push eax push ebx push ecx …
yhcowboy
  • 585
  • 1
  • 5
  • 13
33
votes
2 answers

movq (%rsp), %rsp assembly stack pointer load?

I was reading some code and was not sure what this line does: movq (%rsp), %rsp
jamesatha
  • 7,280
  • 14
  • 37
  • 54
31
votes
10 answers

Print out value of stack pointer

How can I print out the current value at the stack pointer in C in Linux (Debian and Ubuntu)? I tried google but found no results.
Juicy
  • 11,840
  • 35
  • 123
  • 212
21
votes
4 answers

x86 where stack pointer points?

For example if I pushed ax is [SP] points to my value of ax or the word after ax? Also is it differs from real mode to protected mode? I ask this because the Art of assembly book illustrates and explains as the sp points to last pushed data, and on…
user1180619
  • 338
  • 1
  • 4
  • 11
15
votes
5 answers

Is it valid to write below ESP?

For a 32-bit windows application is it valid to use stack memory below ESP for temporary swap space without explicitly decrementing ESP? Consider a function that returns a floating point value in ST(0). If our value is currently in EAX we would,…
J...
  • 30,968
  • 6
  • 66
  • 143
15
votes
5 answers

Function call jumps to the wrong function

I am compiling a c++ static library in vs2008, and in the solution i also have a startup project that uses the lib, and that works fine. But when using the lib in another solution i get an run-time check failure. "The value of ESP was not properly…
Archon
  • 153
  • 1
  • 6
15
votes
2 answers

how does push and pop work in assembly

I'm getting confused on what does pop actually do in assembly. Does pop move the value PUSHed onto the stack last (meaning it doesn't apply if we MOV a value after the the last element PUSHed) or does it just pop whatever value that's last on the…
GamefanA
  • 1,555
  • 2
  • 16
  • 23
14
votes
2 answers

What is the difference between Stack Pointer and Program Counter?

As we always know the procedure of executing task by a microprocessor is just executing binary instructions from memory one by one and there is a program counter which holds the address of the next instruction. So this is how processor executes it's…
Naasif
  • 495
  • 2
  • 6
  • 13
13
votes
1 answer

x86 assembly: Pop a value without storing it

In x86 assembly, is it possible to remove a value from the stack without storing it? Something along the lines of pop word null? I could obviously use add esp,4, but maybe there's a nice and clean cisc mnemonic i'm missing?
NeoTheThird
  • 360
  • 3
  • 16
12
votes
3 answers

pop or add esp, 4 ? What is the difference?

I saw this question, but I didn't find my answer in it.. So, why would I prefer to use add esp, 4 or add esp, 8 instead of using pop one or two times? Is there any difference (performance, safety, etc.) at all or it's a matter of personal choice?
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
12
votes
3 answers

Does the ret instruction add 4 to esp register?

Does the ret instruction cause "esp" register to be increased by 4?
remainn
  • 1,125
  • 3
  • 9
  • 14
12
votes
2 answers

What is an assembly-level representation of pushl/popl %esp?

I'm trying to understand the behavior of pushing and popping the stack pointer register. In AT&T: pushl %esp and popl %esp Note that they store the computed value back into %esp. I'm considering these instructions independently, not in sequence.…
amorimluc
  • 1,661
  • 5
  • 22
  • 32
1
2 3
10 11