0

我正在尝试使用 x86 程序集编写一个可以在文本中搜索单词的程序。当文本中出现单词时,程序将通知用户。我在比较字符串时仍然遇到问题。有什么建议吗?

.model small

.stack 200h

.data

message1 db "Enter your text here: $"
text db 150,151 dup(0)
message2 db 10,13,"Enter the word that you want to find: $"
find db 20,21 dup(0)
yesmessage db 10,13,"The word is in the text$"
nomessage db 10,13,"Sorry the word is not in the text$"

.code
Start:

;Display message and key in strings
mov ax,seg message1
mov ds,ax

mov si,offset text
mov di,offset find

mov dx,offset message1
mov ah,09h
int 21h

mov dx,si
mov ah,0Ah
int 21h

mov ax,seg message2
mov ds,ax

mov dx,offset message2
mov ah,09h
int 21h

mov dx,di
mov ah,0Ah
int 21h

;compare strings
mov bx,00

mov bl,text+1
mov bh,find+1

cmp bl,bh
jne L1

add si,2
add di,2

L2:mov bl,byte ptr[si]
cmp byte ptr[di],bl
jne L1
inc si
inc di
cmp byte ptr[di],"$"
jne L2
mov ah,09h
mov dx,offset yesmessage
int 21h
L1:mov ah,09h
mov dx,offset nomessage
int 21H

mov ax,4c00h
int 21h
end start

预期的结果应该是:

示例 1:

在此处输入您的文字:他老了

输入您要查找的单词:old

这个词在文本中

示例 2:

在此处输入您的文字:他老了

输入您要查找的单词:年轻

对不起,这个词不在文本中
4

1 回答 1

0

我可以在您的代码中看到几个明显的问题。请参阅下面的评论:

mov bx,00   ; this instruction is redundant

mov bl,text+1
mov bh,find+1

cmp bl,bh
jne L1   ; it's quite likely that the strings won't have the same length,
         ; i.e. that find will be shorter than text. this condition is 
         ; therefore incorrect. it would make more sense to use jl, i.e.
         ; jumping to the negative print if text is shorter than find.


mov ah,09h
mov dx,offset yesmessage
int 21h
L1:mov ah,09h 
mov dx,offset nomessage  ; you'll be printing both messages in cases where
int 21H                  ; the substring is found, because you don't have
                         ; any jump that skips past it.   
于 2013-04-10T09:18:46.460 回答