title: Passing argument from C to Assembly
body: <p>I'm trying to make a program in C that uses a function from Assembly. Below you can see the code:</p>
  
  <p><strong><em>sum_c.c</em></strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  extern int _assemblySum(int x, int y);
  
  int main(int argc, char *argv[]){
      int total;
  
      total = _assemblySum(4, 2);
      printf("%d\n", total);
      return 0;
  }
  </code></pre>
  
  <p><strong><em>assembly_Sum.asm</em></strong></p>
  
  <pre><code>SECTION .DATA
  
  SECTION .TEXT
      GLOBAL _assemblySum
  
  _assemblySum:
      push rbp
      mov rbp, rsp
      mov rax, [rbp+16]
      mov rbx, [rbp+24]
      add rax, rbx
      pop rbp
      ret
  </code></pre>
  
  <p><strong>COMPILE</strong></p>
  
  <pre><code>nasm -f elf64 assembly_sum.asm -o assembly_sum.o
  gcc c_sum.c assembly_sum.o -o sum
  ./sum
  </code></pre>
  
  <p>When I run the program I just get random numbers like -1214984584 or 2046906200. I know that I need to use the registers rdi, rsi, rdx and rcx because the 64bit GNU/Linux compiler uses them (<a href="https://stackoverflow.com/questions/22917922/passing-arguments-from-c-to-64bit-linux-assembly">Passing arguments from C to 64bit linux Assembly</a>). But how can I do that?</p>