0

编写汇编程序以用作字典。用户输入单词,程序检查该单词是否存在。TASM,16 位。程序非常适合数组的前两个元素,但如果我提供数组balls.的第 3 个元素,即使选择了数组的下一个元素,(在 上验证emu8086bx变为 007ch,->参考代码<-),repe cmpsb一次尝试后仍然完成检查。与数组的前两个元素一起正常工作。这是我的代码程序首先检查长度,然后检查位。提供句点 ( . )时结束长度检查。

.model large
.data
arrayele db 00d ;to count if all elements of the array have been compared with
count db 00d ;length of input count
nl db 10d,13d,'$' ;newline
mne db "Not Equal$" ;message if not equal
me db "Equal$" ;message if equal
buf db 99,?,99 dup(?) ;buffer where the input will be saved
w0 db "hello$" ;word 0-5
w1 db "which$"
w2 db "balls$"
w3 db "table$"
w4 db "chair$"
w5 db "apples$"
words dw offset w0,offset w1 ;the array
      dw offset w2,offset w3
      dw offset w4,offset w5
.code

main proc
    mov ax,@data
    mov ds,ax
    mov es,ax

    ;take user input
    mov ah,0ah
    mov dx,offset buf
    int 21h

    ;print new line
    mov ah,09h
    mov dx,offset nl
    int 21h

    ;load input to di
    mov di,offset buf
    add di,2

    ;//saving length to a variable
    repeat:
    mov al,[di]
    inc count
    cmp al,"."
    je lenchck
    inc di
    jmp repeat
    ;//end saving
    lenchck: 

    dec count  ;as full stop (period) (.) is also included in the count

    stringmatch1:
    mov cx,0  ;reset register
    mov arrayele,0 ;reset variable
    stringmatch: 
    mov di,offset buf  ;loading input to di
    add di,2
                          ;loading array element to si
    mov bx,0
    mov bl,byte ptr words
    mov si,bx             ;end loading array element to si
    mov cl,count
    repe cmpsb
    je equal

    inc arrayele
    inc words    ;next word in the array
    mov bx,0         ;loading it
    mov bl,byte ptr words
    mov si,bx        ;end loading
    cmp arrayele,06d      ;compare to check if all elements have been compared with
    jg wrong

    jmp stringmatch

    wrong:                ;load notequal message
    mov dx,offset mne
    jmp print

    equal:
    mov dx,offset me      ;load equal message
    print:
    mov ah,09h            ;print it    
    int 21h

    mov ah,4ch            ;exit the program
    int 21h
main endp
end main
4

1 回答 1

1

inc words ;next word in the array

不,对不起。您已经增加(什么是) 的偏移量w0。您还没有移动到数组中的下一个单词。你想要的东西更像...

mov bx, offset words
top:
; get offset of a string
mov si, [bx]
; do your comparison
; start at beginning of input every time
mov di, offset buf + 2 ; don't need a separate operation
mov cl, count ; (are we sure ch is clear?)
repe cmpsb
je found
; no? get next offset in words array
add bx, 2
jmp top

在你寻找'.'的循环中,如果讨厌的用户没有输入'.'会发生什么?中断返回后,输入缓冲区中的第二个字节 (buf + 1) 是实际输入的计数。它包括“。” (如果有)和结束输入的回车(13 或 0Dh)。您可以使用此信息来排除明显不正确的输入。

于 2013-10-08T00:00:01.053 回答