且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在使用返回值时将0移到堆栈?

更新时间:2022-11-11 11:38:52

以下代码揭示了该区域的目的

The purpose of that area is revealed by the following code

int main(int argc, char** argv)
{
    if (rand() == 42)
      return 1;

    printf("Helo World!\n");
    return 0;
}

一开始就是

movl    $0, -4(%rbp)

然后,早期收益如下所示

then the early return looks as follows

callq   rand
cmpl    $42, %eax
jne .LBB0_2
movl    $1, -4(%rbp)
jmp .LBB0_3

然后在最后

.LBB0_3:
movl    -4(%rbp), %eax
addq    $32, %rsp
popq    %rbp
retq

因此,确实保留了该区域来存储函数返回值.似乎并不是非常必要,它也没有在优化的代码中使用,但是在-O0模式下它才是它的工作方式.

So, this area is indeed reserved to store the function return value. It doesn't appear to be terribly necessary and it is not used in optimized code, but in -O0 mode that's the way it works.