title: Assembly: REP MOVS mechanism
body: <p>Looking at the following assembly code:  </p>
  
  <pre><code>MOV ESI, DWORD PTR [EBP + C]
  MOV ECX, EDI
  MOV EAX, EAX
  SHR ECX, 2
  LEA EDI, DWORD PTR[EBX + 18]
  REP MOVS DWORD PTR ES:[EDI], DWORD PTR [ESI]
  MOV ECX, EAX
  AND ECX, 3
  REP MOVS BYTE PTR ES:[EDI], BYTE PTR[ESI]
  </code></pre>
  
  <p>The book I got the code excerpt from explains the first <code>REP MOVS</code> as copying over 4-byte chunks, with the second <code>REP MOVS</code> copying the remaining 2-byte chunk, if it exists.</p>
  
  <p>How do the <code>REP MOVS</code> instructions operate? According to <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff561504(v=vs.85).aspx" rel="noreferrer">MSDN</a>, "The instruction can be prefixed by REP to repeat the operation the number of times specified by the ecx register." Wouldn't that just repeat the same operation over and over again?  </p>