汇编->十进制到十六进制数转换的程序实现

本文介绍了一个简单的程序,该程序能够将通过键盘输入的十进制数转换为屏幕上的十六进制形式。程序使用了三个子程序分别完成从十进制到二进制的转换、从二进制到十六进制的转换以及打印回车换行的功能。

;作者:课本教材
;时间:2007.11.14
;功能:十进制到十六进制数转换的程序实现
;主要实现:调用子程序来实现转换

 

;decihex-main program 
;convert 
decimal on keybd to hex on screen
;
*****************************************
decihex segment
    assume cs:decihex
    
start:
                    ;main proc far
repeate:
    call decibin    ;keybd to binary
    call crlf        ;print cr and lf
    call binihex    ;binary to hex on screen
    call crlf        ;print cr and lf
    jmp repeate        ;
do it again
;main endp


;
----------------------------------------------------
;procedure to convert dec on keybd to binary
;result 
is left in bx register

decibin proc near
    mov bx,
0        ;clear bx for number
newchar:
    mov ah,
1        ;keybd input 
    
int 21h            ;call dos
    sub al,30h        ;ASCII to binary
    jl exit            ;jump 
if<0
    cmp al,9d        ;
is it >9d?
    jg exit            ;yes,not dec digit
    cbw                ;
byte in al to word in ax
    
;(digit 
is now in ax)
;Multiply number 
in bx by 10 decimal

    xchg ax,bx        ;trade digit 
&number
    mov cx,10d        ;put 
10 dec in cx
    mul cx            ;number times 
10
    xchg ax,bx        ;trade number 
& digit
    
;Add digit 
in ax to number in bx
    add bx,ax        ;add digit to number
    jmp newchar        ;
get next digit

exit :
    ret                ;
return from decibin
decibin endp        ;end of decibin proc

;
-------------------------------------------------
;procedure to convert binary number 
in bx to hex on screen

binihex proc near
    mov ch,
4        ;number of digits
    
rorate:
    mov cl,
4        ;set count to bits
    rol bx,cl        ;left digit to right
    mov al,bl        ;mov to al
    and al,0fh        ;mask off left digit
    add al,30h        ;convert hex to ASCII    
    cmp al,3ah        ;
is it >9?
    jl printit        ;jump 
if digit =0 to 9
    add al,7h        ;digit 
is A to F
    
printit:
    mov dl,al        ;print ASCII 
char in DL
    mov ah,
2        ;display output funct
    
int 21h            ;call dos
    dec ch            ;done 
4 digits?
    jnz rorate        ;not yet
    ret                ;
return from binihex
binihex endp        



;
---------------------------------------------------
;procedure to print carriage 
return and linefeed

crlf proc near
    mov dl,0dh        ;carriage 
return 
    mov ah,
2        ;display function
    
int 21h            ;call dos
    mov dl,0ah        ;linefeed
    mov ah,
2        ;display function
    
int 21h            ;call dos
    ret             ;
return from crlf
crlf endp

;
---------------------------------------------------
decihex ends
;
---------------------------------------------------
    end start 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值