新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > 匯編:判斷兩個字符串是否相等(匹配)

匯編:判斷兩個字符串是否相等(匹配)

作者: 時間:2016-12-01 來源:網絡 收藏
;編程從鍵盤輸入兩個字符串到內存緩沖區(qū),并比較兩個字符串是否相同;
;如相同,輸出‘match’;如不同,輸出‘no match’。
;經修改的代碼能夠循環(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: ;輸入第一個字符串
MOV ah,01h ;輸入字符放在AL中
INT 21H ;以回車作為結束符
cmp al,0dh
je tip2
MOV data1[si],AL;保存在內存中
INC si
JMP first
tip2:
LEA dx,string
MOV AH,09H
INT 21H
MOV temp,si ; 保存si使得下一個字符串和它做比較
MOV si,0 ;輸出抬頭2
LEA dx,tital2
MOV ah,09h
INT 21H
second: ;輸入第二個字符串
MOV AH,01H ;輸入字符放在AL中
INT 21H ;同樣以回車作為結束符
CMP AL,0DH
je output
mov data2[si],al ;保存到內存
inc si
jmp second
output:
LEA dx,string
MOV AH,09H
INT 21H
CMP temp,si ;判斷長度是否相等
JNE nomatch
MOV si,0
pipei:MOV AL,DATA1[si];判斷是否每個字符都相等
MOV AH,DATA2[si]
INC si
CMP AL,AH
JNE nomatch
CMP si,temp
JE MATCH
JMP pipei
MATCH: LEA DX,result1 ;不匹配就輸出結論1
MOV AH,09H
INT 21H
JMP EXIT
nomatch:LEA DX,result2 ;匹配就輸出結論2
MOV AH,09H
INT 21H
EXIT: ;詢問是否要繼續(xù)進行
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: ;結束
MOV AH,4CH
INT 21H
CODES ENDS
END START


關鍵詞: 匯編字符串匹

評論


技術專區(qū)

關閉