7

I tried to find out the speed difference between plain loops, loop loops and builtin rep loops. I wrote three programs to compare the behavior:

Program 1

_start: xor %ecx,%ecx
0:      not %ecx
        dec %ecx
        jnz 0b
        mov $1,%eax
        xor %ebx,%ebx
        int $0x80       # syscall 1: exit

Program 2

_start: xor %ecx,%ecx
        not %ecx
        loop .
        mov $1,%eax
        xor %ebx,%ebx
        int $0x80

Program 3

_start: xor %ecx,%ecx
        not %ecx
        rep nop # Do nothing but decrement ecx
        mov $1,%eax
        xor %ebx,%ebx
        int $0x80

It turned out the third program doesn't work as expected, and some recherche tells me, that rep nop aka pause does something completely unrelated.

What are the rep, repz and repnz prefixes doing, when the instruction following them is not a string instruction?

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Duplicate of http://stackoverflow.com/questions/7086220/what-does-rep-nop-mean-in-x86-assembly – Sedat Kapanoglu Apr 21 '12 at 12:40
  • @ssg: Nope. The question you links just explains the behavior for `rep nop`. What happens for other instructions? Please try to read the entire question before voting to close. – fuz Apr 21 '12 at 12:53
  • It seems to me that your experiementation and research efforts all went into NOP, so I thought you wouldn't care about the rest since you didn't even care to test them? – Sedat Kapanoglu Apr 21 '12 at 13:02
  • @ssg There are many people whose main activity seems to be doing NOPs :-) – Gunther Piez Apr 21 '12 at 13:34
  • @ssg I just noticed `rep` doesn't works as expected with `nop` and also with `cmp $0,%ecx`. I am new to assembly so I wanted to ask before digging further. – fuz Apr 21 '12 at 14:39

2 Answers2

9

It depends. rep ret is sometimes used to avoid bad performance of jumping directly to a ret on certain AMD processors. The rep (F3) and repne (F2) prefixes are also used as Mandatory Prefix for many SSE instructions (for example they change packed-single variants to scalar-singe or scalar-double variants). pause (spin lock hint) is an alias of rep nop. Some other new instructions use a "fake rep prefix" as well (popcnt, crc32, vmxon, etc). The "fake" or Mandatory Prefix comes before the optional REX prefix, so it can't be said to be part of the opcode, it really is a prefix.

Other operations generate an #UD if prefixed with a rep.

harold
  • 61,398
  • 6
  • 86
  • 164
4

I'm just going to quote the manual here because I guess that specifies the only "official" behavior.

From Section 4.2 "REP/REPE/REPZ/REPNE/REPNZ - Repeat String Operation Prefix":

The REP prefix can be added to the INS, OUTS, MOVS, LODS, and STOS instructions, and the REPE, REPNE, REPZ, and REPNZ prefixes can be added to the CMPS and SCAS instructions. (The REPZ and REPNZ prefixes are synonymous forms of the REPE and REPNE prefixes, respectively.) The behavior of the REP prefix is undefined when used with non-string instructions.

mtvec
  • 17,846
  • 5
  • 52
  • 83