汇编语言程序设计-十进制转换为十六进制的程序实现

时间:2022-10-21 19:26:49
       
DEC2HEX     SEGMENT
                 ASSUME CS:DEC2HEX
MAIN             PROC FAR
REPEAT:       CALL DEC2BIN   ;十进制转换为二进制
                 CALL CRLF     ;回车与换行
                 CALL BIN2HEX   ;二进制转换为十六进制
                 CALL CRLF
                 JMP REPEAT
MAIN             ENDP
;十进制转换二进制,结果放在寄存器BX中
DEC2BIN       PROC NEAR
                 MOV BX,0
KEYIN:           MOV AH,1
                 INT 21H
                 SUB AL,30H
                 JL EXIT
                 CMP AL,9
                 JG EXIT
                 CBW
                 XCHG AX,BX
                 MOV CX,10
                 MUL CX
                 XCHG AX,BX
                 ADD BX,AX
                 JMP KEYIN
EXIT:  RET
DEC2BIN       ENDP
;二进制转换为十六进制,把BX中的值按高到低先后,以4个二进制位为一个16进制数,搭配成ASCII码输出
BIN2HEX       PROC NEAR
                 MOV CH,4
ROTATE:       MOV CL,4
                 ROL BX,CL
                 MOV AL,BL
                 AND AL,0FH
                 ADD AL,30H
                 CMP AL,3AH
                 JL PRINT
                 ADD AL,07H
PRINT:           MOV DL,AL
                 MOV AH,2
                 INT 21H
                 DEC CH
                 JNZ ROTATE
         RET
BIN2HEX       ENDP
;回车与换行
CRLF            PROC NEAR
                 MOV DL,0DH
                MOV AH,2
                 INT 21H
                 MOV DL,0AH
                 MOV AH,2
                 INT 21H
                 RET
CRLF             ENDP
;
DEC2HEX     ENDS
                 END MAIN