2

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 this code: (code compiled with 64 bit GNU compiler, AT&T syntax)

pushq   %rbp

movq    %rsp, %rbp

subq    $48, %rsp

call    __main
movl    $0, -4(%rbp)
movl    $4, -8(%rbp)
movl    -8(%rbp), %edx
movl    -4(%rbp), %eax
addl    %edx, %eax
movl    %eax, -12(%rbp)
movl    -4(%rbp), %edx
movl    -12(%rbp), %eax
addl    %eax, %edx
movl    -8(%rbp), %eax
addl    %edx, %eax
movl    %eax, -16(%rbp)
addq    $48, %rsp
popq    %rbp
ret

in this small program I can imagine doing all of that without the need to decrement esp by 48. I can just use the base pointer to move values from and on to the stack, and just have esp point to the same position ready to pop ebp and return. can someone clarify why is it necessary to leave "room" for local variables. thanks!! I apologize if this seems like a foolish question

4 Answers 4

2

Do you want every function that your function calls to have to have intimate knowledge of where you've placed your variables on the stack?

Lots of functions call other functions - decrementing the stack pointer is the way for your function to say "I'm using this bit of the stack"


"leaf" methods - methods that never call other functions - could indeed be written in the style that you suggested - because no other piece of code is ever going to make its own use of the stack.

2
  • Ok, so the whole point of decrementing the stack pointer, is to save us the trouble of doing something like: mov "return address", -52($ebp) when calling another function and always keep decrementing ebp every time we insert a local variable for the new function, and to alsp keep track of the beginig and end of each frame (through pushing ebp and decrementing esp), is that right?
    – GamefanA
    Commented Oct 1, 2014 at 7:22
  • call __main looks like MinGW, which implies that you don't have a red-zone below RSP that's safe from asynchronous clobbering. That's a feature of the x86-64 System V ABI (Linux, MacOS, other non-Windows). Commented Oct 17, 2020 at 16:01
2

If an interrupt happens, anything at addresses less than RSP is fair game - the OS will wipe it (that is, replace with its own data) without asking you. Interrupts happen all the time. Ergo - you have to keep all the stuff that you care for at RSP or below.

Also, calling other functions pushes the return address. Unless there's blank space above RSP, that'll overwrite your data.

1
  • Interrupts don't use the user-space stack. Signal handlers can, or Windows SEH exception handlers, and this is probably MinGW code. So there's no red-zone like there would be on Linux what would make it safe to keep locals below RSP. Commented Oct 17, 2020 at 16:03
0

Subtracting a constant from the stack pointer is how space is allocated for local variables. The compiler or assembler programmer will know where those varibles are, as either negative offsets from rbp, or positive (or zero) offsets from rsp.

The example you show is a bit strange, because it performs several adds, storing the results in the local variable data on the stack, then adds a constant back to the stack pointer, effectively freeing all of those local variables (it does leave a sum in eax). Also looking at the example, the "lowest" address used is 4 bytes of data at rbp-16, so a subtract of 20 from esp would have been sufficient (in this case).

This can get more complex if something like _alloca() is used, since it allocates a variable amount of memory from the stack.

Also, usage of rbp is optional. It's being used as a "frame pointer" in the example code, but some compilers have an option to disable frame pointers, in which case the compiler only uses rsp (or esp if in 32 bit mode) to keep track of local variables, freeing up rbp (or ebp) to be used as a generic register.

-3

It is used in remembering functions, It is very much helpful in recursive function calling.

1
  • 4
    This couldn't be any more vague or incomplete. Commented Oct 1, 2014 at 7:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.