为什么我得到的是 "popq %rbp" 而不是 "leave"?

Why am I getting "popq %rbp" instead of "leave"?

我没看出我做错了什么,这是我的 C 代码:

main() {
    int i = 0;
    if (i == 0) i++;
    return 0;
}

用 gcc -S 编译 test.c

我期待 "leave" 而不是 "popq %rbp"。

.L2:
movl [=13=], %eax
popq %rbp
ret

Intel64 和 IA-32 架构软件开发人员手册说:

The LEAVE instruction, which does not any operands, reverses the action of the previous ENTER instruction. The LEAVE instruction copies the contents of the EBP register into the ESP register to release all stack space allocated to the procedure. Then it restores the old value of the EBP register from the stack. This simultaneously restores the ESP register to its original value. A subsequent RET instruction then can remove any arguments and the return address pushed on the stack by the calling program for use by the procedure.

gcc 手动执行此操作。

我没看出我做错了什么

是否使用ENTER/LEAVE由GCC决定。
因为即使是 INTEL 也反对使用 ENTER/LEAVE,难怪 GCC 不再使用它了。
此外,此处不需要 movq %rbp,%rsp,因此您只找到了 popq %rbp.