Computes the string length using Repne scas byte ptr Es:[edi]

Source: Internet
Author: User


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]


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.