簡體   English   中英

匯編字是substring的一個字符串問題

[英]Assembly word is substring of a string problem

我需要制作這段代碼,如果一個單詞是另一個單詞的 substring,它就會顯示出來。 兩者都是從鍵盤讀取的,首先是字符串,然后是我需要檢查它是否為 substring 的單詞。

問題是我輸入了它們,而 output 是:

無效的。 找到的話。 找不到單詞。

我試着檢查第二個輸入是否比第一個大,所以基本上,它會比較它們並顯示消息:“無效”。 無論我寫什么 output 都是一樣的:“無效。找到單詞。找不到單詞。”

這是我的完整代碼:

.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

澄清傑斯特已經說過的話:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM