Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserve bytes in stack: x86 Assembly (64 bit)

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.

like image 838
padam thapa Avatar asked Sep 27 '22 14:09

padam thapa


2 Answers

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.

like image 188
Fifoernik Avatar answered Oct 07 '22 18:10

Fifoernik


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 red-zone:

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

like image 31
Rich Remer Avatar answered Oct 07 '22 19:10

Rich Remer