Scan String Instructions 

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:

SCAS dest_string
SCASB
SCASW
SCASD

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:

InstructionDescription
SCASB Affect the flags based on the result of AL-ES:[DI] ;
IF (DF=0)
DI=DI+1
ELSE
DI=DI-1
SCASW Affect the flags based on the result of AX-ES:[DI+1:DI] ;
IF (DF=0)
DI=DI+2
ELSE
DI=DI-2
SCASD Affect the flags based on the result of EAX-ES:[DI+3:DI] ;
IF (DF=0)
DI=DI+4
ELSE
DI=DI-4

The SCASB Instruction

The 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 Instruction

The 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 Instruction

The 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.

Example: Character search in a string.
	
	MOV DI, offset str
	PUSH DS
	POP ES
	CLD			;left to right or auto-increment mode 
	MOV CX, 12		;string length
	MOV AL, 'A'		;character to be searched is A
REPNE  SCASB  			;cmp till equal or cx=0
	JNE Not_Found
	DEC DI
	...

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.

Example: Skip blank characters in a string.
	
	MOV DI, offset str
	PUSH DS
	POP ES
	CLD			;left to right or auto-increment mode 
	MOV CX, 12		;string length
	MOV AL, ' '		;character to be skipped  is blank
REPE   SCASB  			;cmp till not equal or cx=0
	JE No_Blank
	DEC DI
	...