由于这些字符串是 C++ 字符串,而且我猜它们是由char
s 而不是宽字符组成的,所以每个字符的大小都是一个字节,所以你不应该将索引寄存器乘以edx
4。此外,你不能edx
在紧接之前递增您的je
指示,因为只有跳转是 edx 已被撞到零。
最后,字符串以 0 字节结尾。因此,您正在寻找al
或bl
为零以知道何时停止循环。
您可以按照以下几行修改您的代码:
xor edx, edx ; make sure edx is 0 to start with
LOOP:
mov al, [esi + edx]
mov bl, [edi + edx]
inc edx ; prepare for next char
cmp al, bl ; compare two current characters
jne DIFFERENT ; not equal, get out, you are DONE!
cmp al, 0 ; equal so far, are you at the end?
je SAME ; got to end of both strings, you're good, get out
jmp LOOP ; okay well they agree so far, go to next char
DIFFERENT:
; Do what you need to do for the strings being different
;
;
jmp DONE
SAME:
; Do what you need to do for the strings being the same
;
;
DONE:
但是我建议只在 x86 中搜索字符串比较。有一个cmps
指令。如果您愿意,您甚至可以安排调用该strncmp
函数。有几种方法可以解决这个问题。