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

Trouble understanding stack segment register

Segment registers are used to increase the range of addressable memory from 64K to 1M bytes. But, I have trouble understanding stack segment register (SS) because stack already has two other registers associated to it, sp and bp. Suppose, I have set…
0
votes
0 answers

Why does sturb instruction fail with a bus error?

Why does the following instruction produce a bus error on armv8? sturb wzr, [sp, #0] Bus error (core dumped) Minimal example: .text .global _start _start: sub sp, sp, #1 sturb wzr, [sp, #0] mov x0, #0 mov x8, #93 svc 0
0
votes
0 answers

How do I handle the stack pointer register when returning from an assembly function call to a C program?

My program is composed of two files: main.c and core.s and runs on a 32 bit virtual machine of lubuntu linux. Main.c takes in an integer and passes it to the assembly function void printFunc(int x). The assembly function in turn calls to a C…
0
votes
1 answer

Calculating stack memory size in C from pointers, on Linux Debian?

Inspired by this related SO answer, I am trying to obtain stack memory usage (as distinct from heap memory) on Linux Debian 9, without much success. I have created some testing code that doesn't produce expected results: size_t top_of_stack =…
Theo d'Or
  • 783
  • 1
  • 4
  • 17
0
votes
0 answers

Allocating class in the stack

I was reading this article (which is about how to allocate class object in the stack segment) and came across to this snippet. If I understand correctly he wants to get the address of our PocoClass with the header (with lock object and type…
user12722843
0
votes
2 answers

I'm confused on registers, stack pointer, and let a register point to stack pointer

Could someone help me with these problems? what is a stack pointer? does it have its own address? what does it mean to let a register point to a stack pointer? here are some examples that I don't understand. 1. STR R6, [R9, R8] so this line is…
0
votes
0 answers

Segmentation fault when using esp in different function

I am new to nasm and have written this code: section .text global _start ; just experimenting with functions do_something: mov DWORD [esp+0], 1; mov DWORD [esp+4], 1; mov DWORD [esp+0],0; mov DWORD [esp+4],0; ret ; _start: mov…
gearDev
  • 29
  • 1
  • 8
0
votes
0 answers

Why do I have padding in the stack?

I was trying to understand how the stack (segment) works and thought that it will simply allocate each element (variable, byte, whatever I want to allocate) one after another. BUT after writing following code I noticed something strange. var arr =…
user12722843
0
votes
0 answers

Why 'rbp' of a frame is not matching 'rsp' of previous frame?

#include int funcB(int *ret) { int x = 10; x += *ret;; printf("%d\n", x); return 0; } int funcA(int x, int y) { int ret = 0; ret = x + y; funcB(&ret); ret += 10; return ret; } int main(void) { int…
NeilB
  • 347
  • 2
  • 16
0
votes
0 answers

push stack pointer in MIPS

I am aware of pushing a stack pointer but I am not sure of how to push all of the elements. This is how I defined a stack pointer. addi $sp, $sp, -24 sw $ra, 0($sp) sw $s0, 4($sp) sw $s1, 8($sp) sw $s7, 12($sp) sw $s3, 16($sp) sw $s4, 20($sp)…
0
votes
1 answer

Running GCC compiled code on custom machine. cant find sp initialization in assembly

I was going through .lst while while trying to run gcc compiled code on my custom rv32I machine. I cant find initial vallues of sp Disassembly of section .text: 00010074 : register_fini(): 10074: 00000793 li a5,0 …
veeYceeY
  • 1
  • 1
0
votes
1 answer

How do i write push and pops functions in a stack? In C

Professor asked to write a stack and then define the functions of push and pop in C. Hints were of using a stack pointer and an array, but it seems i can't do it properly (I didn't understand the lesson i think). Can you help me sort it out?
0
votes
0 answers

Do I need to add the line 'mov ebp, esp' in NASM assembly function if it still works without it?

In one of my class examples, a function that takes in int parameter integer begins like: recursive_sum: push ebp mov ebp, esp ;set EBP=ESP pusha ;save all registers(probably overkill) mov ebx, [ebp+8]…
Fatcow808
  • 11
  • 2
0
votes
0 answers

Memory allocation in x86 or similar assembly

Context: I'm designing my own processor and instruction set as a learning exercise. I'm trying to understand how a low-level assembly program knows which memory addresses it can access. Even if we assume we're running without virtual memory or OS…
0
votes
0 answers

__libc_init_array using stack pointer for writes before stack pointer is set

The Problem: When I compile a C program for a bare metal RISC-V environment with GCC, the function __libc_init_array is using the stack pointer for writes to the memory before the stack pointer is set. The stack pointer is set in an assembly file…
pls_help
  • 71
  • 1
  • 2
  • 9