x86_64 calling convention

As written in Wikipedia, Linux/MacOS X uses RDI, RSI, RDX, RCX, R8, and R9 for the first 6 args (uses XMM0-7 fro float) + stack for the others.

Windows uses RCX, RDX, R8, R9 (uses XMM0-3 for float) + stack.

#include <iostream>

using namespace std;

int foo(int a, int b, int c, int d, int e, int f, int g){
  int r = a+b+c+d+e+f+g;
  return r;
}

int main(int argc, char const* argv[])
{
  cout << foo(1,2,3,4,5,6,7);
  return 0;
}

MacOS X 10.9:

(lldb) disassemble --name main
foo`main:
foo[0x100000ec0]:  push   rbp
foo[0x100000ec1]:  mov    rbp, rsp
foo[0x100000ec4]:  push   rbx
foo[0x100000ec5]:  sub    rsp, 0x38
foo[0x100000ec9]:  mov    eax, 0x1
foo[0x100000ece]:  mov    ecx, 0x2
foo[0x100000ed3]:  mov    edx, 0x3
foo[0x100000ed8]:  mov    r8d, 0x4
foo[0x100000ede]:  mov    r9d, 0x5
foo[0x100000ee4]:  mov    r10d, 0x6
foo[0x100000eea]:  mov    r11d, 0x7
foo[0x100000ef0]:  mov    rbx, qword ptr [rip + 0x121] ; (void *)0x0000000000000000
foo[0x100000ef7]:  mov    rbx, qword ptr [rbx]
foo[0x100000efa]:  mov    qword ptr [rbp - 0x10], rbx
foo[0x100000efe]:  mov    dword ptr [rbp - 0x14], 0x0
foo[0x100000f05]:  mov    dword ptr [rbp - 0x18], edi
foo[0x100000f08]:  mov    qword ptr [rbp - 0x20], rsi
foo[0x100000f0c]:  mov    edi, eax
foo[0x100000f0e]:  mov    esi, ecx
foo[0x100000f10]:  mov    ecx, r8d
foo[0x100000f13]:  mov    r8d, r9d
foo[0x100000f16]:  mov    r9d, r10d
foo[0x100000f19]:  mov    dword ptr [rsp], 0x7
foo[0x100000f20]:  mov    dword ptr [rbp - 0x24], r11d
foo[0x100000f24]:  call   0x100000e50               ; foo(int, int, int, int, int, int, int)
foo[0x100000f29]:  mov    rdi, qword ptr [rip + 0xe0] ; (void *)0x0000000000000000
foo[0x100000f30]:  mov    esi, eax
foo[0x100000f32]:  call   0x100000f60               ; symbol stub for: std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int)
foo[0x100000f37]:  mov    rdi, qword ptr [rip + 0xda] ; (void *)0x0000000000000000
foo[0x100000f3e]:  mov    rdi, qword ptr [rdi]
foo[0x100000f41]:  cmp    rdi, qword ptr [rbp - 0x10]
foo[0x100000f45]:  mov    qword ptr [rbp - 0x30], rax
foo[0x100000f49]:  jne    0x100000f5b               ; main + 155
foo[0x100000f4f]:  mov    eax, 0x0
foo[0x100000f54]:  add    rsp, 0x38
foo[0x100000f58]:  pop    rbx
foo[0x100000f59]:  pop    rbp
foo[0x100000f5a]:  ret    
foo[0x100000f5b]:  call   0x100000f66               ; symbol stub for: __stack_chk_fail
 
(lldb) disassemble --name foo
foo`foo(int, int, int, int, int, int, int):
foo[0x100000e50]:  push   rbp
foo[0x100000e51]:  mov    rbp, rsp
foo[0x100000e54]:  sub    rsp, 0x30
foo[0x100000e58]:  mov    eax, dword ptr [rbp + 0x10]
foo[0x100000e5b]:  mov    r10, qword ptr [rip + 0x1b6] ; (void *)0x0000000000000000
foo[0x100000e62]:  mov    r11, qword ptr [r10]
foo[0x100000e65]:  mov    qword ptr [rbp - 0x8], r11
foo[0x100000e69]:  mov    dword ptr [rbp - 0xc], edi
foo[0x100000e6c]:  mov    dword ptr [rbp - 0x10], esi
foo[0x100000e6f]:  mov    dword ptr [rbp - 0x14], edx
foo[0x100000e72]:  mov    dword ptr [rbp - 0x18], ecx
foo[0x100000e75]:  mov    dword ptr [rbp - 0x1c], r8d
foo[0x100000e79]:  mov    dword ptr [rbp - 0x20], r9d
foo[0x100000e7d]:  mov    dword ptr [rbp - 0x24], eax
foo[0x100000e80]:  mov    eax, dword ptr [rbp - 0xc]
foo[0x100000e83]:  add    eax, dword ptr [rbp - 0x10]
foo[0x100000e86]:  add    eax, dword ptr [rbp - 0x14]
foo[0x100000e89]:  add    eax, dword ptr [rbp - 0x18]
foo[0x100000e8c]:  add    eax, dword ptr [rbp - 0x1c]
foo[0x100000e8f]:  add    eax, dword ptr [rbp - 0x20]
foo[0x100000e92]:  add    eax, dword ptr [rbp - 0x24]
foo[0x100000e95]:  mov    dword ptr [rbp - 0x28], eax
foo[0x100000e98]:  mov    eax, dword ptr [rbp - 0x28]
foo[0x100000e9b]:  mov    r10, qword ptr [r10]
foo[0x100000e9e]:  cmp    r10, qword ptr [rbp - 0x8]
foo[0x100000ea2]:  mov    dword ptr [rbp - 0x2c], eax
foo[0x100000ea5]:  jne    0x100000eb4               ; foo(int, int, int, int, int, int, int) + 100
foo[0x100000eab]:  mov    eax, dword ptr [rbp - 0x2c]
foo[0x100000eae]:  add    rsp, 0x30
foo[0x100000eb2]:  pop    rbp
foo[0x100000eb3]:  ret    
foo[0x100000eb4]:  call   0x100000f66               ; symbol stub for: __stack_chk_fail
foo[0x100000eb9]:  nop    dword ptr [rax]

Windows 8:

0:000> uf foo!main (int, char **)
foo!main [c:\users\sokoide\projects\spike\foo\foo.cpp @ 16]:
   16 00007ff7`fbfd2400 4889542410      mov     qword ptr [rsp+10h],rdx
   16 00007ff7`fbfd2405 894c2408        mov     dword ptr [rsp+8],ecx
   16 00007ff7`fbfd2409 57              push    rdi
   16 00007ff7`fbfd240a 4883ec40        sub     rsp,40h
   16 00007ff7`fbfd240e 488bfc          mov     rdi,rsp
   16 00007ff7`fbfd2411 b910000000      mov     ecx,10h
   16 00007ff7`fbfd2416 b8cccccccc      mov     eax,0CCCCCCCCh
   16 00007ff7`fbfd241b f3ab            rep stos dword ptr [rdi]
   16 00007ff7`fbfd241d 8b4c2450        mov     ecx,dword ptr [rsp+50h]
   17 00007ff7`fbfd2421 c744243007000000 mov     dword ptr [rsp+30h],7
   17 00007ff7`fbfd2429 c744242806000000 mov     dword ptr [rsp+28h],6
   17 00007ff7`fbfd2431 c744242005000000 mov     dword ptr [rsp+20h],5
   17 00007ff7`fbfd2439 41b904000000    mov     r9d,4
   17 00007ff7`fbfd243f 41b803000000    mov     r8d,3
   17 00007ff7`fbfd2445 ba02000000      mov     edx,2
   17 00007ff7`fbfd244a b901000000      mov     ecx,1
   17 00007ff7`fbfd244f e8f6ecffff      call    foo!ILT+325(?fooYAHHHHHHHHZ) (00007ff7`fbfd114a)
   17 00007ff7`fbfd2454 8bd0            mov     edx,eax
   17 00007ff7`fbfd2456 488b0dc3ec0000  mov     rcx,qword ptr [foo!_imp_?coutstd (00007ff7`fbfe1120)]
   17 00007ff7`fbfd245d ff15c5ec0000    call    qword ptr [foo!_imp_??6?$basic_ostreamDU?$char_traitsDstdstdQEAAAEAV01HZ (00007ff7`fbfe1128)]
   18 00007ff7`fbfd2463 33c0            xor     eax,eax
   19 00007ff7`fbfd2465 4883c440        add     rsp,40h
   19 00007ff7`fbfd2469 5f              pop     rdi
   19 00007ff7`fbfd246a c3              ret
                   ^ Extra character error in 'uf foo!main (int, char **)'
0:000> uf foo!foo (int, int, int, int, int, int, int)
foo!foo [c:\users\sokoide\projects\spike\foo\foo.cpp @ 10]:
   10 00007ff7`fbfd23a0 44894c2420      mov     dword ptr [rsp+20h],r9d
   10 00007ff7`fbfd23a5 4489442418      mov     dword ptr [rsp+18h],r8d
   10 00007ff7`fbfd23aa 89542410        mov     dword ptr [rsp+10h],edx
   10 00007ff7`fbfd23ae 894c2408        mov     dword ptr [rsp+8],ecx
   10 00007ff7`fbfd23b2 57              push    rdi
   10 00007ff7`fbfd23b3 4883ec10        sub     rsp,10h
   10 00007ff7`fbfd23b7 488bfc          mov     rdi,rsp
   10 00007ff7`fbfd23ba b904000000      mov     ecx,4
   10 00007ff7`fbfd23bf b8cccccccc      mov     eax,0CCCCCCCCh
   10 00007ff7`fbfd23c4 f3ab            rep stos dword ptr [rdi]
   10 00007ff7`fbfd23c6 8b4c2420        mov     ecx,dword ptr [rsp+20h]
   11 00007ff7`fbfd23ca 8b442428        mov     eax,dword ptr [rsp+28h]
   11 00007ff7`fbfd23ce 8b4c2420        mov     ecx,dword ptr [rsp+20h]
   11 00007ff7`fbfd23d2 03c8            add     ecx,eax
   11 00007ff7`fbfd23d4 8bc1            mov     eax,ecx
   11 00007ff7`fbfd23d6 03442430        add     eax,dword ptr [rsp+30h]
   11 00007ff7`fbfd23da 03442438        add     eax,dword ptr [rsp+38h]
   11 00007ff7`fbfd23de 03442440        add     eax,dword ptr [rsp+40h]
   11 00007ff7`fbfd23e2 03442448        add     eax,dword ptr [rsp+48h]
   11 00007ff7`fbfd23e6 03442450        add     eax,dword ptr [rsp+50h]
   11 00007ff7`fbfd23ea 890424          mov     dword ptr [rsp],eax
   12 00007ff7`fbfd23ed 8b0424          mov     eax,dword ptr [rsp]
   13 00007ff7`fbfd23f0 4883c410        add     rsp,10h
   13 00007ff7`fbfd23f4 5f              pop     rdi
   13 00007ff7`fbfd23f5 c3              ret
                  ^ Extra character error in 'uf foo!foo (int, int, int, int, int, int, int)'

Leave a Reply

Your email address will not be published. Required fields are marked *