汇编实验:串比较和 DOS / BIOS 调用

要求:输入用户名(回显)和密码(不回显),判断与预设的用户名和密码是否相同,如果相同则登陆成功,否则登陆失败。

踩坑:程序初始化的时候需要将 ES 的地址赋值给 ES 寄存器,才能正确使用 CMPSB 指令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
DATAS SEGMENT
U_ORI DB 'root'
U_LEN DB $-U_ORI
P_ORI DB '123456'
P_LEN DB $-P_ORI
U_BUF DB 250 DUP(0)
P_BUF DB 250 DUP(0)
P_CNT DB 0
U_TIP DB 'username: $'
P_TIP DB 'password: $'
SUC DB 'Login successfully, welcome!', 0AH, '$'
REJ DB 'Infomation does not match, you are rejected!', 0AH, '$'
STAR DB '*$'
D_LOG DB 'DEBUG LOG', 0AH, '$'
DATAS ENDS
CODES SEGMENT
ASSUME CS: CODES, DS: DATAS, ES: DATAS
START:
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
MOV DX, OFFSET U_TIP
CALL PRT_STR
CALL U_INP
CALL PRT_CR
MOV DX, OFFSET P_TIP
CALL PRT_STR
CALL P_INP
CALL PRT_CR
CALL CMP_INFO
MOV AH, 4CH
INT 21H
DEBUG_LOG:
MOV AH, 09H
MOV DX, OFFSET D_LOG
INT 21H
MOV AH, 4CH
INT 21H
; function: to print a string
PRT_STR PROC
MOV AH, 09H
INT 21H
RET
PRT_STR ENDP
; function: to print a new line
PRT_CR PROC
MOV AH, 02H
MOV DL, 0AH
INT 21H
RET
PRT_CR ENDP
; function: to input username
U_INP PROC
MOV AL, 200
MOV U_BUF, AL
MOV AH, 0AH
MOV DX, OFFSET U_BUF
INT 21H
RET
U_INP ENDP
; function: to input password
P_INP PROC
MOV BX, OFFSET P_BUF
CIRC:
MOV AH, 07H
INT 21H
CMP AL, 0DH
JZ NEXT
MOV [BX], AL
INC BX
INC P_CNT
MOV DX, OFFSET STAR
CALL PRT_STR
JMP CIRC
NEXT:
RET
P_INP ENDP
; function: to compare information
CMP_INFO PROC
MOV AL, U_BUF+1
CMP AL, U_LEN
JNE S_NEQ
MOV AL, P_CNT
CMP AL, P_LEN
JNE S_NEQ
MOV SI, OFFSET U_BUF+2
MOV DI, OFFSET U_ORI
CLD
MOV CL, U_LEN
MOV CH, 0
REPE CMPSB
JNE S_NEQ
MOV SI, OFFSET P_BUF
MOV DI, OFFSET P_ORI
CLD
MOV CL, P_LEN
MOV CH, 0
REPE CMPSB
JNE S_NEQ
MOV DX, OFFSET SUC
CALL PRT_STR
RET
S_NEQ:
MOV DX, OFFSET REJ
CALL PRT_STR
RET
CMP_INFO ENDP
CODES ENDS
END START