汇编字是字符串问题的子字符串



我需要使这个代码显示,如果一个词是另一个的子字符串。它们都是从键盘上读取的,首先是字符串,然后是我需要检查它是否是子字符串的单词。

问题是我输入它们,输出是:

无效。词的发现。找不到字

我试着检查第二个输入是否大于第一个,所以基本上,它会比较它们并显示消息:"invalid."无论我写什么,输出都是一样的:"Invalid。词的发现。文字未找到。">

下面是我的完整代码:
.model small
.stack 200h
.data
prompt1 db "Input String: $"
prompt2 db 10,10, 13, "Input Word: $"
prompt3 db 10,10, 13, "Output: $"
found db "Word Found. $"
notfound db "Word Not Found. $"
invalid db 10,10, 13, "Invalid. $"
InputString db 21,?,21 dup("$")  
InputWord db 21,?,21 dup("$")
actlen db ?
.code
start:
mov ax, @data
mov ds, ax
mov es, ax
;Getting input string
mov ah,09h
lea dx, prompt1
int 21h
lea si, InputString
mov ah, 0Ah
mov dx, si
int 21h
;Getting input word
mov ah,09h
lea dx, prompt2
int 21h
lea di, InputWord
mov ah, 0Ah
mov dx, di
int 21h
;To check if the length of substring is shorter than the main string
mov cl, [si+1]
mov ch, 0
add si, 2
add di, 2
mov bl, [di+1]
mov bh, 0
cmp bx, cx
ja invalid_length
je valid
jb matching
valid:
cld
repe cmpsb
je found_display
jne notfound_display
mov     bp, cx      ;CX is length string (long)
sub     bp, bx      ;BX is length word  (short)
inc     bp
cld
lea     si, [InputString + 2]
lea     di, [InputWord + 2]
matching:
mov     al, [si]    ;Next character from the string
cmp     al, [di]    ;Always the first character from the word
je      check
continue:  
inc     si          ;DI remains at start of the word
dec     bp
jnz     matching    ;More tries to do
jmp     notfound_display
check:
push    si
push    di
mov     cx, bx     ;BX is length of word
repe cmpsb
pop     di
pop     si
jne     continue
jmp     found_display
again:
mov si, ax    
dec dx
lea di, InputWord
jmp matching

invalid_length:
mov ah, 09h
lea dx, invalid
int 21h

found_display:
mov dx, offset found
mov ah, 09h
int 21h

notfound_display:
mov dx, offset notfound
mov ah, 09h
int 21h

end start

澄清一下Jester已经说过的话:

invalid_length:
mov ah, 09h
lea dx, invalid
int 21h
found_display:
mov dx, offset found
mov ah, 09h
int 21h
notfound_display:
mov dx, offset notfound
mov ah, 09h
int 21h
end start

标签本身影响ip寄存器的操作。这是因为标签在运行时不存在;它们只是为程序员提供了方便。因此,您的计算机实际对上述代码所做的是:

invalid_length:
mov ah, 09h
lea dx, invalid
int 21h
mov dx, offset found
mov ah, 09h
int 21h
mov dx, offset notfound
mov ah, 09h
int 21h
end start
如果您只希望运行这三个中的一个,则需要在每个的末尾重定向ip。有几种方法可以做到这一点,这里有一种:
invalid_length:
mov ah, 09h
lea dx, invalid
int 21h
jmp done
found_display:
mov dx, offset found
mov ah, 09h
int 21h
jmp done
notfound_display:
mov dx, offset notfound
mov ah, 09h
int 21h
;fallthrough is intentional
done:
mov ax,4C00h
int 21h        ;exit program and return to DOS

最新更新