比较x86程序集中的字符数组 [英] Compare arrays of characters in x86 assembly

查看:113
本文介绍了比较x86程序集中的字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较2个字符串,以查看它们在汇编中是否相等.区分大小写.它们是从cpp包装器传递过来的.这是我到目前为止的内容:

I'm trying to compare 2 strings to see if they're equal in assembly. Case sensitive. They're passed in from a cpp wrapper. Here is what I have so far:

我在esi中存储了一个字符串,在edi中存储了另一个字符串:

I stored one string in esi, the other in edi:

LOOP:
    mov al, [esi + edx*4]
    mov bl, [edi + edx*4]
    sub al, bl
    INC edx
    je LOOP
    jmp END_LOOP  ; if it's not equal, do some stuff at the end

END_LOOP:

我做错了什么?怎么知道我何时处于char数组的结尾?

What am I doing wrong? How does it know when I'm at the end of a char array?

推荐答案

由于这些字符串是C ++字符串,而且我猜它们是由char而不是宽字符组成的,因此每个字符的大小为一个字节因此,您不应该将索引寄存器edx乘以4.此外,您不能在je指令之前在之前之前递增edx,因为只有跳转是因为edx达到零.

Since these strings are C++ strings, and I'm guessing they are made of chars and not wide characters, then each character is one byte in size so you should not be multiplying the index register edx by 4. Also, you cannot increment edx before immediately before your je instruction, because that only jumps is edx has been bumped to zero.

最后,字符串以0字节结尾.因此,您正在寻找albl为零,以知道何时停止循环.

Finally, the strings end with a 0 byte. So you are looking for either al or bl to be zero to know when to stop your loop.

您可以按照以下方式修改代码:

You can modify your code along these lines:

    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函数.有几种方法可以解决这个问题.

However I suggest just doing a search for string comparison in x86. There is a cmps instruction. You can even arrange to call the strncmp function if you like. There are several ways to go about this.

这篇关于比较x86程序集中的字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆