.MODEL SMALL
.STACK 100h
.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: $'
.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 ; 用于字符 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 ; 用于字符 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
MOV AH, 0 ; 确保上位清零
JMP DISPLAY_RESULT
SUB_OPERATION:
MOV AL, BL
SUB AL, BH
JAE POSITIVE_DIFF
NEG AL
POSITIVE_DIFF:
MOV AH, 0 ; 确保上位清零
JMP DISPLAY_RESULT
MUL_OPERATION:
MOV AL, BL
MUL BH
; 结果存储在 AX 中
JMP DISPLAY_RESULT_AX
DIV_OPERATION:
MOV AL, BL
XOR AH, AH
DIV BH
MOV AH, 0 ; 确保上位清零
JMP DISPLAY_RESULT
DISPLAY_RESULT:
LEA DX, result
MOV AH, 09H
INT 21H
; 显示结果
MOV AH, 02H
MOV DL, AL
; 显示高半字节
MOV AL, DL
AND AL, 0F0h
SHR AL, 4
CALL PRINT_HEX
; 显示低半字节
MOV AL, DL
AND AL, 0Fh
CALL PRINT_HEX
JMP END_PROGRAM
DISPLAY_RESULT_AX:
LEA DX, result
MOV AH, 09H
INT 21H
; 显示高字节(AX 的高半字节)
MOV DL, AH
AND DL, 0F0h
SHR DL, 4
CALL PRINT_HEX
; 显示低字节(AX 的低半字节)
MOV DL, AH
AND DL, 0Fh
CALL PRINT_HEX
; 显示低字节(AX 的低位)
MOV DL, AL
AND DL, 0F0h
SHR DL, 4
CALL PRINT_HEX
MOV DL, AL
AND DL, 0Fh
CALL PRINT_HEX
JMP END_PROGRAM
PRINT_HEX:
ADD AL, '0'
CMP AL, '9'
JBE SHORT PRINT_CHAR
ADD AL, 7
PRINT_CHAR:
MOV DL, AL
MOV AH, 02H
INT 21H
RET
END_PROGRAM:
MOV AH, 4CH
INT 21H
END START