0

I had some problem when I learned about assembly code.

I use "compiler explorer" that is a website that supporting a lot of compiler.

I made a simple code and compiled it as x86-64 gcc.

<C++ code>:

int sum(int a, int b)
{
    return a + b;
}

int main(void)
{
    return sum(3, 4);
}

:

sum(int, int):

    push    rbp
    mov     rbp, rsp
    mov     DWORD PTR [rbp-4], edi
    mov     DWORD PTR [rbp-8], esi
    mov     edx, DWORD PTR [rbp-4]
    mov     eax, DWORD PTR [rbp-8]
    add     eax, edx
    pop     rbp
    ret

main:

    push    rbp
    mov     rbp, rsp
    mov     esi, 4
    mov     edi, 3
    call    sum(int, int)
    nop
    pop     rbp
    ret

As I know, stack presents for local variable and saving return address, etc in x86.

I can't see that anything about "sub rsp, ??" in function prologue. And I can't see "add rsp, ??" in function epilogue, too.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Maybe because there is no local variable in your code? – tofro Apr 07 '21 at 06:38
  • @tofro: main is a red herring and could be omitted from this [mcve]. The question is about `sum`, which does spill its function args to the stack. (In the red-zone below RSP, instead of reserving more stack space, because it's a leaf function.) – Peter Cordes Apr 07 '21 at 06:39
  • @PeterCordes : Thanks to you, it was solved clearly!!! Why couldn't I find the posts about this question... I'm so ashamed... – Changjin Koo Apr 07 '21 at 07:12
  • 1
    It helps when you know the answer so you what to look for; I found them by searching on "site:stackoverflow.com red zone gcc stack frame`. So IDK how easily they'd come up searching on terms like "missing sub", especially if phrasing it as "gcc writing outside its stack frame" didn't occur to you. – Peter Cordes Apr 07 '21 at 07:15

0 Answers0