【发布时间】:2013-10-07 21:53:48
【问题描述】:
编写汇编程序以用作字典。用户输入单词,程序检查该单词是否存在。 TASM,16 位。程序非常适合数组的前两个元素,但是如果我提供数组的第三个元素 balls. ,即使正在选择数组的下一个元素,(在 emu8086 上验证,bx 变为 @987654324 @,-> 参考代码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
【问题讨论】:
-
字典将超过近 200 个单词,所以是的,数组是必须的,别无他法:)
-
buf db 99,?,99 dup(?)的目的是什么(与buf db 99 dup(?)相比)? -
@us2012 用于输入,必须这样做,否则程序不会输入