.MODEL SMALL
.DATA
prompt1 DB 'Enter first hex digit (0-F): $'
prompt2 DB 'Enter second hex digit (0-F): $'
prompt3 DB 'Enter operation character (A-D): $'
result DB 'Result: $'
newline DB 13, 10, '$' ; 定义一个换行符
.CODE
START:
MOV AX, @DATA
MOV DS, AX
; 输入第一个十六进制数X
LEA DX, prompt1
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
SUB AL, '0'
CMP AL, 9
JBE SHORT STORE_X
SUB AL, 7 ; for characters A-F
STORE_X:
MOV BL, AL ; BL = X
; 输入第二个十六进制数Y
LEA DX, prompt2
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
SUB AL, '0'
CMP AL, 9
JBE SHORT STORE_Y
SUB AL, 7 ; for characters A-F
STORE_Y:
MOV BH, AL ; BH = Y
; 输入操作符A-D
LEA DX, prompt3
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
MOV CL, AL
; 执行操作
CMP CL, 'A'
JE ADD_OPERATION
CMP CL, 'B'
JE SUB_OPERATION
CMP CL, 'C'
JE MUL_OPERATION
CMP CL, 'D'
JE DIV_OPERATION
; 无效输入,退出程序
JMP END_PROGRAM
ADD_OPERATION:
MOV AL, BL
ADD AL, BH
JMP DISPLAY_RESULT
SUB_OPERATION:
MOV AL, BL
SUB AL, BH
JAE POSITIVE_DIFF
NEG AL
POSITIVE_DIFF:
JMP DISPLAY_RESULT
MUL_OPERATION:
MOV AL, BL
MUL BH
JMP DISPLAY_MUL_RESULT
DIV_OPERATION:
MOV AL, BL
XOR AH, AH
DIV BH
JMP DISPLAY_RESULT
DISPLAY_RESULT:
LEA DX, result
MOV AH, 09H
INT 21H
; 输出高位
MOV AH, 0
MOV AL, AL
SHR AL, 4
ADD AL, '0'
CMP AL, '9'
JBE SHORT OUTPUT_HIGH
ADD AL, 7 ; for characters A-F
OUTPUT_HIGH:
MOV DL, AL
MOV AH, 02H
INT 21H
; 输出低位
MOV AL, AH
AND AL, 0Fh
ADD AL, '0'
CMP AL, '9'
JBE SHORT OUTPUT_LOW
ADD AL, 7 ; for characters A-F
OUTPUT_LOW:
MOV DL, AL
MOV AH, 02H
INT 21H
; 输出换行符
LEA DX, newline
MOV AH, 09H
INT 21H
JMP END_PROGRAM
DISPLAY_MUL_RESULT:
LEA DX, result
MOV AH, 09H
INT 21H
; 输出高位
MOV AL, AH
AND AL, 0Fh
SHR AL, 4
ADD AL, '0'
CMP AL, '9'
JBE SHORT MUL_OUTPUT_HIGH
ADD AL, 7
MUL_OUTPUT_HIGH:
MOV DL, AL
MOV AH, 02H
INT 21H
; 输出低位
MOV AL, AH
AND AL, 0Fh
ADD AL, '0'
CMP AL, '9'
JBE SHORT MUL_OUTPUT_LOW
ADD AL, 7
MUL_OUTPUT_LOW:
MOV DL, AL
MOV AH, 02H
INT 21H
; 输出换行符
LEA DX, newline
MOV AH, 09H
INT 21H
END_PROGRAM:
MOV AH, 4CH
INT 21H
END START