实现功能如下:
1.只接收0~9,A~Z,a~z范围内的字符输入,该范围外内容输入会报错并进行重新输入
2.输出的二进制数有后缀B
DATA SEGMENT
MSG1 DB 'INPUT A NUMBER OF FOUR HEXADECIMAL DIGIT$'
MSG2 DB 'INPUT ILLEGAL$'
HOUZHUI DB 'B$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV CX,0504H ;高四位用于循环,低四位用于移位
LEA DX,MSG1 ;输出提示信息
MOV AH,09H
INT 21H
MOV DL,0DH ;回车
MOV AH,2
INT 21H
MOV DL,0AH ;换行
MOV AH,2
INT 21H
INPUT:
MOV AH,01H
INT 21H
DEC CH
CALL JUDGE
CMP AL,39H
JBE NEXT
SUB AL,07H
NEXT:
SUB AL,30H
SHL BX,CL
ADD BL,AL
CMP CH,0
JNE INPUT
MOV CX,16
;在输出二进制数前进行一次回车换行,美观
BACK:
MOV DL,0DH ;回车
MOV AH,2
INT 21H
MOV DL,0AH ;换行
MOV AH,2
INT 21H
MOV CX,16
OUTPUT:
MOV DL,0
ROL BX,1
ADC DL,30H ;DL+30+CF
MOV AH,02H ;输出DL中ASCII码对应的字符
INT 21H
LOOP OUTPUT
;在二进制输出完毕后进行后缀B输出
LEA DX,HOUZHUI
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
;---------------------------------------------------------
WRONGINPUT PROC NEAR
LEA DX,MSG2 ;错误提示输出
MOV AH,09H
INT 21H
MOV DL,0DH ;回车
MOV AH,2
INT 21H
MOV DL,0AH ;换行
MOV AH,2
INT 21H
RET
WRONGINPUT ENDP
;-----------------------------------------------------------
JUDGE PROC NEAR
CMP CH,0 ;最后一位输入,跳转
JE S2
CMP AL,30H ;如果小于0,报错
JB WRONG
CMP AL,39H ;如果大于9跳转,小于9则证明是数字,返回主程序
JA S0
RET
S0:
CMP AL,41H ;如果小于A且大于9,报错
JB WRONG
;此行以后AL中字符ASCII大于A
CMP AL,46H ;如果大于F继续跳转,小于F证明属于A~F,就返回主程序
JA S1
RET
S1:
CMP AL,61H ;如果大于F且小于a,报错
JB WRONG
;此行以后AL中字符ASCII码大于a
CMP AL,66H
JA WRONG ;如果大于f,报错
;此行以后字符属于a~f之间
SUB AL,20H
RET
WRONG:
CALL WRONGINPUT
JMP START
S2:
CMP AL,68H ;判断是否是h
JE BACK
CMP AL,48H ;判断是否是H
JE BACK
JMP WRONG
JUDGE ENDP
;------------------------------------------------------
CODE ENDS
END START