[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
CMPS             Compare String (Byte or Word)       Flags: O D I T S Z A P C
                                                            *       * * * * *
CMPS destination-string,source-string

          Logic:    CMP (DS:SI), (ES:DI)       ; Sets flags only
                    if DF = 0
                        SI . SI + n            ; n = 1 for byte, 2 for word
                        DI . DI + n
                    else
                        SI . SI - n
                        DI . DI - n

    This instruction compares two values by subtracting the byte or word
    pointed to by ES:DI, from the byte or word 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
                           byte(word)
   dest,source               22(30)       2        1     CMPS STR1,STR2
   (repeat) dest,source  9 + 22(30)/rep  2/rep     1     REPE CMPS STR1,STR2
  --------------------------------------------------------------------------

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

  -------------------------------- Example ---------------------------------

    Assuming the definition:

          buffer1 db      100 dup (?)
          buffer2 db      100 dup (?)

    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    cmps    buffer1,buffer2 ;...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 CMPS 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 backup these registers so they point to the mismatched
    characters.

See Also: CMP CMPSB CMPSW SCAS REP CLD STD Flags
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson