x86 程序集与空终止数组的比较



>我正在处理一个汇编函数,我需要计算以空结尾的数组中的字符。我正在使用Visual Studio。数组是在C++中制作的,内存地址被传递给我的程序集函数。问题是一旦我达到空值 (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 个字节?我只是想移动我不需要的字节,但这似乎是浪费指令。

如果要

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

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

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

最新更新