EDI: Storing strings
EDI: store string
Al: store character x
Repne SCAs byte PTR es: [EDI]: traverses the string, and stops when encountering the character x every time ecx-1 is cycled
A classic way to calculate the length of a string in assembly is to use this instruction.
00406930 /$ 89FA mov edx,edi
00406932| 89c7 mov EDI, eax; EDI is the stored string
00406934 |. B9 ffffff mov ECX, - 0x1; ECX in - 1
00406939| 30c0xor Al, Al; Al = 0, will traverse all strings
0040693b | F2: AE repne SCAs byte PTR es: [EDI]; traverses the string in bytes, CX-1 every time
0040693D |. B8 FEFFFFFF mov eax,-0x2
00406942 | 29c8 sub eax, ECX; since the end of Cx is subtracted once more, use - 2 to subtract to get the real length
00406944 |. 89D7 mov edi,edx
Example:
#include<stdio.h> int main()
{ char str[] = "123456789"; int strCount=0; int c = 0;
_asm
{
lea edi,str
mov ecx,0xFFFFFFFF xor al,al
repne scas byte ptr es:[esi]
mov eax,0xFFFFFFFE sub eax,ecx
mov c,ecx
mov strCount,eax
}
printf("ecx=%d count = %d",c,strCount); return 0;
Results:
Put AL in the 0x34, try again, the program will be 3 cycles after 4, this time stop cycle, ecx=-5,count=3
Computes the string length using Repne scas byte ptr Es:[edi]