title: NASM Linux 32-bit: scanf, printf calls
body: <p>I've written simple assembly code, which uses <code>printf</code> and <code>scanf</code> to write and read numbers, to and from command line respectively.For this particular code, I'm getting output different from given input. It seems that first value printed is <code>[var1]+[var2]*2^8</code> . However, by increasing size of variables <code>var1</code> &amp; <code>var2</code> to 4 bytes, code gives correct output. How?</p>
  
  <pre><code>    ;assemble and compile with :
      ;nasm -f elf testing.asm &amp;&amp; gcc -m32 -o testing testing.o
      extern printf,scanf
  
      ;store eax,ebc,ecx,edx onto the stack
      %macro push_reg 0
      push edx
      push ecx
      push ebx
      push eax
      %endmacro
  
      ;restore eax,ebx,ecx,edx
      %macro pop_reg 0
      pop eax
      pop ebx
      pop ecx
      pop edx
      %endmacro
  
      section .text
      global main
      main:
      ;reads number var1
      push_reg          
      push var1
      push formatin
      call scanf          ;scanf("%d",var1);
      add esp,8           ;restoring stack pointer 
      pop_reg            
  
      ;reads number var1
      push_reg
      push var2
      push formatin
      call scanf        ;scanf("%d",var2);
      add esp,8         ;restoring stack pointer 
      pop_reg           
  
      ;printing number var1
      push_reg          
      push dword[var1]
      push formatout
      call printf       ;printf("%d",content of var1);
      add esp,8         ;restoring stack pointer 
      pop_reg          
  
      ;printing number var2
      push_reg
      push dword[var2]
      push formatout
      call printf       ;printf("%d",content of var2);
      add esp,8         ;restoring stack pointer 
      pop_reg       
  
      exit:
      mov eax,1
      int 0x80
  
      section .bss
      var1 resb 1
      var2 resb 1
  
      section .data
      formatout: db "%d",10,0
      formatin: db "%d",0
  </code></pre>
  
  <p>Input:</p>
  
  <pre><code>1
  1
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>257
  1
  </code></pre>