What happens when a prefix prefix is ​​attached to a non-line instruction?

I tried to figure out the difference in speed between equal loops, loop loops and built-in rep loops. I wrote three programs to compare 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 that the third program does not work as expected, and some recherche tell me that rep nop aka pause does something completely unrelated.

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

+6
source share
2 answers

It depends. rep ret sometimes used to avoid the bad results of switching directly to ret on some AMD processors. The prefixes rep (F3) and repne (F2) are also used as a mandatory prefix for many SSE instructions (for example, they change packed single variants to scalar-synchronous or scalar-double variants). pause (hint to block hide and seek) is an alias of rep nop . Some other new instructions also use the "fake representation prefix" ( popcnt , crc32 , vmxon , etc.). A “fake” or mandatory prefix precedes the optional REX prefix, so you cannot say that it is part of the operation code, it is really a prefix.

Other operations generate #UD if the prefix is ​​with rep .

+8
source

I will simply give manual here because, I think, this only indicates "official" behavior.

From section 4.2 "REP / REPE / REPZ / REPNE / REPNZ - prefix of the operation of repeating a line":

The prefix REP can be added to the INS, OUTS, MOVS, LODS and STOS instructions, and the prefixes REPE, REPNE, REPZ, and REPNZ 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-line instructions.

+4
source

Source: https://habr.com/ru/post/913787/


All Articles