why gcc 4.x default reserve 8 bytes for stack on linux when calling a method?

前端 未结 3 1737
傲寒
傲寒 2021-01-02 15:06

as a beginner of asm, I am checking gcc -S generated asm code to learn.

why gcc 4.x default reserve 8 bytes for stack when calling a method?

func18 is the e

3条回答
  •  鱼传尺愫
    2021-01-02 15:56

    As richard mentioned above, it's all because of optimization, showing below. but still I got no idea why 8 bytes reserved is something optimized?!

    original c

    void func18() {}
    int main() {return 0;}
    

    compile without optimization flag specified

        .text                                                                                   
    .globl _func18
    _func18:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        leave
        ret
    .globl _main
    _main:                                                                                      
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    $0, %eax
        leave
        ret
        .subsections_via_symbols
    

    with -Os optimization flag, no more stack reserve

        .text
    .globl _func18
    _func18:
        pushl   %ebp
        movl    %esp, %ebp
        leave
        ret
    .globl _main
    _main:
        pushl   %ebp
        xorl    %eax, %eax
        movl    %esp, %ebp
        leave
        ret
        .subsections_via_symbols
    

提交回复
热议问题