;作者:课本教材
;时间: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
本文介绍了一个简单的程序,该程序能够将通过键盘输入的十进制数转换为屏幕上的十六进制形式。程序使用了三个子程序分别完成从十进制到二进制的转换、从二进制到十六进制的转换以及打印回车换行的功能。
1万+

被折叠的 条评论
为什么被折叠?



