Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- TASM 2.x / MASM 6.x Assembly Language - <b>scas scan string (byte or word) flags: o d i t s z a p c</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
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
                    else
                        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
                           byte(word)
   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.

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

    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 SCASW CMP CMPS REP CLD STD Flags

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson