title: What does "rep; nop;" mean in x86 assembly? Is it the same as the "pause" instruction?
body: <ul>
  <li>What does <code>rep; nop</code> mean?</li>
  <li>Is it the same as <code>pause</code> instruction?</li>
  <li>Is it the same as <code>rep nop</code> (without the semi-colon)?</li>
  <li>What's the difference to the simple <code>nop</code> instruction?</li>
  <li>Does it behave differently on AMD and Intel processors?</li>
  <li>(bonus) Where is the official documentation for these instructions?</li>
  </ul>
  
  <hr>
  
  <h3>Motivation for this question</h3>
  
  <p>After some discussion in the comments of <a href="https://stackoverflow.com/questions/7083482/how-to-prevent-compiler-optimization-on-a-small-piece-of-code">another question</a>, I realized that I don't know what <code>rep; nop;</code> means in x86 (or x86-64) assembly. And also I couldn't find a good explanation on the web.</p>
  
  <p>I know that <code>rep</code> is a prefix that means <em>"repeat the next instruction <code>cx</code> times"</em> (or at least it was, in old 16-bit x86 assembly). According to this <a href="http://en.wikipedia.org/wiki/X86_instruction_listings#Original_8086.2F8088_instructions" rel="noreferrer">summary table at Wikipedia</a>, it seems <code>rep</code> can only be used with <code>movs</code>, <code>stos</code>, <code>cmps</code>, <code>lods</code>, <code>scas</code> (but maybe this limitation was removed on newer processors). Thus, I would think <code>rep nop</code> (without semi-colon) would repeat a <code>nop</code> operation <code>cx</code> times.</p>
  
  <p>However, after further searching, I got even more confused. It seems that <code>rep; nop</code> and <code>pause</code> <a href="https://stackoverflow.com/questions/2589447/is-it-necessary-that-each-machine-code-can-only-map-to-one-assembly-code/2589462#2589462">map to the exactly same opcode</a>, and <code>pause</code> has a bit different behavior than just <code>nop</code>. Some <a href="http://www.x86-64.org/pipermail/discuss/2005-March/005800.html" rel="noreferrer">old mail from 2005</a> said different things:</p>
  
  <ul>
  <li><em>"try not to burn too much power"</em></li>
  <li><em>"it is equivalent to 'nop' just with 2 byte encoding."</em></li>
  <li><em>"it is magic on intel. Its like 'nop but let the other HT sibling run'"</em></li>
  <li><em>"it is pause on intel and fast padding on Athlon"</em></li>
  </ul>
  
  <p>With these different opinions, I couldn't understand the correct meaning.</p>
  
  <p>It's being used in Linux kernel (on both <a href="http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/arch/um/sys-i386/asm/processor.h#L53" rel="noreferrer">i386</a> and <a href="http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/arch/um/sys-x86_64/asm/processor.h#L20" rel="noreferrer">x86_64</a>), together with this comment: <code>/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */</code> It is also <a href="http://doc.bertos.org/2.6/attr_8h_source.html#l00096" rel="noreferrer">being used in BeRTOS</a>, with the same comment.</p>