Reserve bytes in stack: x86 Assembly (64 bit)

2.3k views Asked by At
pushq   %rbp
movq    %rsp, %rbp
subq    $32, %rsp

I have big question regarding explanation of "$32" in third instruction. The information from search and blogs specifies that in above third instruction we are reserving certain "bytes" of space for stack. From doc, %rsp is 64 bit register and %esp is 32 bit.

Is "$32" means 32 bytes ?($number means constant ?) If yes then how are we allocating 32 bytes on 64 bit register? Above instruction were generated from "otool". I am using macbook pro.

I am just trying to learn a small things about assembly.

Thanks.

2

There are 2 answers

4
Fifoernik On BEST ANSWER

Is "$32" means 32 bytes ?($number means constant ?) If yes then how are we allocating 32 bytes on 64 bit register?

These 32 bytes are not allocated on 64 bit register. They are allocated on the stack. By lowering the stackpointer (which is in %rsp) the address range from %rsp to %rsp+31 is at our disposal for storing data.

2
Rich Remer On

In 64-bit code, 128 bytes are reserved for you already, so you don't have to mess with the stack pointer in leaf functions, which consequently means you probably don't have to push/pop %rbp. Just use %rsp-128 to %rsp-1 for your data.

This reserved area is called the :

http://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64/