首页 > 代码库 > 输入一个十进制的数到dx_ax,然后十六进制转十进制输出

输入一个十进制的数到dx_ax,然后十六进制转十进制输出

  1 ;HtoD
  2 
  3 data segment
  4       n dw ?                        
  5 data ends
  6 
  7 stack segment
  8         db 50 dup(?)
  9 stack ends
 10 
 11 code segment
 12         assume cs:code,ss:stack,ds:data
 13   start:mov ax,stack
 14         mov ss,ax
 15         mov sp,50
 16         mov ax,data
 17         mov ds,ax
 18 
 19         mov si,10
 20         mov di,10000
 21         mov bx,0
 22         mov cx,0
 23                         ;input the number which is lower than 6,5536,0000(65536(high)*10000(low))
 24       s:mov ah,1
 25         int 21h
 26         cmp al,0dh      ;when input ‘\n‘,quit
 27         jz t
 28         sub al,30h
 29         mov ah,0
 30         mov ds:[0],ax
 31                         ;high part
 32         mov ax,bx       
 33         mul si
 34         mov bx,ax
 35                         ;low part
 36         mov ax,cx       
 37         mul si          ;ax*10=dx_a
 38         div di          ;can‘t div 10000h(0000h~FFFFh)  =ax(Shang)...dx(Yu)
 39 
 40         add bx,ax       ;high part
 41         mov cx,dx       ;low part
 42         add cx,ds:[0]   ;it won‘t JinWei , because
 43                         ;dx_ax=ax*10 , the last character of dx_ax value 0 ,
 44                         ;dx=dx_ax%10000 , the last character of dx is 0 ,
 45                         ;dx=dx+ds:[0](0~9) , the last character won‘t JinWei ,
 46                         ;thus cx won‘t JinWei
 47         jmp s
 48       t:mov ax,cx
 49         mov dx,bx
 50 
 51         cmp dx,0
 52         jnz HtoD_dword
 53         call HtoD_word
 54 
 55    exit:mov ah,4ch
 56         int 21h
 57 
 58 HtoD_word:                           
 59         mov bx,10       ;division
 60         mov cx,0
 61         mov dx,0        ;high part(dx_ax<10000)
 62       u:div bx
 63         push dx
 64         mov dx,0
 65         inc cx
 66         cmp ax,0
 67         jnz u
 68 
 69         mov ah,2
 70       v:pop dx          ;dx<10
 71         add dl,30h      ;0->48 48
 72         int 21h
 73         loop v
 74 
 75         ret
 76 
 77 HtoD_dword:
 78         push ax         ;store low part
 79         mov ax,dx
 80         call HtoD_word
 81                         ;output ‘0‘(low part must contain four character)
 82         pop bx          ;bx=low part
 83         mov ah,2
 84         mov dl,0
 85         cmp bx,1000
 86         ja w
 87         int 21h
 88         cmp bx,100
 89         ja w
 90         int 21h
 91         cmp bx,10
 92         ja w
 93         int 21h
 94 
 95       w:mov ax,bx       ;ax=low part
 96         call HtoD_word
 97 
 98         jmp exit
 99 
100 code ends
101         end start
102 
103         ;mov ax,432
104         ;call HtoD_word
105 
106         ;mov ax,0
107         ;mov dx,1
108         ;call HtoD_dword

 

输入一个十进制的数到dx_ax,然后十六进制转十进制输出