[汇编] 十进制与十六进制互转

 

 

  1 ;程序设计的要求:通过键盘给一个四位的16进制数,程序把它转换成10进制数,并显示出来。
  2 ;把输入的4位16进制数以10十进制的值存放到变量num中,注意num可能会有5位数。所以下面
  3 ;就通过÷,%(10000,1000,100,10)来得到每一位数,即十进制数.  
  4 ;相同的道理,把num转换为16进制
  5 DSEG SEGMENT
  6     ;数据段:在此处添加程序所需的数据
  7     num dw ?
  8     arr dw 10 dup(?) 
  9     STRING1  DB  '[choce 0 (dec->hex) or 1 (hex->dec)]:$'      
 10     STRING2  DB  '[input a hex number which must be four digits]:$'
 11     STRING3  DB  '[input a dec number which must be four digits]:$'
 12 DSEG ENDS 
 13 
 14 CSEG SEGMENT
 15 ;***************************************************************************
 16 MAIN PROC FAR          ;主程序入口 
 17     assume  cs:CSEG, ds:DSEG          
 18 ;---------------------------------------------------------------------------
 19 chose:   
 20     mov ax, DSEG
 21     mov ds, ax
 22     mov es, ax
 23 
 24     lea dx, STRING1
 25     mov ah, 9
 26     int 21h         ; output string at ds:dx 
 27     
 28     mov ah, 01h     ;键盘输入并回显,AL=input char
 29     int 21h
 30     cmp al,30h      ;和'0'比较
 31     je  start2      ;转到10-16进制入口
 32     cmp al,31h      ;和'1'比较
 33     je  start1      ;转到16-10进制入口  
 34     call change     ;换行,重新选择
 35     jmp chose       ;输入错误,让其重新输入
 36 ;---------------------------------------------------------------------------
 37 start1:             ;4位16进制转10进制,入口
 38        call change     ;自动换行
 39        
 40     lea dx, STRING2
 41     mov ah, 9
 42     int 21h         ; output string at ds:dx 
 43     
 44     mov num, 0
 45     mov cx, 4        ;输入4位16进制数(这里没有处理少于或多于4位的情况)
 46 L1:    mov ah, 01h     ;键盘输入并回显,AL=input char
 47     int 21h
 48     push cx         ;保护cx
 49     mov cl, 4
 50     shl num, cl        ;输入的数以10进制的形式存到num中[逻辑左移4位,SHL OPR,CNT 前一个不能是操作数,后一个为CL寄存器]
 51     pop cx
 52 
 53     cmp al, 3ah     ;<9直接al-'0'扩展->num->bx
 54     jb s1
 55     cmp al,47h      ;9< <G  [小写的大于大写的]
 56     jb x1 
 57     sub al,20h      ;小写的比大写的多减去20h
 58 x1:    sub al, 7h
 59 s1:    sub al, 30h
 60     mov ah, 0
 61     add num, ax
 62     mov bx, num
 63     loop L1  
 64 solve1:
 65     call change     ;自动换行
 66     call fun1        ;调用主函数
 67     call change
 68     jmp  chose
 69     call exit        ;退出
 70 ;--------------------------------------------------------------------------     
 71 start2:   
 72     call change     ;自动换行
 73        
 74     lea dx, STRING3
 75     mov ah, 9
 76     int 21h         ; output string at ds:dx 
 77     
 78     mov num, 0
 79     mov cx, 4        ;输入4位16进制数(这里没有处理少于或多于4位的情况)
 80 L2:    mov ah, 01h     ;键盘输入并回显,AL=input char
 81     int 21h
 82     push cx         ;保护cx  
 83     push ax
 84     mov cx,10d  
 85     mov ax,num      ;字乘法:(dx,ax)=ax*src
 86     mul cx
 87     mov num,ax      ;把ax给num  
 88     pop ax
 89     pop cx
 90 s2:    sub al, 30h     ;输入转换为数字-'0'
 91     mov ah, 0
 92     add num, ax
 93     mov bx, num
 94     loop L2  
 95     jmp  solve2
 96 solve2:
 97     call change     ;自动换行
 98     call fun2        ;调用主函数
 99     call change
100     jmp  chose
101     call exit        ;退出     
102 ;******************************************************************************;
103 MAIN ENDP   
104 
105 fun1 proc           ;被除数放在dx中
106     mov cx, 10000d    ;把除数存放到cx中
107     call dec_div
108 
109     mov cx, 1000d
110     call dec_div
111     
112     mov cx, 100d
113     call dec_div
114     
115     mov cx, 10d
116     call dec_div
117     
118     mov cx, 1d
119     call dec_div
120     ret
121 fun1 endp        
122 
123 dec_div     proc    ;除法实现,除数为cx的值
124     mov ax, bx
125     mov dx, 0
126     div cx          ;div 无符号:div src 16位操作:商ax=(dx,ax)/src,余数dx
127     mov bx, dx
128     mov dl, al
129 
130     add dl, 30h     ;转换为char并显示
131     mov ah, 02h
132     int 21h
133     ret
134 dec_div    endp 
135 ;------------------------------------------------------------------------
136 fun2 proc           ;被除数放在dx中
137     mov cx,1000h    ;把除数存放到cx中
138     call hex_div
139 
140     mov cx,0100h
141     call hex_div
142     
143     mov cx,0010h
144     call hex_div
145     
146     mov cx,0001h
147     call hex_div
148     
149     ret
150 fun2 endp        
151 
152 hex_div     proc    ;除法实现,除数为cx的值
153     mov ax, bx
154     mov dx, 0
155     div cx          ;div 无符号:div src 16位操作:商ax=(dx,ax)/src,余数dx
156     mov bx, dx
157     mov dl, al
158     
159     cmp dl, 10d     ;和10比   >=10
160     jb show 
161     add dl,7h       ;>=10的情况
162 show:
163     add dl, 30h     ;转换为char并显示
164     mov ah, 02h
165     int 21h
166     ret
167 hex_div    endp
168 ;*************************************************************************************************
169 ;回车换行
170 change     proc
171     push ax
172     mov ah, 02h
173     mov dl, 0ah
174     int 21h
175     mov ah, 02h
176     mov dl, 0dh
177     int 21h
178     pop ax
179     ret
180 change    endp
181 ;*************************************************************************************************
182 ;退出
183 exit    proc
184     ;按任意键退出
185     mov ah,1
186     int 21h
187     mov ax, 4c00h  ;程序结束,返回到操作系统
188     int 21h
189 exit    endp
190 ;*************************************************************************************************
191 END MAIN   
192 CSEG ENDS

 

 

posted @ 2014-04-14 19:42  beautifulzzzz  阅读(5519)  评论(1编辑  收藏  举报