Assembly 如何使用FASM比较x86程序集中的两个字符串?

Assembly 如何使用FASM比较x86程序集中的两个字符串?,assembly,x86,fasm,Assembly,X86,Fasm,我想将存储在寄存器si中的用户输入与另一个字符串进行比较。 顺便说一下,我用的是FASM。这是用户输入后到目前为止我的代码。 如果我使用repe cmpsb命令,我知道我必须使用额外的段,但我不知道如何使用。并且repe cmpsb命令不适用于此代码 .input_done: cmp si, 0 je no_input jmp short .compare_input .compare_input: mov cx, 20 ;For the repe cmpsb co

我想将存储在寄存器
si
中的用户输入与另一个字符串进行比较。
顺便说一下,我用的是FASM。这是用户输入后到目前为止我的代码。
如果我使用
repe cmpsb
命令,我知道我必须使用额外的段,但我不知道如何使用。并且
repe cmpsb
命令不适用于此代码

.input_done:
   cmp si, 0
   je no_input
   jmp short .compare_input


.compare_input:
    mov cx, 20 ;For the repe cmpsb command.
    cld
    mov di, info ;The string I want to compare.
    mov es, di
    mov di, info
    repe cmpsb
    cmp cx, 0
    je showinfo
.showinfo:
    ... ;The output string if both string are the same.

info db "info", 0
对于一个简单的程序来说,两个字符串可能都存储在同一个内存段中。只需将
DS
中的值放入
ES

...
push ds
pop  es
mov  di, info
...

并且
repe cmpsb
命令不适用于此代码

.input_done:
   cmp si, 0
   je no_input
   jmp short .compare_input


.compare_input:
    mov cx, 20 ;For the repe cmpsb command.
    cld
    mov di, info ;The string I want to compare.
    mov es, di
    mov di, info
    repe cmpsb
    cmp cx, 0
    je showinfo
.showinfo:
    ... ;The output string if both string are the same.

info db "info", 0
您已将计数器
CX
设置为20的固定数字,其中至少一个字符串(“info”)只有4个字符。难怪这种比较会失败


由于要比较相等性,第一步是查看两个字符串是否具有相同的长度。如果没有,您已经知道答案。
如果长度相同,则将其用作计数器
CX

; String1 pointer in DS:SI, length in CX
; String2 pointer in ES:DI, length in DX

cmp  cx, dx
jne  NotEqual
repe cmpsb
jne  NotEqual
Equal:       ; .showinfo:
...          ; The output string if both string are the same.
; Don't fall through here!!!
NotEqual:
...

您不仅需要指定汇编器,还需要指定目标平台。例如,在linux中,这显然是错误的代码,因为它既不使用32b指针,也不使用64b指针。然后,如果您说这是DOS 16b,我们需要了解您是如何定义数据的,为了您自己添加好的问题,您应该尝试生成自己的代码,例如,您当前的代码确实设置了
di
两次,但不清楚
si
中的内容(或者设置它,或者添加清晰的注释)这里有一篇文章解释了16b实模式下的
segment:offset
寻址(为了让您更好地了解
es
ds
的用途):
repe
的结束状态也处理不正确。(通过
cmpcx,0
而不是
jcxz
或者更确切地说是测试
jne
状态来销毁它)。