title: x86-64 order of passing parameters in registers
body: <p>I'm curious about the parameter passing procedure in the x86-64 environment and therefore I wrote a snippet of code.</p>
  
  <pre><code>//a.c
  extern int shared;
  int main(){
      int a=100;
      swap(&amp;a, &amp;shared);
  }
  //b.c
  int shared=1;
  void swap(int* a, int* b){
      *a ^= *b ^= *a ^= *b;
  }
  </code></pre>
  
  <p>I compile two files using the following commands:
  <code>gcc -c -fno-stack-protector a.c b.c</code>
  Then I <code>objdump -d a.o</code> to check a.o's disassembly code.</p>
  
  <pre><code>Disassembly of section .text:
  
  0000000000000000 &lt;main&gt;:
     0:   55                      push   %rbp
     1:   48 89 e5                mov    %rsp,%rbp
     4:   48 83 ec 10             sub    $0x10,%rsp
     8:   c7 45 fc 64 00 00 00    movl   $0x64,-0x4(%rbp)
     f:   48 8d 45 fc             lea    -0x4(%rbp),%rax
    13:   be 00 00 00 00          mov    $0x0,%esi
    18:   48 89 c7                mov    %rax,%rdi
    1b:   b8 00 00 00 00          mov    $0x0,%eax
    20:   e8 00 00 00 00          callq  25 &lt;main+0x25&gt;
    25:   b8 00 00 00 00          mov    $0x0,%eax
    2a:   c9                      leaveq 
    2b:   c3                      retq
  </code></pre>
  
  <p>Due to my working environment is <strong>Ubuntu 16.04 x86-64</strong>, I found it hard to understand the order of passing parameter.</p>
  
  <p>In my point of view, the default call convention is <code>fastcall</code> here and therefore the parameters are passed from <strong>right to left</strong>.</p>
  
  <p>I know from the x86-64 System V ABI manual that <code>rdi</code> and <code>rsi</code> are used for passing the first two parameters <a href="https://i.stack.imgur.com/eaSf7.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/eaSf7.jpg" alt="enter image description here"></a></p>
  
  <p>However, according to the disassembly code, <code>rdi</code> is responsible for <code>var a</code>, which is the param on the left, meaning it should be the <strong>second</strong> param.</p>
  
  <p>Could someone help me point out my mistake? </p>