| |||||||||||||||||||||
| |||||||||||||||||||||
The scan string instructions are useful in searching for a particular value or a character in a string. They have one of the following forms:
The SCAS instruction only requires a destination string (pointed at by ES:DI) rather than both a source and a destination string. The source operand is the value in the AL (SCASB), AX (SCASW), or EAX (SCASD) register. The SCAS instruction compares the value in the accumulator (AL, AX, or EAX) against the value pointed at by ES:DI and then increments (or decrements) DI by one, two, or four. When the first form, SCAS des_string, is used the assembler will replace it by SCASB, SCASW, or SCASD depending on the size of the operand dest_string. The semantics of the instructions SCASB, SCASW, and SCASD are illustrated below:
The SCASB InstructionThe SCASB instruction compares the content of the AL register to the byte addressed by ES:[DI] by performing the subtraction operation AL - ES:[DI], setting the arithmetic flags based on the result of the subtraction. Then, DI is either incremented or decremented by 1 depending on the Direction Flag. After executing this instruction, it is possible to use any of the conditional jump instructions. The SCASW InstructionThe SCASW instruction compares the content of the AX register to the word addressed by ES:[DI] by performing the subtraction operation AX - ES:[DI+1:DI], setting the arithmetic flags based on the result of the subtraction. Then, DI is either incremented or decremented by 2 depending on the Direction Flag. The SCASD InstructionThe SCASD instruction compares the content of the EAX register to the double word addressed by ES:[DI] by performing the subtraction operation EAX - ES:[DI+3:DI], setting the arithmetic flags based on the result of the subtraction. Then, DI is either incremented or decremented by 4 depending on the Direction Flag. Similar to the compare string instructions, the conditional repeat prefixes REPE/REPZ and REPNE/REPNZ can be used with the scan string instructions. The next example illustrates the use of the scan string instructions in searching for a particular character in a string.
Note that in the above example, if the searched character is found, then DI will be pointing at the address of the first location it is found at. The following example can be used to skip initial blank characters in a string. It leaves DI pointing to the first non-blank character in the searched string.
|