"Use of mnemonics" demonstrations
Use of rep scas

This test demonstrates the use of the scanning mnemonics, well SCASB anyway. Set a breakpoint for USEOF_REPSCAS and run the test. Single-step past the first instruction. Edi now contains the address of the string-to-scan. Make a data inspector using the menu item, "inspect, data by register, EDI".
1. first scan continues while the zero flag is not set. AL is given the character "b". The instruction therefore looks for the character "b" and stops when it is found. Note that edi is now one byte beyond the "b", and that the zero flag is set. Note also the value in ECX.
2. second scan continues while the zero flag is set. AL is given the character "a". The instruction therefore continues while "a" continues to be found and then stops. Not that edi is now one byte beyond the first non-"a" character found, and that the zero flag is clear. Note also the value in ECX.
3. he next test AL is given the character "z", but this drops out after one iteration because "z" is not found. ECX therefore returns 5 and EDI has incremented by 1.
4. the next test AL is again given the character "z" but this time the scan drops out only after ECX is exhausted. Note in particular that the REPNZ has dropped out while the zero flag is not set. This indicates that it must have dropped out because ECX was exhausted. Be careful to use both the zero flag and ECX to check results if necessary.
5. the next test AL is given the character "a" but ECX is only 5. This REPZ scan drops out with ECX=0, but also with the zero flag clear. Don't let your code rely on ECX to see if a non-"a" character was found. Test the zero flag instead.
6. the last test AL is given the character "b" but ECX is only 5. This REPNZ scan drops out with ECX=0, but also with the zero flag set. Don't let your code rely on ECX to see if a "b" character was found. Test the zero flag instead.

Here is the actual code used:- ;

DATA
;
SCASTEST DB 'aaaabbbbccccdddd'
;
CODE
;
USEOF_REPSCAS:
MOV EDI,OFFSET SCASTEST
MOV ECX,6D
MOV AL,'b'
REPNZ SCASB
MOV EDI,OFFSET SCASTEST
MOV ECX,6D
MOV AL,'a'
REPZ SCASB
MOV EDI,OFFSET SCASTEST
MOV ECX,6D
MOV AL,'z'
REPZ SCASB
MOV EDI,OFFSET SCASTEST
MOV ECX,6D
MOV AL,'z'
REPNZ SCASB
MOV EDI,OFFSET SCASTEST
MOV ECX,5D
MOV AL,'a'
REPZ SCASB
MOV EDI,OFFSET SCASTEST
MOV ECX,5D
MOV AL,'b'
REPNZ SCASB
RET