X86-assembly/Instructions/cmpsb

From aldeid
Jump to navigation Jump to search
You are here:
cmpsb

Description

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.

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

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

Comments