Catalina Cuellar
Catalina Cuellar

Reputation: 129

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!

Upvotes: 2

Views: 406

Answers (1)

Catalina Cuellar
Catalina Cuellar

Reputation: 129

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.

Upvotes: 5

Related Questions