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

Why does printf make a segmentation fault if there is no \n?

To learn multithreading, I use code given by the teacher, that works for my classmates, but not for me. The error happens here: void fonction2(){ int compteur1 = 1; while(1){ fflush(stdout); printf("je suis la…
0
votes
1 answer

ESP calling convention issues C++

For homework I'm supposed to read a file and the sort the strings. For this I'm using a selection sort and it works. Now, right after it makes the call to selSort function it crashes. I have ran out of ideas to solve this issue can anyone give me a…
0
votes
0 answers

Crash on MARGIN = 0 ---> Stack overflow?

This is a part of the VxWorks thread crash dump Could someone help me to decode the following two cases of "taskShow" GOOD Healthy one STACK BASE END SP SIZE HIGH MARGIN ----- ----------------…
IceDefcon
  • 35
  • 9
0
votes
1 answer

why stack pointer is initialized to the maximum value?

why stack pointer is initialized to the maximum value? I only knows that It is the tiny register which stores the last program request’s address in a stack. It is the particular kind of buffer that stores the information in the order of top-down.…
0
votes
1 answer

How to create a dumpRegisters PROC in assembly?

I'm creating a program that shows the registers called dumpRegisters. The registers need to match what is shown in the Registers window in the debugger (using Visual Studio). The registers in the window of the debugger are: EAX, EBX, ECX, EDX, ESI,…
user18102815
0
votes
1 answer

Stack pointer set correctly but call not working

I'm using TI Code Composer Studio for the MSP430F1232. Starting from a template in assembler, a call to a routine doesn't work but when I put the routine directly in the code,it works. Why does the call won't work? ;generated code, the watchdog is…
user3635160
0
votes
1 answer

8051 Assembly Language - Stack pointer initialization

Considering the 8051 micro controller RAM organization, I would like to set my stack pointer to address 30h. I would like to know if I have to do it only once in the beginning of my program, or is it necessary to set it before every subroutine call…
OcR19
  • 23
  • 4
0
votes
0 answers

Write an MIPS program that reads 5 integers and displays them in reversed order

I have already done that, but I don't seem to understand the $sp - Stack pointer fully. As it says that the Stack Pointer $ sp should always point to the last occupied memory cell of the stack. (when I run it the $sp stays the same until the…
S. Jilani
  • 1
  • 1
0
votes
0 answers

Can you push/pop a 32-bit register in a 64-bit program?

It seems that, while a 32-bit program can push/pop either 2 or 4 bytes to the stack, a 64-bit program has no choice but to push/pop 8 bytes at a time. Is there really no way to push/pop a 4-byte value in 64-bit mode? If not, is there a reason why…
user1636349
  • 458
  • 1
  • 4
  • 21
0
votes
0 answers

What is the difference between temporary register($t) and stack pointer($sp)

Hi I am studying computer architecture. We learned that we usually use temporary registser($t) to temporarily store values ​​in registers. However, I saw a data that uses the stack pointer ($sp) to temporarily store the value of the saved register…
user13242010
0
votes
1 answer

Why is there the need to use a precise return address for shellcode execution?

I'm trying to understand why in order to successfully execute my shellcode payload, I need to use a specific return address inside the stack. If I use a different address that is still inside the stack, I either get a SIGSEGV segmentation fault or a…
AkechiShiro
  • 67
  • 1
  • 10
0
votes
1 answer

Why does printf cause segmentation fault?

I have an assembly program that doesn't work: .data .LC0: .string "%f%f" b: .long 1085066445 a: .long 1078774989 .text .globl main main: pxor %xmm0, %xmm0 pxor %xmm1, %xmm1 movl $.LC0, %edi movl $2, %eax # %eax…
dVNE
  • 161
  • 9
0
votes
2 answers

Is using placement new with variable on the stack is correct?

Let's take a look to this code: A a(123); new(&a) A(124); Test says that in this case when program is shutting down destructor ~A() will called once. So if in A we has some pointers as fields we will get memory leak. A a(123); a.~A(); new(&a)…
dasfex
  • 1,148
  • 2
  • 12
  • 30
0
votes
0 answers

ESP pointer in relation to when the return address is popped off the stack

I am taking a class right now and as part of the research project there is a question on how the ESP/RSP pointer is used in relation to popping the return address on a stack frame into the register. Now looking up the leave instruction I found the…
0
votes
0 answers

c++ stack alignment for one specific call

I have a global function func() implemented in assembly, declared as an external in a C++ source file: extern "C" void func(char*); func() requires the stack to be aligned at 32 bytes. Now, I could: Force all my calls to be aligned at 32 bytes…
Reimundo Heluani
  • 918
  • 9
  • 18