1

I'm learning assembly, and I tried compiling the following C code into assembly with GCC with optimization disabled (https://godbolt.org/z/4cz3ocfa5)

void f() {
    int x = 1;
    int y = 2;
    int z = 3;
}

int main() {
    f();
    return 0;
}

assembly,

f():
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], 1
        mov     DWORD PTR [rbp-8], 2
        mov     DWORD PTR [rbp-12], 3
        nop
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        call    f()
        mov     eax, 0
        pop     rbp
        ret

I'm confused why where is no mov rsp, rbp before the pop instruction in the epilogue (especially when there are local variables in the stack frame of f).
I thought a complete epilogue should be like

    mov rsp, rbp
    pop rbp
    ret

enter image description here

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Guanwei HU
  • 55
  • 4
  • 2
    RSP is already equal to RBP; a `mov` would be redundant and the compiler knows that because it generated the code. This is possible because it put the variables in the red zone below RSP, a feature of the x86-64 System V ABI. Use `-mno-red-zone` if you want it not to take advantage of that and match your traditional stack layout diagrams. – Peter Cordes Feb 12 '23 at 06:35
  • Another duplicate: [Why there is no \`leave\` instruction at function epilog on x64?](https://stackoverflow.com/q/54781648) – Peter Cordes Feb 12 '23 at 06:43

0 Answers0