繁体   English   中英

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

[英]Calculate the size of a string using inline assembler in C

我在组装方面很糟糕,但我目前有一个使用 C 的作业和使用 VS2015 x86 本机编译器的内联组装。 我需要计算参数给定的字符串的大小。 这是我的方法:

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);
}

我的想法是增加 esi 注册表,直到找到 null 字符,但是每次我得到 8 作为结果。

帮助表示赞赏!

我将发布更正的代码,非常感谢 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);
}

发生的事情是cmp [entry+ esi], 0将指针值 + 索引与零进行比较,而不是字符串内容。

暂无
暂无

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

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