x86 calling convention: should arguments passed by stack be read-only?

后端 未结 3 798
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 11:26

It seems state-of-art compilers treat arguments passed by stack as read-only. Note that in the x86 calling convention, the caller pushes arguments onto the stack and the callee

3条回答
  •  一向
    一向 (楼主)
    2021-02-07 12:03

    Actually, I just compiled this function using GCC:

    int foo(int x)
    {
        goo(&x);
        return x;
    }
    

    And it generated this code:

    _foo:
            pushl       %ebp
            movl        %esp, %ebp
            subl        $24, %esp
            leal        8(%ebp), %eax
            movl        %eax, (%esp)
            call        _goo
            movl        8(%ebp), %eax
            leave
            ret
    

    This is using GCC 4.9.2 (on 32-bit cygwin if it matters), no optimizations. So in fact, GCC did exactly what you thought it should do and used the argument directly from where the caller pushed it on the stack.

提交回复
热议问题