title: Link C with NASM in 64 bit
body: <p>I have a NASM file and a C file. My OS is Ubuntu 17.04 64 bit</p>
  
  <p>I have followed the instruction from another post. <a href="https://stackoverflow.com/questions/24991944/linking-c-with-nasm">Linking C with NASM</a></p>
  
  <p>Here's my code</p>
  
  <p>main.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int doit(int a, int b);
  
  int main()
  {
    printf("%d\n", doit(2,4));
    return 0;
  }
  </code></pre>
  
  <p>doit.asm:</p>
  
  <pre><code>global doit
  
  section .data
  
  section .text
  
  doit:
  
      xor   rax, rax          ;sets rax to 0
  
      mov   eax, [rsp+8]      ;sets **b** to eax(32bit = int size)
      add   eax, [rsp+16]     ;adds **a** to eax(32bit = int size)
  
      ret
  </code></pre>
  
  <p>compiling:</p>
  
  <pre><code> home@main:~/Desktop/TEST$ nasm -f elf64 doit.asm &amp;&amp; gcc -Wall main.c doit.o
   home@main:~/Desktop/TEST$ ./a.out 318503633
   home@main:~/Desktop/TEST$
  </code></pre>
  
  <p>As you can see, The result is not even close to the predicted result, which is 6</p>
  
  <p>Please tell me why is the result different from the 32bit asm</p>