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

Does it matter where the ret instruction is called in a procedure in x86 assembly

I am currently learning x86 assembly. Something is not clear to me still however when using the stack for function calls. I understand that the call instruction will involve pushing the return address on the stack and then load the program counter…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
2
votes
5 answers

In C language, can I access local variable of main function in another function through stack pointer?

I need to access the value of variable a which is defined in main function without passing it as argument. main() { int a=10; func(); printf("%d\n",a); } void func(){ //i need access of variable a here. } How can i do this?
user3168064
  • 97
  • 2
  • 11
2
votes
1 answer

Making a space for local variables in assembly

So I do have to write an assembly program which would call extern C function. So I wrote simple pow function, I compiled my assembly program with this C code. Everything works. But as I saw from -S command from gcc, the compiler makes a space for…
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57
2
votes
2 answers

Understanding user vs. system stack pointers in Motorola 68k

I'm trying to understand the usage and implementation of a Stacks in assembly language (Motorola 68k). I know that the MC 68k, has 8 address registers, A7 being the special one. Since it's two stack pointers, sharing the name of "System stack…
TTEd
  • 309
  • 1
  • 10
2
votes
4 answers

Why do we need to decrement the stack pointer when calling a function

I've been studying assembly for a while now and i'm beginning to get the hang of it, however the one thing that i can't seem to understand is why do we need for decrementing the stack pointer to leave roam for the local variables, take a look at…
GamefanA
  • 1,555
  • 2
  • 16
  • 23
2
votes
4 answers

Invalid ESP when using multiple inheritance in C++ (VS2005)

I've been making a game which uses the Box2D physics engine, and I've come across some weirdness with the stack pointer (ESP) and multiple inheritance. I've managed to reproduce it in a minimal amount of code, and it seems that the order in which I…
XwipeoutX
  • 4,765
  • 4
  • 29
  • 41
2
votes
3 answers

why ESP register is discouraged to use while using PUSH or POP instructions?

I was going through the C code in which i have to figure out the number of registers used in a particular trace of program.The code was neglecting to store ESP register whenever push or pop commands were encountered .I also referred X86…
archies50
  • 103
  • 2
  • 9
1
vote
2 answers

How do I stop ESP from being corrupted in a __fastcall?

I'm attempting to write a function in assembly that sets a block of memory to a specified value, much like memset(), however, when I go to get the third argument off the stack (it uses the fastcall calling convention), the register, ECX, gets some…
user1079713
1
vote
0 answers

STM32 and SP value at startup: should the reset handler set SP manually?

I am experiencing a sporadic bug on some STM32F7s. While usually SP register takes its initial value from reset vector (stored in persistent read-only memory), sometimes it is initially set to an apparently random value. I discovered that someone…
Giuseppe Guerrini
  • 4,274
  • 17
  • 32
1
vote
0 answers

What happens in the CPU if I pop / push all the stack?

I am doing an energy profile of an ARM processor for an academic project. I managed to measure the power consumption of several assembly instructions by running them in a ~200 insts loop, e.g.: .rept 200 add r1, r2, r3 .endr However, when coming…
1
vote
1 answer

Storing and Loading $ra

I'm currently having trouble writing this recursive factorial assembly code in MIPS. I do not want to change the format of this code, as it is for a project, but I want to understand how to store or call $ra so that it will return to "branch:" in…
kaili
  • 13
  • 3
1
vote
1 answer

Why does the stack pointer and frame pointer have the same address?

I was under the impression that the frame pointer $fp is set to the first word of stack according to Computer Organization and Design MIPS 5th ed page 103. int func(int g) { int f = 9; return g+f; } mips gcc 12.2.0 generated assembly…
African_king
  • 177
  • 1
  • 8
1
vote
1 answer

How can DOS's stack pointer init put a segment just below another segment?

I am learning MS-DOS source code during that i am in difficulty with some line of code: BIOSSEG: EQU 40H BIOSLEN: EQU 2048 DOSLEN: EQU 8192 In the next page there are some lines of code, INIT: XOR BP,BP ; Set up stack just ; below I/O…
1
vote
0 answers

Why the stack pointer is special in the register file?

I have a question about the register file. As I know, the stack pointer is one of the special registers in the register file. Why it is determined specifically? I mean compiler can define any register as a stack pointer and it can use it because it…
1
vote
0 answers

Current state of Stack Pointer in 8085

In Intel 8085 microprocessor, is there any way or instruction exist for find out, to where Stack Pointer currently points to?.