简体   繁体   English

使用 C 中的内联汇编程序计算字符串的大小

[英]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.我在组装方面很糟糕,但我目前有一个使用 C 的作业和使用 VS2015 x86 本机编译器的内联组装。 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.我的想法是增加 esi 注册表,直到找到 null 字符,但是每次我得到 8 作为结果。

Help is appreciated!帮助表示赞赏!

I will be posting the corrected code, thanks a lot Jester for sorting it out我将发布更正的代码,非常感谢 Jester 整理出来

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.发生的事情是cmp [entry+ esi], 0将指针值 + 索引与零进行比较,而不是字符串内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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