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

How to debug segmantation fault happening on 'stp' instruction in arm binary?

My application randomly and rarely crashes with segmentation fault signal. When coredump is opened in GDB following can be seen: arm instruction leading to crash is: 0x7f8ea08130 fd 7b b7 a9 stp x29, x30, [sp,#-144]! When code of crashed frame is…
senx
  • 630
  • 9
  • 18
0
votes
0 answers

Stack pointer in Assembly

I have a C exercise call a function from assembly. "Calculate perimeter of a triangle". int a = 3, b = 4, c =5; int Calculate_triangle(int a, int b, int c); I have a code of Assembly here PUSH HL PUSH AX MOVW AX, [HL] ADDW AX, [HL+8] ADDW AX,…
Quang Minh Lê
  • 169
  • 1
  • 2
  • 13
0
votes
1 answer

Stack Pointer (SP) adjustment at RST 5.5 interruption

In a microprocessor-8085 we run the command CALL 3000H . The program counter (PC) is equal to (PC)=2000H and the stack pointer is equal to (SP)=4000H. In the middle of the execution of the command we have an RST 5.5 interruption. I want to define…
MJ13
  • 195
  • 6
0
votes
1 answer

Stack Pointer in Assembly. ESP points always empty space or not?

Initially, ESP refers to the empty space of memory. When I push something such as push 1, then it will refers to the memory location of 1, or next empty space? here is the example push ebp mov ebp, esp /* --------- (empty space) <- esp,…
monstereo
  • 830
  • 11
  • 31
0
votes
0 answers

Why ASLR generates SIGSEV if the stack pointer is reassigned

I am writing a char device that takes as input with ioctl a function pointer and a buffer pointer. I want to modify the user machine context so that back in user mode, that function is executed with a new stack pointed by that buffer…
Angelo
  • 334
  • 4
  • 14
0
votes
0 answers

Modify the user stack pointer in kernel mode

I am writing a char device that takes as input with ioctl a function pointer and a buffer pointer. I want to modify the user machine context so that back in user mode, that function is executed with a new stack pointed by that buffer pointer. What…
Angelo
  • 334
  • 4
  • 14
0
votes
1 answer

Long division in ASM x86

I am writing an ASM program that divides two numbers and calculates 20 decimal places. My strategy was to calculate the next digits with a long division sort of process and push them to the stack. Then get the stack pointer, subtract 20 digits * 8…
Matt
  • 556
  • 8
  • 31
0
votes
0 answers

Mips Stack Pointer can't reach next stack value

i'm trying to do a program in mars that , using $sp, can jump to the next address of the stack and read the value: fine_while: add $a0, $S,$a2 li $v0, 1 syscall move $t5, $sp#indirizzo iniziale move $sp, $t6 #indiirzzi di -51 subi $sp, $sp, 4…
0
votes
3 answers

Compiling an Assembly Program using avr

why do we need to initialize stack pointer in the begnning of the program of AVR assembly programming
0
votes
1 answer

Why is Saved Frame Pointer present in a stack frame?

I read that the SFP is used to restore EBP to its previous value. Why does EBP needs to return to it's initial value?
zahlen
  • 73
  • 2
  • 11
0
votes
2 answers

Extending an ArrayList - fragile-base class

Examining the best practices in Java, we find avoiding inheritance is a good one. One of the reasons maybe illustrated in the following problem: Here we have a sub-class "Stack" extends "ArrayList" class Stack extends ArrayList { private int…
Profess Physics
  • 317
  • 4
  • 11
0
votes
1 answer

Stack pointer moved back but values not loaded to register file?

What happens when the stack pointer is moved back to its original position but values that were saved in the stack are not loaded back from the memory into the register file? ie values are still present in stack memory when the stack pointer is…
0
votes
1 answer

Where is The Value of the Current Stack Pointer Register Stored Before Context Switching In POSIX C Threads

If I were to use pthreads in POSIX environments, and a context switch is about to happen, the current value of the esp register has to be stored somewhere so I can retrieve it when I context switch back to this thread, as the esp register's value…
falhumai96
  • 327
  • 2
  • 17
0
votes
0 answers

Get the Current Value of the Stack Pointer of a POSIX Thread in C

I would like to know if there exists a portable way to retrieve the current stack pointer value in C of a specific POSIX thread, pointed by its thread ID? The only way to get an approximate stack pointer value is to kill an interrupt to a specific…
falhumai96
  • 327
  • 2
  • 17
0
votes
0 answers

C call stack uses "ebp" to visit variables, then "esp" seems redundant?

C function uses call stack(stack frame) to push/pop registers before/after function call. If ebp is the frame pointer that's used to visit all variables on stack, then seems esp is redundant? Then why in prolog/epilog of a function call, we operate…
Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
1 2 3
10
11