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
3
votes
3 answers

Using the esp register

I was trying to understand how to use the stack with assembly and in my attempt I came across the following code in one of the questions in SO, namely: push ecx mov eax, 4 mov ebx, 1 mov ecx, result mov edx, result_len int 0x80 mov eax, 4 mov…
O.A.
  • 171
  • 1
  • 11
2
votes
1 answer

The value of ESP was not properly saved.... and C/C++ calling conventions

I am writing an application using the OpenCV libraries, the Boost libraries and a pieve of code that I have downloaded from this LINK. I have created a project under the same solution with Thunk32 and I have the following…
2
votes
1 answer

How to change processor stack?

Why doesn't this code print "test"? #include #include void foo ( void ) { printf("test\n"); } __declspec(naked) void bar ( void ) { asm { push 0x000FFFFF call malloc pop ecx push eax add…
Mike
  • 1,760
  • 2
  • 18
  • 33
2
votes
4 answers

Get the Stack Pointer in C on Mac OS X Lion

I've run into some strange behaviour when trying to obtain the current stack pointer in C (using inline ASM). The code looks like: #include class os { public: static void* current_stack_pointer(); }; void*…
Michael Barker
  • 14,153
  • 4
  • 48
  • 55
2
votes
1 answer

Why does the ESP referring to the address in a stack jumps 4h each time?

Why doesn't the Extended Stack Pointer (ESP) jump 1h in each PUSH or POP operation?
Obzajd
  • 283
  • 1
  • 2
  • 8
2
votes
0 answers

I thought 8086's stack grew down, but my memory dump routine shows it growing up?

I'm testing some memory dumping routines in 8086 Assembly based on the one that Keith of Chibiakumas created for displaying registers and showing memory. This routine is a modified version of his that shows the memory in big-endian 16-bit…
puppydrum64
  • 1,598
  • 2
  • 15
2
votes
2 answers

Rust assembly: how do I indicate that I need the value of the SP?

I'm messing around with the asm! macro on an embedded ARM (Thumb) target. I have an interrupt service routine that is designed to get the number that the svc instruction was called with: #[cortex_m_rt::exception] unsafe fn SVCall() { let mut…
laptou
  • 6,389
  • 2
  • 28
  • 59
2
votes
1 answer

Is it valid for the Stack Pointer and Frame pointer to point to the same address in ARM 64?

I am having a bit of trouble understanding how stack frames work in ARM. It is my current understanding that a stack frame is region of memory between the Stack Pointer and the Frame Pointer, and that this is the space programs store information in.…
Zoey
  • 135
  • 1
  • 1
  • 7
2
votes
1 answer

Why is MIPS stack base 0x7ffffffc rather than 0x80000000

Why is MIPS stack base 0x7ffffffc rather than 0x80000000? If I understand correctly, the stack pointer refers to the last item placed on the stack. So, if that's the case, doesn't that mean that address 0x7ffffffc never gets used because the…
Zack
  • 6,232
  • 8
  • 38
  • 68
2
votes
1 answer

Child-EBP vs Child-SP

While following up on some windbg tutorials I have noticed that some callstacks using k command are in this format, specially mine Child-SP RetAddr Call Site While other online resources like CodeProject have the k command spit…
Mohamed341
  • 43
  • 4
2
votes
0 answers

Function of ECX stack register in Assembly code dump

#include #include int main(int argc, char ** argv) { char buffer[500]; strcpy(buffer, argv[1]); return 0; } I can compiling this program using gcc -m32 -fno-stack-protector -z execstack -fno-pie -no-pie -g -o vuln…
2
votes
1 answer

Why am I getting a segmentation fault when moving the stack pointer?

Working with assembly code and wondering why I get a seg fault with the instruction subl $8, %esp? pushl %ebp movl %esp, %ebp movl 16(%ebp), %esi movl 12(%ebp), %edi movl 8(%ebp), %eax movl $0, %ebx …
2
votes
2 answers

ARM assembly. Is it safe to use r13 (stack pointer) as a general purpose register?

I'm writing an extremely optimized leaf function and to make it run faster I want to use R13 as a general purpose register. I preserve R13 by moving it to one of VFP registers before using it and before returning from function I restore it by moving…
Igor Yarmolyk
  • 67
  • 2
  • 10
2
votes
2 answers

convert C code to MIPS assembly - combination function using recursion

I have a problem with conversing C code to MIPS assembly code of combination function (nCr). nCr = (n-1Cr-1) + (n-1Cr) and when I put int 5 for n and 3 for r (digit data), the result has to be 10. I want to use the recursion and stack pointer, but I…
Yoom
  • 23
  • 2
2
votes
1 answer

Stack Pointer points to reserved Memory

I am debugging a code for a cryptographic implementation on a Tricore TC275 from Infineon (reference assembly language). PMI_PSPR (wx!p): org = 0xC0000000, len = 24K /*Scratch-Pad RAM (PSPR)*/ DMI_DSPR (w!xp): org = 0xD0000000, len = 112K …
jonnyx
  • 345
  • 2
  • 16