【问题标题】:Calculate the size of a string using inline assembler in C在 C 中使用内联汇编器计算字符串的大小
【发布时间】:2019-11-17 01:00:11
【问题描述】:

我很不擅长汇编,但我目前有一个使用 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 注册表直到找到空字符,但是每次我得到 8 作为结果。

感谢您的帮助!

【问题讨论】:

  • 你有什么问题?
  • 我真的不明白为什么我的回报总是8
  • 可能是因为您没有在循环内增加eax?或者可能是因为您在循环中使用了esi,但没有将其分配给eaxres
  • 字符串有多长? :D 请注意,您也不需要dec esicmp [entry+esi] 也可能处理指针而不是指向的字符串。因此,在循环将entry 加载到寄存器之前,例如mov ebx, [entry] 然后使用cmp [ebx+esi], 0。最好确保使用字节比较,所以cmp byte ptr [ebx+esi], 0
  • 我更改了cmp [entry+esi],它成功了。所以是的,它正在处理指针本身,而不是字符串

标签: c assembly inline-assembly


【解决方案1】:

我将发布更正后的代码,非常感谢 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 将指针值 + 索引与零而不是字符串内容进行比较。

【讨论】:

  • entrada 是什么?
  • @JL2210 可能应该是entry,但在复制编辑时忘记了。
猜你喜欢
  • 2012-02-25
  • 2014-01-10
  • 2021-03-03
  • 2021-03-04
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2019-10-11
相关资源
最近更新 更多