检查两个字符串在汇编中是否相等
Checking if two strings are equal in assembly
该程序是检查用户输入的密码是否与程序中直接指定的密码相匹配。无法理解为什么当我尝试直接从键盘输入时总是碰巧得到 'PASSWORD INCORRECT'。当直接在程序中指定 'SRC' 时,输出似乎是完美的。
.MODEL SMALL
.STACK 1000H
DISP MACRO MSG ;macro to display a string of characters
LEA DX,MSG
MOV AH,09H
INT 21H
ENDM
INPUT MACRO ;macro to input character by character
MOV AH,01H
INT 21H
ENDM
DATA SEGMENT
CR EQU 0DH
LF EQU 0AH
MSG DB 'ENTER YOUR PASSWORD PLEASE : ',CR,LF,'$'
TRU DB 'PASSWORD CORRECT$'
FAL DB 'PASSWORD INCORRECT$'
SRC DB 10 DUP('$')
DEST DB 'YO$'
LEN EQU ($-DEST)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:DATA
START: MOV AX,DATA
MOV DS,AX
MOV ES,AX
MOV SI,OFFSET SRC
MOV DI,OFFSET DEST
CLD
MOV CX,LEN
XOR BX,BX
DISP MSG
RE: INPUT
MOV [SI],AL
INC SI
INC BX
CMP AL,CR
JNE RE
CMP BX,CX ;if string lengths dont match then the strings are unequal
JNE L1
MOV SI,OFFSET SRC
REPE CMPSB
JNZ L1
L2: DISP TRU
JMP EXIT
L1: DISP FAL
EXIT: MOV AH,4CH
INT 21H
CODE ENDS
END START
您检查读取的字符是否为回车return 放置在之后 字符已写入SRC
缓冲区。因此,当您稍后比较这两个字符串时,SRC
将包含 DEST
不包含的 CR 字符。
也就是说,如果您输入 YO,您将得到 DEST = 'YO$'
、SRC = 'YO\r', and LEN = 3
。
这是有效的输入循环的修改版本(新代码为小写):
RE: INPUT
cmp al,CR
je got_input ; exit the loop if we read a CR character
MOV [SI],AL
INC SI
INC BX
jmp RE
got_input:
inc bx ; LEN includes the '$' character after 'YO', so increase bx by one to match that
CMP BX,CX ; if string lengths dont match then the strings are unequal
DATA SEGMENT
STR1 DB "ENTER FIRST STRING HERE ->$"
STR2 DB "ENTER SECOND STRING HERE ->$"
STR11 DB "FIRST STRING : ->$"
STR22 DB "SECOND STRING: ->$"
INSTR1 DB 20 DUP("$")
INSTR2 DB 20 DUP("$")
NEWLINE DB 10,13,"$"
N DB ?
S DB ?
MSG1 DB "BOTH STRING ARE SAME$"
MSG2 DB "BOTH STRING ARE DIFFERENT$"
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:MOV AX,DATA
MOV DS,AX
LEA SI,INSTR1
LEA DI,INSTR2
GET STRING
MOV AH,09H
LEA DX,STR1
INT 21H
MOV AH,0AH
MOV DX,SI
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,0AH
MOV DX,DI
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
PRINT THE STRING
MOV AH,09H
LEA DX,STR11
INT 21H
MOV AH,09H
LEA DX,INSTR1+2
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,09H
LEA DX,STR22
INT 21H
MOV AH,09H
LEA DX,INSTR2+2
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
STRING COMPARISION
MOV BX,00
MOV BL,INSTR1+1
MOV BH,INSTR2+1
CMP BL,BH
JNE L1
ADD SI,2
ADD DI,2
L2:MOV BL,BYTE PTR[SI]
CMP BYTE PTR[DI],BL
JNE L1
INC SI
INC DI
CMP BYTE PTR[DI],"$"
JNE L2
MOV AH,09H
LEA DX,MSG1
INT 21H
JMP L5
L1:MOV AH,09H
LEA DX,MSG2
INT 21H
L5:
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
Z:\SEM3\SS-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->PATEL
FIRST STRING : ->ARPIT
SECOND STRING: ->PATEL
BOTH STRING ARE DIFFERENT
Z:\SEM3\SS-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->ARPIT
FIRST STRING : ->ARPIT
SECOND STRING: ->ARPIT
该程序是检查用户输入的密码是否与程序中直接指定的密码相匹配。无法理解为什么当我尝试直接从键盘输入时总是碰巧得到 'PASSWORD INCORRECT'。当直接在程序中指定 'SRC' 时,输出似乎是完美的。
.MODEL SMALL
.STACK 1000H
DISP MACRO MSG ;macro to display a string of characters
LEA DX,MSG
MOV AH,09H
INT 21H
ENDM
INPUT MACRO ;macro to input character by character
MOV AH,01H
INT 21H
ENDM
DATA SEGMENT
CR EQU 0DH
LF EQU 0AH
MSG DB 'ENTER YOUR PASSWORD PLEASE : ',CR,LF,'$'
TRU DB 'PASSWORD CORRECT$'
FAL DB 'PASSWORD INCORRECT$'
SRC DB 10 DUP('$')
DEST DB 'YO$'
LEN EQU ($-DEST)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:DATA
START: MOV AX,DATA
MOV DS,AX
MOV ES,AX
MOV SI,OFFSET SRC
MOV DI,OFFSET DEST
CLD
MOV CX,LEN
XOR BX,BX
DISP MSG
RE: INPUT
MOV [SI],AL
INC SI
INC BX
CMP AL,CR
JNE RE
CMP BX,CX ;if string lengths dont match then the strings are unequal
JNE L1
MOV SI,OFFSET SRC
REPE CMPSB
JNZ L1
L2: DISP TRU
JMP EXIT
L1: DISP FAL
EXIT: MOV AH,4CH
INT 21H
CODE ENDS
END START
您检查读取的字符是否为回车return 放置在之后 字符已写入SRC
缓冲区。因此,当您稍后比较这两个字符串时,SRC
将包含 DEST
不包含的 CR 字符。
也就是说,如果您输入 YO,您将得到 DEST = 'YO$'
、SRC = 'YO\r', and LEN = 3
。
这是有效的输入循环的修改版本(新代码为小写):
RE: INPUT
cmp al,CR
je got_input ; exit the loop if we read a CR character
MOV [SI],AL
INC SI
INC BX
jmp RE
got_input:
inc bx ; LEN includes the '$' character after 'YO', so increase bx by one to match that
CMP BX,CX ; if string lengths dont match then the strings are unequal
DATA SEGMENT
STR1 DB "ENTER FIRST STRING HERE ->$"
STR2 DB "ENTER SECOND STRING HERE ->$"
STR11 DB "FIRST STRING : ->$"
STR22 DB "SECOND STRING: ->$"
INSTR1 DB 20 DUP("$")
INSTR2 DB 20 DUP("$")
NEWLINE DB 10,13,"$"
N DB ?
S DB ?
MSG1 DB "BOTH STRING ARE SAME$"
MSG2 DB "BOTH STRING ARE DIFFERENT$"
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:MOV AX,DATA
MOV DS,AX
LEA SI,INSTR1
LEA DI,INSTR2
GET STRING
MOV AH,09H
LEA DX,STR1
INT 21H
MOV AH,0AH
MOV DX,SI
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,0AH
MOV DX,DI
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
PRINT THE STRING
MOV AH,09H
LEA DX,STR11
INT 21H
MOV AH,09H
LEA DX,INSTR1+2
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,09H
LEA DX,STR22
INT 21H
MOV AH,09H
LEA DX,INSTR2+2
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
STRING COMPARISION
MOV BX,00
MOV BL,INSTR1+1
MOV BH,INSTR2+1
CMP BL,BH
JNE L1
ADD SI,2
ADD DI,2
L2:MOV BL,BYTE PTR[SI]
CMP BYTE PTR[DI],BL
JNE L1
INC SI
INC DI
CMP BYTE PTR[DI],"$"
JNE L2
MOV AH,09H
LEA DX,MSG1
INT 21H
JMP L5
L1:MOV AH,09H
LEA DX,MSG2
INT 21H
L5:
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
Z:\SEM3\SS-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->PATEL
FIRST STRING : ->ARPIT
SECOND STRING: ->PATEL
BOTH STRING ARE DIFFERENT
Z:\SEM3\SS-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->ARPIT
FIRST STRING : ->ARPIT
SECOND STRING: ->ARPIT