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
2 answers

How to manipulate MIPS code and use stack pointers?

So, I recently made a code to count the number of binary 1's in C-code and in MIPS code. I did so in C by using a remainder value and increment a count_one variable. In MIPS, I did the same program but I shifted the bytes of the number until it…
CodeFreak
  • 90
  • 1
  • 2
  • 15
1
vote
0 answers

How esp register move?

I have a piece of code like this: #include main() { xxx(1234); } int xxx(int b) { int c; c=b; return c; } I use gdb to show what value of ESP register and how it work by make breakpoint at xxx(1234); and c=b; line. Run…
user173717
  • 73
  • 1
  • 10
1
vote
2 answers

Doesn't the frame pointer make the stack pointer redundant?

As far as I understand it, the stack pointer points to the "free" memory on the stack, and "pushing" data on the stack writes to the location pointed by the stack pointer and increments/decrements it. But isn't it possible to use offsets from the…
user3735658
1
vote
1 answer

Incrementing %esp and CDECL

I've been reading up on the x86 stack and the CDECL convention and read something that confused me. Among the caller's responsibilities listed were popping the parameters, using them or simply incrementing %esp to remove them. How does that last…
user8814
  • 94
  • 6
1
vote
1 answer

Reason for making esp as reference for variables on 32bit OS while rbp on 64bit OS on intel machines?

Below program is giving different output on 32bit and 64bit machine. When i checked its assembly code generated by compiler, i found that reference of variable on 32bit machine was esp while on 64bit OS it was rbp. In my opinion ebp has to be the…
1
vote
2 answers

why the compiler reserves just 0x10 bits for a int?

I have the following code: #include using namespace std; void f() { cout << "hello" << endl; } void f(int i) { cout << i << endl; } int main() { f(); f(0x123456); } I compiled it using g++, then disassembled it using…
elyashiv
  • 3,623
  • 2
  • 29
  • 52
1
vote
1 answer

Boost exceptions lead to stack pointer corruption (Run-Time Check Failure #0 ...)

In an small project i am using several boost packages (asio, property_tree, filesystem, etc.) and i had to notice, that everything works fine, until an exception gets thrown somewhere in the boost packages. It happens in all those packages, but i…
Janosch
  • 1,204
  • 1
  • 11
  • 19
1
vote
4 answers

How does the stack pointer register work

Well, how does the stack work? For example the instruction: push ax is equal to: sub sp, 4 mov sp, ax where sp is a stack pointer. Is that right? My question is - what's the point of subtracting 4 from the sp register if a moment later I change it…
user2489034
  • 239
  • 2
  • 3
  • 15
0
votes
1 answer

Frame, Stack Frame in process Stacking Unstacking

Stacking process When I talked about the stacking process I was talking about the frame that it would store the data of CPU registers like PC(Program counter) or LR but the advisor said it wasn't and didn't dig deep. But when I look up on google,…
ttd2409
  • 1
  • 1
0
votes
1 answer

Stack pointer add on loop

Learning some reverse engineering and I came across some examples of loops in x86 assembly 00401036 mov [ebp+var_4], 0 0040103D mov [ebp+var_8], 0 00401044 loc_401044: 00401044 cmp [ebp+var_4], 0 00401048 jnz …
Kenneth Cox
  • 84
  • 1
  • 6
0
votes
0 answers

Can I get %rsp with inline assembly?

This question is related to this question: How can I access arguments 7+ using inline assembly? I understand that accessing the pointers myself is non-standard, potentially unsafe, and not portable. That being said, I'd like to get the stack pointer…
Connor
  • 867
  • 7
  • 18
0
votes
0 answers

Is there a way to track the value of the stack pointer using Intel Processor Trace?

Is there a way to record the value of the stack pointer (either periodically, or when it changes) using Intel Processor Trace? I think it could be done with PTWRITE, but sadly I'm working with a chip that doesn't support this.
Edd Barrett
  • 3,425
  • 2
  • 29
  • 48
0
votes
4 answers

What is the RUST equivalent of following C code?

I have a small C code which demonstrate Runtime Stack functionality by modifying data at a stack address. #include int * fun() { int a = 10; return &a; } int * fun2() { int b = 20; return &b; } int main () { int *a =…
0
votes
2 answers

What is the benefit of having a dedicated stack pointer register?

As a more specific question for assembly - Why make ISA be aware of the existence of "stack" concept? - Stack Overflow and suggested by @xiver77, what is the benefit of having a dedicated stack pointer register and instructions for each ISA such as…
tristone
  • 95
  • 6
0
votes
1 answer

How stack memory works when pushing value to it on x86_64?

I have been writing some code in assembly and i found a bug that was overwriting others memory locations and giving to me a segmentation fault, this trouble was made using the rbp register, but the following example use other register to make easy…