title: C calling conventions in assembly (64 bit) - scanf
body: <p>I have some assembly code that uses scanf and printf and I'm running into some problems. When both of these functions are used in the same code, the values in the registers seem to be lost. The program basically loads a number and prints it out. We run it using </p>
  
  <pre><code>nasm -f elf64 file.asm &amp;&amp; gcc -o file file.o &amp;&amp; ./file
  </code></pre>
  
  <p>on linux</p>
  
  <p>Here's our code:</p>
  
  <pre><code>extern printf
  extern scanf
  section .data
  
    a db "set: ", 0
    b db "not set: ", 0
    reading db "Please enter a number: ", 0
    message db "\n", 0
    printsent db "%s", 10, 0
    printint db "%d", 10, 0
    printchar db "%c", 10, 0
  
    readInt db "%d", 0
    input db "%d", 0
  
  section .text
      global main
  
  main:
  
  hatta: 
  push rbp,
  mov  rbp, rsp,
  push rbx,
  xor  rax, rax,
  mov  rdi, printsent,
  mov  rsi, reading
  call  printf,
  pop  rbx,
  
  xor  rax, rax,
  mov  rdi, readInt,
  call  scanf,
  mov  rbx, rdi
  
  push rbx,
  xor  rax, rax,
  mov  rdi, printint,
  
  mov  rsi, rbx,
  call  printf,
  pop  rbx,
  
  pop  rbp,
  ret
  </code></pre>
  
  <p>The odd thing is that if the line <code>mov rdi, printint,</code> is removed, we obtain the correct values. However, if we do the same thing with printsentence, we get a segmentation fault. Could anyone tell us the reason for this?</p>
  
  <p>Thanks!</p>