title: Why is the value of EDX overwritten when making call to printf?
body: <p>I've written a simple assembly program:</p>
  
  <pre><code>section .data
  str_out db "%d ",10,0
  section .text
  extern printf
  extern exit
  global main
  main:
  
  MOV EDX, ESP
  MOV EAX, EDX
  PUSH EAX
  PUSH str_out
  CALL printf
  SUB ESP, 8 ; cleanup stack
  MOV EAX, EDX
  PUSH EAX
  PUSH str_out
  CALL printf
  SUB ESP, 8 ; cleanup stack
  CALL exit
  </code></pre>
  
  <p>I am the NASM assembler and the GCC to link the object file to an executable on linux.</p>
  
  <p>Essentially, this program is first putting the value of the stack pointer into register EDX, it is then printing the contents of this register twice. However, after the second printf call, the value printed to the stdout does not match the first.</p>
  
  <p>This behaviour seems strange. When I replace every usage of EDX in this program with EBX, the outputted integers are identical as expected. I can only infer that EDX is overwritten at some point during the printf function call.</p>
  
  <p>Why is this the case? And how can I make sure that the registers I use in future don't conflict with C lib functions?</p>