acomplex
acomplex

Reputation: 51

GNU Assembler - why doesn't -4(%rbp) override frame pointer on stack?

Consider the following disassembly code:

_sum:
0000000100000f60    pushq   %rbp
0000000100000f61    movq    %rsp, %rbp
0000000100000f64    movl    %edi, -0x4(%rbp)
0000000100000f67    movl    %esi, -0x8(%rbp)

As far as I know, %rbp is a 64 bit register so it means that pushq %rpb allocates 8 bytes on stack. If it does so, why is movl %edi, -0x4(%rbp) valid? I believe it overrides 4 bytes of already saved %rbp which has to be used to return from procedure later on. %edi and %esi are arguments passed to sum function.

Upvotes: 0

Views: 237

Answers (1)

Michael
Michael

Reputation: 58457

The stack grows downward. What pushq does is:

RSP  ← RSP – 8;
Memory[SS:RSP]  ← SRC; (* push quadword *)

So the last value you pushed (i.e. the old value of %rbp) is at (%rsp), and anything at negtive offsets from %rsp is "free space".

Upvotes: 2

Related Questions