简体   繁体   中英

Calculate the size of a string using inline assembler in C

I am quite bad at assembly, but I currently have an assignment using C and inline assembly using the VS2015 x86 native compiler. I need to calculate the size of a string given by parameter. Here is my approach:

void calculateLength(unsigned char *entry)
{
    int res;
    __asm {
        mov esi, 0
        strLeng:
            cmp [entry+ esi], 0
            je breakLength
            inc esi
            jmp strLeng
        breakLength:
        dec esi
        mov res, esi
    }
    printf("%i", res);
}

My idea was to increment the esi registry until the null character was found but, every time I get 8 as a result.

Help is appreciated!

I will be posting the corrected code, thanks a lot Jester for sorting it out

void calculateLength(unsigned char *entry) {
    int res;
    __asm {
        mov esi, 0
        mov ebx, [entry]
        strLeng:
            cmp [ebx + esi], 0
            je breakLength
            inc esi
            jmp strLeng
        breakLength:
        mov res, esi
    }
    printf("%i", res);
}

What was happening was that cmp [entry+ esi], 0 was comparing the pointer value + the index with zero and not the string content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM