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

643ylb08  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(122)

我需要做这段代码,它显示一个单词是否是另一个单词的子串,两者都是从键盘读取的,首先是字符串,然后是我需要检查它是否是子串的单词。
问题是我输入它们,输出是:
无效。找到单词。未找到单词。
我试着检查第二个输入是否比第一个大,所以基本上,它会比较它们并显示消息:“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
qyuhtwio

qyuhtwio1#

为了澄清杰斯特已经说过的话:

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

相关问题