title: mov instruction fetches data from memory to memory in x86 assembly
body: <p>I've disassembled a <em>x86 elf binary</em> that was making use of the C <code>scanf</code> function.<br>
  Here is the disassembled chunk of code related to <code>scanf</code> : </p>
  
  <pre><code>0x0804857a      89442404       mov dword [esp + 0x4], eax  
  0x0804857e      c70424b28604.  mov dword [esp], 0x80486b2                                 
  0x08048585      e8eafdffff     call sym.imp.scanf  
  </code></pre>
  
  <p>Checking with <code>gdb</code>, the memory at the address <code>0x80486b2</code> contains the data <code>0x7325</code> ("<strong>%s</strong>" string in <em>ASCII</em> code).<br>
  So what this code apparently does is <em>pushing</em> <code>scanf</code> parameters in reverse order on the stack so as to call <code>scanf</code> with these two arguments.<br>
  This would have been typically coded in C as <code>scanf ("%s", &amp;somevar);</code>  </p>
  
  <p>What I would have expected here given the assembly code is that the <em>32-bit representation of the <strong>constant</em></strong> <code>0x80486b2</code> gets loaded <em>into the address</em> pointed to by the stack pointer ...<br>
  But rather, the <code>mov</code> instruction has loaded the <em>32-bit representation of <strong>whatever was at the address</em></strong> <code>0x80486b2</code> <em>into the address</em> pointed to by the stack pointer ... Is that right ?   </p>
  
  <p>So what we basically get is that <code>mov</code> has just moved around data from a <em>memory location</em> to another <em>memory location</em>, which according to this <a href="http://www.cs.virginia.edu/~evans/cs216/guides/x86.html" rel="nofollow noreferrer">x86 assembly introduction</a> (among a plethora of others sources) is illicit (emphasis is mine): </p>
  
  <blockquote>
    <p>In cases where memory transfers are desired, the source memory
    contents must <strong><em>first be loaded into a register</em></strong>, then can be stored to
    the destination memory address.</p>
  </blockquote>
  
  <p>No register has been made use of here as intermediary.<br>
  How is that possible ?</p>