SCAS             Scan String (Byte or Word)          Flags: O D I T S Z A P C
*       * * * * *
SCAS destination-string

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

This instruction compares the accumulator (AL or AX) to the byte or
word pointed to by ES:DI. SCAS sets the flags according to the results
of the comparison; the operands themselves are not altered. After the
comparison, DI is 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
dest-str                  15(19)       1        1     SCAS WORD_TABLE
(repeat) dest-str     9 + 15(19)/rep 1/rep      1     REPNE SCAS BYTE_TABLE

Notes:         This instruction is always translated by the
assembler into either SCASB, Scan String Byte, or
SCASW, Scan String Word, depending upon whether
destination-string refers to a string of bytes or
words. In either case, however, you must explicitly
load the DI register with the offset of the string.

SCAS is useful for searching a block for a given
byte or word value. Use CMPS, Compare String, if you
wish to compare two strings (or blocks) in memory,
element by element.


Assuming the definition:

LOST_A   DB   100  DUP(?)

the following example searches the 100-byte block starting at LOST_A
for the character 'A' (65 decimal).

MOV     AX, DS
MOV     ES, AX          ;SCAS uses ES:DI, so copy DS to ES
CLD                     ;Scan in the forward direction
MOV     AL, 'A'         ;Searching for the lost 'A'
MOV     CX,100          ;Scanning 100 bytes (CX is used by REPNE)
LEA     DI, LOST_A      ;Starting address to DI
REPNE   SCAS    LOST_A          ;   ...and scan it.
JE      FOUND           ;The Zero Flag will be set if we found
;   a match.
NOTFOUND:       .               ;If we get here, no match was found
FOUND:  DEC     DI              ;Back up DI so it points to the first
.               ;   matching 'A'

Upon exit from the REPNE SCAS loop, the Zero Flag will be set if a
match was found, and cleared otherwise. If a match was found, DI will
be pointing one byte past the match location; the DEC DI at FOUND
takes care of this problem.

See also: SCASB
See also: SCASW
See also: CMP
See also: CMPS
See also: REP
See also: CLD
See also: STD
See also: Flags

SCAS Scan String (Byte or Word)