[英]How to compare two strings in x86 assembly with 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 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
mov di, info ;The string I want to compare. mov es, di
对于一个简单的程序,两个字符串可能都存储在同一个内存段中。 只需将DS
中的值放在ES
。
...
push ds
pop es
mov di, info
...
并且
repe cmpsb
命令不适用于此代码。
您已将计数器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:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.