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
1
vote
0 answers

segmentation fault after linking c++ file with asm file

still me. I am following the sample code given in a book but I got a segmentation fault error when linking extern asm file with cpp file Could someone tell me what the issue is: global _integer_add ; section .text i ran the code with and without…
1
vote
1 answer

Why is value of RSP higher than RBP as displayed in GDB?

I am inspecting a process (which has no bugs actually) with gdb. However I noticed, when doing info registers, that RSP is higher than RBP, which is not consistent with the fact that the stack grows downwards. Is this perhaps some optimization by…
Aaa Bbb
  • 627
  • 4
  • 12
1
vote
1 answer

Does the prologue of a function can write outside of its frame?

I'm currently trying to analyse the assembly of an old video game from N64. In order to do so, I'm using some N64 debugger to read and understand the underlying MIPS code. In one of the calls I'm looking at, the prologue is defined as follows: ADDIR…
1
vote
1 answer

In x86 32 bit protected mode, can we still use SP rather than ESP register when use PUSH/POP instructions?

I don't know when use SP register and when use ESP register, besides, I want to know when use ESP register, does it always decrease 4 and can't decrease 2?
1
vote
1 answer

Stack Pointer is decremented to allocate space for local variables when a function is called

I read somewhere that Stack Pointer is decremented to allocate space for local variables when a function is called. I don't understand how it is true because according to me it should be incremented. Can somebody please explain?
Anshul Gupta
  • 265
  • 2
  • 12
1
vote
1 answer

Assuring stack pointer alignment using bitwise operators

Assume I want to reserve 8 bytes on the stack and I also want to make sure current stack pointer is 8 byte aligned. I have seen some codes that assure current sp is 8 bye aligned using this logic: sp = sp & -8; They AND it with the amount they are…
Dan
  • 577
  • 1
  • 3
  • 9
1
vote
1 answer

Run-Time Check Failure #0 in embedded asm code

I'm a bit new to assembler, but I'm trying to lookup the parameters from a C++ method in the esp stack, using embedded assembler code. So far I haven't even been able to copy the esp pointer to ebp so I can get a hold on the stack (in case it…
1
vote
1 answer

Is that possible that using stack pointer to locate/index local variables on stack with llvm?

I'm trying to modify llvm X86 backend to use rsp to locate/index local variables rather than rbp. The problem is that the offset between local variables and rsp is not fixed, so I have to calculate it by myself. It's easy to handle it if rsp's…
ys z
  • 71
  • 1
  • 3
1
vote
4 answers

Deallocating locally defined variables in C

Assume we have the following piece of code: void foo() { char buffer[100]; } Is there a (preferably portable) way in C to deallocate buffer from the runtime stack (akin to add esp, 100 in assembly), before foo() returns?
1
vote
1 answer

Is the difference between programming model wrt Program Counter and Stack Pointer in case of Assembly?

Processor model I ● Registers  PC – Program Counter  Single data register (accumulator) without name  We will use symbol A to describe operations on this register ● Stack with an unspecified implementation ● Data specified…
user366312
  • 16,949
  • 65
  • 235
  • 452
1
vote
0 answers

assembly: I don't understand why the stackpointer seems(!) to reserve insufficient memory here

I'm a beginner in assembly, so the answer to my question probably is totaly obvious for most of you, but not for me. Please don't blame. On a 64-bit-system this C-code: 1| int main () 2| { 3| char ary[230]; 4| ary[0] = 2; 5| return 0; 6|…
a kind person
  • 329
  • 1
  • 6
  • 17
1
vote
1 answer

in linux where is user mode stack stored?

I know that kernel mode stack is stored with thread_info structure of task_struct structure. But where is user mode stack stored. I guess, it will be stored in process address space as a memory region, because during page fault kernel checks if the…
user1434287
  • 381
  • 3
  • 16
1
vote
1 answer

'dds esp' on Windbg

I am not sure if I am understanding the raw output of dds esp or its 64-bit counterpart dqs rsp properly. When I see a list of entries in the stack, I tend to assume that wherever I see return addresses, those are calls made by code that have not…
ForeverLearning
  • 6,352
  • 3
  • 27
  • 33
1
vote
1 answer

$sp register does not change at the beginning of the function

Linked to: How to get a call stack backtrace?(GCC,MIPS,no frame pointer) I am reproducing the call stack(more details at the link above) by iterating the function using the assembly code and user stack. I have to find the previous $sp for each…
David
  • 733
  • 2
  • 11
  • 30
1
vote
1 answer

Why does %rbp point to nothing?

It is known that %rsp points to the top of the stack frame and %rbp points to the base of the stack frame. Then I can't understand why %rbp is 0x0 in this piece of code: (gdb) x/4xg $rsp 0x7fffffffe170: 0x00000000004000dc …
alacerda
  • 155
  • 1
  • 8