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

Do I have to clean the stack when I call the C function "exit" from assembly?

I was asked to create a small program in assembly while using C functions. While doing so, I was wondering about something specific. I know that when working with assembly, wherever I want to call a C function, I must push it's arguments to the…
PeNpeL
  • 103
  • 1
  • 7
4
votes
3 answers

Why "Procedure Call Standard for the ARM Architecture" (AAPCS) requires SP to be 8-byte aligned?

Since this is a recurring topic, I'm putting up a question about it. According to AAPCS: 5.2.1.1 Universal stack constraints SP mod 4 = 0. The stack must at all times be aligned to a word boundary 5.2.1.2 Stack constraints at a public…
auselen
  • 27,577
  • 7
  • 73
  • 114
4
votes
2 answers

Can I use rsp as a general purpose register?

I was told if I use rsp as a general purpose register the operating system may dump registers to where it points in the case of an interrupt, causing problematic behavior. Is this true, and if not hence, if I don't need a stack, could I use rsp as a…
kvanbere
  • 3,289
  • 3
  • 27
  • 52
4
votes
1 answer

Why is 0x20 subtracted from the stack pointer in the prologue of this function's code?

void main(){ int c; c = function(1, 2); } int function(int a, int b){ char buf[10]; a = a+b; return a; } Assembly code: main: 08048394: push %ebp 08048395: mov %esp,%ebp 08048397: and…
user1831833
  • 49
  • 1
  • 3
3
votes
2 answers

Is this inline-asm approach for stack switching ok?

For some functions, I need to switch the stack so that the original stack remains unmodified. For that purpose, I have written two macros as shown below. #define SAVE_STACK() __asm__ __volatile__ ( "mov %%rsp, %0; mov %1, %%rsp" : \ "=m"…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
3
votes
1 answer

why rsp register starts at 0x7FFFFFFFDFD0

im learning x86 assembly, using the code below for testing, i see in gdb console that the rsp register which points at the top of the stack starts at 0x7FFFFFFFDFD0, if i understand correctly, in the code i haven't used push or pop which modifies…
Isaí
  • 49
  • 3
3
votes
1 answer

What Is The Initial Value of Stack Pointer of 8086 (x86-16) CPU?

What is the initial value of SS, SP, BP? Are they null (0x0) or for example does SP immediately set to 0xFFFE? When we write an assembly code without using pop and push statements does stack pointer set to a value?
Hüseyin Aydın
  • 486
  • 5
  • 9
3
votes
1 answer

Where does the stack pointer start for RISC-V? and where does the stack pointer point at?

For RISC-V, does the stack pointer point at the last data that was pushed on the stack, or the next free address location for the stack? When the stack pointer is being initialized at the very beginning of the program (e.g. crt.S) (i.e. stack is…
bna3p45vnh
  • 91
  • 2
  • 8
3
votes
1 answer

How should I get gcc to realign the stack pointer to a 16-byte boundary on the way in to a function?

I'm trying to get an existing JIT working on Windows x86_64 using mingw64. I'm getting segfaults when the JIT calls back into precompiled code, and that code calls Windows APIs, because aligned move instructions such as movaps within the Windows API…
rakslice
  • 8,742
  • 4
  • 53
  • 57
3
votes
1 answer

Why are we adding 0 to a double void pointer here?

For context, this is code called from a bootloader that is supposed to boot into the main application. This snippet is from a function with an argument uintptr_t address that specifies the address of where the main application has been written to. I…
Capn Jack
  • 1,201
  • 11
  • 28
3
votes
3 answers

If esp points to the top of the stack, where does ebp point to?

I am having some trouble understanding how the esp and ebp registers are used. Why do we do: pushl %ebp movl %esp, %ebp at the start of every function? What is ebp holding when it is pushed for the first time?
user9293205
3
votes
1 answer

Interrupt / Stack Pointers / PIC Microcontroller

I am currently working with the PIC16F1829 micro controller. However, I am stuck on interrupt routine appropriate execution method. I want the interrupt routine to exit out of the infinite loop in all of the functions (LED animations), that are…
RytisBe
  • 69
  • 8
3
votes
1 answer

Where exactly(!) does the stack pointer point to on x86-CPUs? TO the top element or RIGHT BEHIND it?

In some tutorials it is said that the stack pointer points TO the top element of the stack: +-------------+ | stack | +-------------+ | top element | <-- esp +-------------+ In others it is said that it points RIGHT BEHIND it, so to the…
3
votes
1 answer

BUS Error while storing halfword value into stack memory in armv8 architecture?

I have two registers w1 and w2 that I want to store on stack. I want to store the complete word w1 and half part of w2 into the stack. Here is my implementation: STR w1, [sp, #-8]! STRH w2, [sp, #-8]! On compilation the first instruction runs fine…
3
votes
2 answers

Prologue of "add esp, 0FFFFFFF8h"

I'm a tad bit rusty on my MASM, so I don't really recall what to do here (if anything needs to be done at all). I have a MASM (X86) routine that looks as follows. It has two local variables taking up 5 bytes total: MSC_ASM_GenerateBlock PROC…
jww
  • 97,681
  • 90
  • 411
  • 885
1 2
3
10 11