【问题标题】:x86 assembly compare with null terminated arrayx86 程序集与空终止数组比较
【发布时间】:2023-03-15 13:40:01
【问题描述】:

我正在编写一个程序集函数,我需要计算一个以空字符结尾的数组中的字符。我正在使用视觉工作室。该数组是用 C++ 制作的,内存地址被传递给我的汇编函数。问题是一旦我达到 null (00),我的循环就不会结束。我曾尝试使用testcmp,但似乎比较的是 4 个字节而不是 1 个字节(字符的大小)。

我的代码:

_arraySize PROC              ;name of function

start:                  ;ebx holds address of the array
    push ebp            ;Save caller's frame pointer
    mov ebp, esp        ;establish this frame pointer
    xor eax, eax        ;eax = 0, array counter
    xor ecx, ecx        ;ecx = 0, offset counter

arrCount:                       ;Start of array counter loop

    ;test [ebx+eax], [ebx+eax]  ;array address + counter(char = 1 byte)
    mov ecx, [ebx + eax]        ;move element into ecx to be compared
    test ecx, ecx               ; will be zero when ecx = 0 (null)
    jz countDone
    inc eax                     ;Array Counter and offset counter
    jmp arrCount

countDone:

    pop ebp
    ret


_arraySize ENDP

如何只比较 1 个字节?我只是想转移我不需要的字节,但这似乎是在浪费一条指令。

【问题讨论】:

    标签: arrays assembly x86 compare


    【解决方案1】:

    如果要比较单个字节,请使用单字节指令:

    mov  cl, [ebx + eax]        ;move element to be compared
    test  cl, cl                ; will be zero when NUL
    

    (请注意,零字符是 ASCII NUL,而不是 ANSI NULL 值。)

    【讨论】:

    • 谢谢你,这么简单的修复,我现在对组装有了更好的理解。不知道我只能指定寄存器的 1 个字节,现在我想起来很有意义。
    • @soda:请通过投票并接受答案来感谢我。
    • 还不能投票,我没有足够的代表。刚刚知道如何选择答案。
    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多