最近因为比较忙啊,好久没有来论坛了,今天忽然发现10天前放了一个问题在这,谢谢各位,问题已近得到解决。
不过这个程序还是有问题的,在判断的时候应该先判断两个字符串的长度是否相同,因为如果两个字符串的长度不一,可是短字符串和长字符串的前面相等个数的字符相同,这个程序就无能为力了。以下是改进版:
data_seg segment
match db 'MATCH','$'
nmatch
db 'NOT MATCH','$'
str_one db 'please input your string(end with ENTER):','$'
str_two db 'please input your string again:','$'
next_line db 0dh,0ah,'$'
input_one db 50,?,50 dup(?)
input_two db 50,?,50 dup(?)
char_num_1 db 0
char_num_2 db 0
;-----------存放两次输入字符的个数,如果不等,可直接判断NOT MATCH
data_seg ends
stack_seg segment
db 256 dup(0)
stack_seg ends
code_seg segment
assume cs:code_seg,ds:data_seg,ss:stack_seg
start:
mov ax,data_seg
mov ds,ax
mov es,ax
lea dx,str_one
mov ah,9h
int 21h
lea bx,input_one
call input
lea bx,char_num_1
mov [bx],di
;------------------------
lea dx,next_line
mov ah,9h
int 21h
lea dx,str_two
mov ah,9h
int 21h
lea bx,input_two
call input
lea bx,char_num_2
mov [bx],di
lea dx,next_line
mov ah,9h
int 21h
;------------------------
lea si,input_one
lea di,input_two
call str_cmps
mov ah,4ch
int 21h
;子程序功能:从键盘输入一字符串
;入口参数
:bx,存放缓冲区手地址 ,di寻址
;出口参数
:无
input PROC near
push ax
push bx
xor di,di
mov ah,01h
input_char:
int 21h
mov byte ptr [bx+di],al
inc di
cmp al,0dh
jnz input_char
pop bx
pop ax
ret
input ENDP
;子程序功能:比较两字符串,相同输出MATCH,否则输出NOT MATCH
;入口参数
:si存放源字符串地址,di存放目标字符串地址(即第二次输入的字符串)
;出口参数
:无
str_cmps
proc near
push ax
push cx
push dx
lea bx,char_num_1
mov cl,byte ptr [bx]
lea bx,char_num_2
mov ch,byte ptr [bx]
mov al,cl
cmp cl,ch
jnz false
;-----------------------如果两次输入的字符数不相等,直接判断
mov cl,al
xor ch,ch
cld
repe cmpsb
jz success
jnz false
last_thing:
pop dx
pop cx
pop ax
ret
success:
mov dx,offset match
mov ah,9
int 21h
jmp last_thing
false:
mov dx,offset nmatch
mov ah,9
int 21h
jmp last_thing
str_cmps endp
code_seg ENDS
END start