题目
十进制到十六进制数转换程序。程序要求从键盘取得一个十进制数,然后把该数以十六进制形式在屏幕上显示出来。
思路
主程序部分十分简单,调用一个子程序DECIBIN实现从键盘取得十进制数并把它转成二进制数;另一个子程序BINHEX把此二进制数以十六进制数显示出来。另外用CRLF子程序取得回车和换行的效果,BX寄存器用来在过程间传递要转换的数。
代码
;;;;;;;;通过寄存器传递参数;;;;;;;;;;;
datasg segment
datasg ends
;---------------------------
codesg segment
assume cs:codesg,ds:datasg,es:datasg
;----------主函数------------
main proc far
start:
push ds
sub ax,ax
push ax
mov ax,datasg
mov ds,ax
mov es,ax
;--------------
repeat: ;死循环
call decibin
call crlf
call binihex
call crlf
jmp repeat
main endp
;----------主函数-------------
;-----------decibin----------
decibin proc near
mov bx,0 ;清理bx
newchar:
mov ah,1
int 21h
sub al,30H
jl exit ;<0 则退出
cmp al,9d ;>9也退出
jg exit
cbw ;byte in AL to work in AX
;Multiply number in BX by 10 decimal
xchg ax,bx
mov cx,10d ;ax*bx
mul cx
xchg ax,bx
add bx,ax ;bx = ax*10 + bx
jmp newchar
exit:
ret
decibin endp
;-----------decibin----------
;-----------binihex----------
binihex 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 printit
add al,7h
printit:
mov dl,al
mov ah,2
int 21h
dec ch
jnz rotate
ret
binihex endp
;-----------binihex----------
;---------- crlf-------------
crlf proc near
mov dl,0dh
mov ah,2
int 21h
mov dl,0ah
mov ah,2
int 21h
ret
crlf endp
;---------- crlf-------------
codesg ends
end start
调试
反汇编
在未进入decibin子程序时查看堆栈段的内容,075A是ds的偏移地址,0000是push ax(0)
进入子程序后查看堆栈段,Push了(IP)
查看decibin子程序
子程序decibin结束后,bx=123
紧接着调用子程序CRLF
再者调用binihex
最终查看结果