GCC with x64 sta

can anybody explain to me why the followig lines of code int main (int argc, char *argv[])

{
int i=17;
int output=0

.....

}

get translated to the following x64 assembly code:

pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl %edi, -20(%rbp)
movq %rsi,-32(%rbp)
movl $17, -8(%rbp)
movl $0, -4(%rbp)
...

i don;t undertstand why SP got reduced by 32. the registers %edi, %rsi seem to correspond to argc, and *argv[] which are not used in the main code. i thought the space between [%esp, %rbp] is allocated to the local variables only and not to the function args, hence the size should be only 8 in the code above, ie the rsp should be decremented by 8 and not 32. any explanation , please?


Knowing the ... part would have helped.

I assume gcc decided the ... part needed the registers and so it pushed the variables onto the stack.

The stack can be used for any local variable or function argument. The only requirement by the SysV x64 ABI is that the first few function arguments start in registers.

The function can then move the arguments onto the stack and bring a local variable into a register if it wants, it's up to the compiler!

Variables that are local are not guaranteed to be put on the stack! Nor are args going to stay in the registers.

So it pushes argc and argv, then it decided to push i and output. That's 24 bytes of space. But if a function call is next, then the stack has to be aligned to 16 bytes, thus that goes to 32.

Try -O3 and see if the code chagnes.

To put it another way:

Args go like this: %rdi %rsi %rdx %rcx %rbx %r8 %r9 (extra args on the stack) (red zone)

Is the start of the function. The function can then do whatever it wants with the stack. Yes, whatever! As long as it returns the stack in the state it got it in.

(Note, this is for system V (used by everything except windows), windows is different)

链接地址: http://www.djcxy.com/p/84332.html

上一篇: c / c ++在堆栈上分配

下一篇: GCC与x64 sta