[英]How to compare two strings in x86 assembly with FASM?
I want to compare the user input stored in the register si
with another string. 我想将寄存器
si
存储的用户输入与另一个字符串进行比较。
By the way, I'm using FASM. 顺便说一下,我正在使用FASM。 Here's my code so far after the user input.
这是我用户输入后的代码。
If I'm using the repe cmpsb
command, I know that I have to use the extra segment, but I don't know how. 如果我正在使用
repe cmpsb
命令,我知道我必须使用额外的段,但我不知道如何。 And the repe cmpsb
command doesn't work with this code. 并且
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
For a simple program it's probably true that both strings will be stored in the same memory segment. 对于一个简单的程序,两个字符串可能都存储在同一个内存段中。 Just place the value from
DS
in ES
. 只需将
DS
中的值放在ES
。
...
push ds
pop es
mov di, info
...
And the
repe cmpsb
command doesn't work with this code.并且
repe cmpsb
命令不适用于此代码。
You've set the counter CX
to a fixed number of 20, where at least one of the strings ("info") has only 4 characters. 您已将计数器
CX
设置为固定数字20,其中至少有一个字符串(“info”)只有4个字符。 No wonder that the comparison fails. 难怪比较失败了。
Since you want to compare for equality, your first step will be to see if both strings have the same lengths. 由于您要比较相等性,因此您的第一步是查看两个字符串是否具有相同的长度。 If not, you already know the answer.
如果没有,你已经知道了答案。
If they are the same length, then use that as the counter CX
. 如果长度相同,则将其用作计数器
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.