匯編:判斷兩個(gè)字符串是否相等(匹配)
;如相同,輸出‘match’;如不同,輸出‘no match’。
;經(jīng)修改的代碼能夠循環(huán)使用
DATAS SEGMENT
data1 DB 100 DUP(?)
data2 DB 100 DUP(?)
string DB 0DH,0AH,$
tital1 DB Please input the first string :,0DH,0AH,$
tital2 DB Please input the second string :,0DH,0AH,$
tital3 db Do you want to have a try again(if yes,input y,else input n ): ,0dh,0ah,$
result1 db match,0dh,0ah,$
result2 db no match,0dh,0ah,$
temp DW 0
DATAS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS
START: MOV AX,DATAS
MOV DS,AX
MOV si,0
tip1: ;輸出抬頭1
LEA dx,tital1
MOV ah,09h
INT 21H
first: ;輸入第一個(gè)字符串
MOV ah,01h ;輸入字符放在AL中
INT 21H ;以回車作為結(jié)束符
cmp al,0dh
je tip2
MOV data1[si],AL;保存在內(nèi)存中
INC si
JMP first
tip2:
LEA dx,string
MOV AH,09H
INT 21H
MOV temp,si ; 保存si使得下一個(gè)字符串和它做比較
MOV si,0 ;輸出抬頭2
LEA dx,tital2
MOV ah,09h
INT 21H
second: ;輸入第二個(gè)字符串
MOV AH,01H ;輸入字符放在AL中
INT 21H ;同樣以回車作為結(jié)束符
CMP AL,0DH
je output
mov data2[si],al ;保存到內(nèi)存
inc si
jmp second
output:
LEA dx,string
MOV AH,09H
INT 21H
CMP temp,si ;判斷長(zhǎng)度是否相等
JNE nomatch
MOV si,0
pipei:MOV AL,DATA1[si];判斷是否每個(gè)字符都相等
MOV AH,DATA2[si]
INC si
CMP AL,AH
JNE nomatch
CMP si,temp
JE MATCH
JMP pipei
MATCH: LEA DX,result1 ;不匹配就輸出結(jié)論1
MOV AH,09H
INT 21H
JMP EXIT
nomatch:LEA DX,result2 ;匹配就輸出結(jié)論2
MOV AH,09H
INT 21H
EXIT: ;詢問是否要繼續(xù)進(jìn)行
lea dx,tital3
mov ah,09h
int 21h
mov ah,01h
int 21h
cmp al,y
lea dx,string
mov ah,09h
int 21h
je start
cmp al,n
je over
jmp exit
over: ;結(jié)束
MOV AH,4CH
INT 21H
CODES ENDS
END START
評(píng)論