title: Assembly (AT&T 32 bit) scanf questions
body: <p>I want to write the following C code in Assembly:</p>
  
  <pre><code>int main(void)
  {
      int x,y;
      scanf("%d%d",&amp;x,&amp;y);
      printf("%d%d",x,y);
      return 0;
  }
  </code></pre>
  
  <p>Firstly I tried it with only one integer to scan/print:</p>
  
  <pre><code>.section    .rodata #read only data section
  fmt:    .string "%d%d"
      .text   
  .globl  main    
      .type   main, @function
  main:   
  pushl   %ebp    #save the old frame pointer
  movl    %esp,   %ebp    #create the new frame pointer
  
  pushl %esp #location of x
  pushl $fmt
  call scanf
  
      #stack should now have exactly the scanned number x and then the format, as needed for printf.
  call printf
  
  movl    $0, %eax
  movl    %ebp,   %esp    #restore the old stack pointer - release all used memory.
  popl    %ebp    #restore old frame pointer (the caller function frame)
  ret 
  </code></pre>
  
  <p>But it did not work. For some reason, the following trick made it work (added before the printf):</p>
  
  <pre><code>addl $4,%esp #pop format
  pushl 4(%esp)
  pushl $fmt
  </code></pre>
  
  <p>I don't understand why would pushl 4(%esp) make it work, so for my first question I am asking for a clarification on that matter.
  Then I tried doing the same with two variables:</p>
  
  <pre><code>fmt:    .string "%d%d"
  [...]
      pushl %esp #location of x
      pushl %esp #location of y
      pushl $fmt
      call scanf
  </code></pre>
  
  <p>But it caused a segmentation error. It didn't even reached the printf part, where I would have tried something like this:</p>
  
  <pre><code>addl $4,%esp #pop format    
  pushl 8(%esp)
  pushl 8(%esp)
  pushl $fmt
  
  call printf
  </code></pre>
  
  <p>(Following the same logic as with the pushl 4(%esp) before.
  So my second question is how can I make it work with two variables.
  Thank you!</p>
  
  <p>edit: Why would the following code won't work for scanning two variables?</p>
  
  <pre><code>subl $8,%esp #leave place for two vars
  pushl -4(%ebp) #location of x
  pushl -8(%ebp) #location of y
  pushl $fmt
  call scanf
  </code></pre>