汇编实现10进制转16进制

2018-05-13  本文已影响0人  HughDong

程序运行后,在第一行提示"Please Enter a Decimal Number:"
接收从键盘键入的一个介于0~65535之间的十进制数,以非数字字符作为输入结束标志;
将此十进制数转化成二进制数,存放在BX寄存器中;
若此数为0,则退出程序;若此数不为0,在下一行显示"The Hexidecimal is "和它的十六进制格式表示值;
返回程序开始处,再次显示输入提示;
十进制数输入过程/十六进制数显示过程/回车换行用子程序实现.


程序代码

.model small
.data
array1 db 5 dup(0)
msg1   db "Please Enter a Decimal Number:",'$'
msg2   db "The Hexidecimal is ",'$'
.code
.startup
    JMP print_str
    MOV AX,@DATA
    MOV DS,AX
print_str:;打印第一行字
    LEA DX,msg1
    MOV AH,9
    INT 21H
    MOV BX,0
    JMP input_num
input_num:;输入数字
    MOV AH,1
    INT 21H
    MOV AH,0
    MOV CL,0
    JMP judge1
judge1:;判断ascii>30H(0)
    CMP AL,30H
    JNC judge2
    JMP done
judge2:;判断ascii<=3AH(9)
    CMP AL,3AH
    JC  save_num
    JMP done
save_num:;将输入的数字拼成整数
    INC CH
    MOV CL,AL
    SUB CL,30H
    MOV AX,BX
    MOV DX,10
    MUL DX
    MOV BX,AX
    ADD BL,CL
    MOV AX,0
    CMP CH,5
    JB input_num
    JMP done
convert_num:;10->16进制转换
    MOV DX,0
    MOV AX,BX
    MOV CX,10H
    DIV CX
    MOV BX,AX
    CMP DX,0AH
    JB ascii_num
    JMP ascii_alphabet
ascii_num:
    ADD DX,30H
    JMP reverse_num
ascii_alphabet:
    ADD DX,37H
    JMP reverse_num
reverse_num:;余数逆序存储进数组,5,4,3,2,1
    DEC SI
    MOV [SI],DL
    CMP BX,0
    JBE done_arr
    JMP convert_num
done_arr:
    LEA SI,array1
    JMP print_arr
print_arr:;打印数组
    ADD SI,1
    CMP Byte PTR [SI],0H
    JE print_arr
    MOV AH,2
    MOV DL,Byte PTR [SI]
    INT 21H
    CMP SI,offset array1+4
    JE quit
    JMP print_arr
done:;输出第二行文字并准备数字转换
    CMP BX,0
    JBE quit
    MOV AH,02H
    MOV DL,0DH
    INT 21H
    MOV AH,02H
    MOV DL,0AH
    INT 21H
    LEA DX,msg2
    MOV AH,9
    INT 21H
    LEA SI,array1
    ADD SI,5
    JMP convert_num
quit:
.exit
end
上一篇下一篇

猜你喜欢

热点阅读