CMPSB            Compare String Byte                 Flags: O D I T S Z A P C
*       * * * * *
CMPSB

Logic:    CMP (DS:SI), (ES:DI)           ; Sets flags only
if DF = 0
SI   SI + 1
DI   DI + 1
else
SI   SI - 1
DI   DI - 1

This instruction compares two values by subtracting the byte pointed
to by ES:DI, from the byte pointed to by DS:SI, and sets the flags
according to the results of the comparison. The operands themselves
are not altered. After the comparison, SI and DI 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.

                                                                          
Operands                  Clocks   Transfers  Bytes   Example
-                        22         2        1     CMPSB
(repeat)                9 + 22/rep   2/rep      1     REPE CMPSB

                                 Example                                  

The following example compares BUFFER1 against BUFFER2 for the first
mismatch.

cld                     ;Scan in the forward direction
mov     cx, 100         ;Scanning 100 bytes (CX is used by REPE)
lea     si, buffer1     ;Starting address of first buffer
lea     di, buffer2     ;Starting address of second buffer
repe    cmpsb           ;   ...and compare it.
jne     mismatch        ;The Zero Flag will be cleared if there
;   is a mismatch
match:          .               ;If we get here, buffers match
.
mismatch:
dec     si              ;If we get here, we found a mismatch.
dec     di              ;Back up SI and DI so they point to the
.               ;   first mismatch

Upon exit from the REPE CMPSB loop, the Zero Flag will be cleared if a
mismatch was found, and set otherwise. If a mismatch was found, DI and
SI will be pointing one byte past the byte that didn't match; the DEC
DI and DEC SI instructions backup these registers so they point to the
mismatched characters.

See also: CMP
See also: CMPS
See also: CMPSW
See also: SCAS
See also: REP
See also: CLD
See also: STD
See also: Flags

CMPSB Compare String Byte