In x86-64 assembly, I see something like this for my main function…
push %rbp
mov %rsp, %rbp
mov %edi,-0x14(%rbp)
mov %rsi,-0x20(%rbp)
movl $0x0,-0x4(%rbp)
movl $0x0,-0x8(%rbp)
Notice that the first thing it does it throw the base pointer on the stack. Then, it moves the old top of the stack (rsp) into the base ptr register.
Here’s my question, the last two lines are initializing local variables to be 0. They are at offset -4 and -8 from the base ptr. BUT…if the old base pointer is at offset 0 from the new base ptr, how can that be? The old base pointer should be 8 bytes long since this is a 64 bit machine. Therefore, the local variables shouldn’t start until -0x8(rbp).
The old base pointer is at
0(%rbp). It occupies bytes0(%rbp)through7(%rbp). The byte at-4(%rbp)does not overlap the old base pointer. (Neither do the other bytes-3(%rbp),-2(%rbp), and-1(%rbp).)