CMPS            Compare Strings                      Flags: O D I T S Z A P C
* - - - * * * * *
CMPS source_string,destination_string

Logic CMP [DS:eSI],[ES:eDI]
if DF = 0
eSI ← eSI + n ; n = 1 for byte, 2 for word,
eDI ← eDI + n ; 4 for dword (386+)
else
eSI ← eSI - n
eDI ← eDI - n
endif

This instruction compares two values by subtracting the
destination string element pointed to by ES:eDI, from the source
string element pointed to by DS:eSI, and sets the flags according
to the results of the comparison. The operands themselves are not
altered. After the comparison, eSI and eDI are incremented (if the
direction flag is cleared) or decremented (if the direction flag
is set), in preparation for comparing the next element of the
string. If the address-size attribute of CMPS is 16, the SI and DI
registers will be used as source and destination indices,
otherwise ESI and EDI.

The source segment can be changed with a segment override, the
destination segment cannot.


Note: This instruction is always translated by the
assembler into CMPSB, Compare String Byte, CMPSW,
Compare String Word, or CMPSD, Compare String Dword,
depending upon whether source refers to a string of
bytes, words or doublewords. In either case, you must
explicitly load the eSI and eDI registers with the
offset of the source and destination strings.

CMPSB, CMPSW, and CMPSD are synonyms for the byte,
word, and doubleword CMPS instructions that do not
require any operands. They are simpler to use but
provide no type or segment checking.


If the REPE or REPNE prefix modifies this instruction, the CPU
compares the value of the destination string element to the value
of the source string element. It then steps eSI and eDI in the
direction indicated by DF by the indicated size, until either the
REPE/REPNE condition is false or eCX counts to zero.


Opcode Format
A6 CMPS m8,m8 ; = CMPSB
A7 CMPS m16,m16 ; = CMPSW
A7 CMPS m32,m32 ; = CMPSD


Length and timing
Variations Bytes 8088 186 286 386 486 Pentium
cmpsb 1 30 22 8 10 8 5 NP
cmpsw 1 - - - 10 8 5 NP
cmpsd 1 - - - 10 8 5 NP
repX cmpsb 2 9+30n 5+22n 5+9n 5+9n 7+7n* 9+4n NP
repX cmpsw 2 9+30n 5+22n 5+9n 5+9n 7+7n* 9+4n NP
repX cmpsd 2 - - - 5+9n 7+7n* 9+4n NP

repX = repe, repz, repne, or repnz
* : 5 if n = 0