CMPSW Compare String Word Flags: O D I T S Z A P C
* * * * * *
CMPSW
Logic: CMP (DS:SI), (ES:DI) ; Sets flags only
if DF = 0
SI SI + 2
DI DI + 2
else
SI SI - 2
DI DI - 2
This instruction compares two numbers by subtracting the word pointed
to by ES:DI, from the 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
- 30 2 1 CMPSW
(repeat) 9 + 30/rep 2/rep 1 REPE CMPSW
-------------------------------- Example ---------------------------------
The following example compares BUFFER1 against BUFFER2 for the first
mismatch.
cld ;Scan in the forward direction
mov cx, 50 ;Scanning 50 words (100 bytes)
lea si, buffer1 ;Starting address of first buffer
lea di, buffer2 ;Starting address of second buffer
repe cmpsw ; ...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 si ;Back up DI and SI so they point to the
dec di ; first mismatch
dec di
Upon exit from the REPE CMPSW 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 word (two bytes) past the word that didn't
match; the DEC DI and DEC SI pairs backup these registers so they
point to the mismatched characters.
Seealso:
This page last updated on Fri Nov 30 10:49:50 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster