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
0
votes
1 answer

vector, sfml and "the value of esp was not properly saved across the function call" error

I have a struct "Layer" and class "LayerHandler". Layer consists only a texture, sprite and two constructors - one default and one with a reference parameter. LayerHandler class is a class that handles the drawing of all the layers we have. I add…
mentor93
  • 309
  • 1
  • 5
  • 16
0
votes
2 answers

Balancing the stack

If I setup the stack like this push rbp; mov rbp, rsp; sub rsp, 64; do I need to mov rsp, rbp; pop rbp; ret 64; or only mov rsp, rbp; pop rbp; ret; ?
alexandernst
  • 14,352
  • 22
  • 97
  • 197
-1
votes
3 answers

stack pointer changes through function call

I'm trying to understand more about stack pointer's behavior through function calls, and I'm not sure I understand what happens when we call and return from a function. Assuming I have this main program: int main() { demo(); return 0; } And…
IntToThe
  • 59
  • 6
-1
votes
1 answer

What register is used instead of FP (Frame Pointer) in 8086 assembly?

What register is used in 8086 assembly instead of FP? I think it is SP or ESP. Am I right?
John
  • 67
  • 6
-1
votes
1 answer

Stack/Frame pointer as external variable

I was writing some logging logic and wanted to make some indentations. The easiest way to understand whether any function call was present or if some function has finished is to look at the current address of the stack/frame. Let's suppose that…
-2
votes
1 answer

What is the initial value of `ESP`?

Under Visual Studio masm: mov ecx,270 l1: pop eax loop l1 push eax The point of this code is to find out if there is and what is the initial value of ESP. I try to pop immediately after the program starts, and experiment that after how…
Zackham
  • 45
  • 5
1 2 3
10
11