Repeat prefixes and mandatory prefixes in x86

1.4k views Asked by At

In my quest of writing a small disassembler for linux specific to x86 arch, I'm faced with a small issue. It's with regard to mandatory prefixes and repeat prefixes. Looking at the Intel docs [1], it's said that repeat prefixes are 0xf2 or 0xf3, and mandatory prefixes are 0x66, 0xf2 or 0xf3.

There are two instructions which have the following base opcodes:

crc32 -- f2 0f 38 f0 (Here, 0xf2 is a mandatory prefix)
movbe -- 0f 38 f0

So, the opcodes of a 'movbe' instruction which has to repeat as long as the counter register is non-zero should be:

repnz movbe == f2 0f 38 f0

When I start disassembling an instruction, if I see the byte 0xf2, how do I know that it's a mandatory prefix for the crc32 instruction but not a repeat prefix for the movbe instruction, or vice-versa? Which instruction do I match the opcode pattern "f2 0f 38 f0" to?

What am I missing?

[1] http://www.intel.com/design/intarch/manuals/243191.HTM

Thanks and Regards,
Hrishikesh Murali

2

There are 2 answers

3
MazeGen On

You can use the repeat prefixes only with string instructions (see the manual). "f2 0f 38 f0" is always CRC32 instruction.

5
Alexey Frunze On

MOVBE, (move to/from big-endian in memory), is not an instruction repeatable through a REP((N)E) prefix.

Only string instructions are repeatable that way. Those are: MOVS*, LODS*, STOS*, SCAS*, CMPS*, INS*, OUTS*, where * is either of B, W, D or Q (except INS* and OUTS*, which only go up to double words, not quad words).

Intel's manual entry for rep/rep(n)e explains that.