[汇编] 十进制和十六进制数之间的转换

刚接触汇编,很多细节搞得还不是很清楚,写了一个十进制和十六进制之间相互转换的程序(有点挫。。)就当练手了。

贴出代码,希望路过的大牛可以给一些指导。

思路:(10->16)十进制数输入的时候单个字符处理,遇见回车结束输入,最后得到一个十进制数。然后循环左移(只处理4次),取后四位,也就是相当于/16

最后输出字符。(16->10)类似处理,先转换成10进制数(每次*16),最后依次取出每一位输出。

; multi-segment executable file template.

data segment
    ; add your data here!  
    num dw 0
    string1 db  'please choose  1 (dec->hex) or 2 (hex->dec)]:$'
    inputDec db 'please input a Dec number :$'
    inputHex db 'please input a Hex number :$'

ends

stack segment
    dw   128  dup(0)
ends

code segment 

start:
 ;set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax  
    
 ;-------------选择模式------------------ 
 model:
    lea dx, string1
    mov ah,9
    int 21h
    
    mov ah,1
    int 21h
    sub al,30h
    cmp al,1 
    mov cx,10  ;乘10
    jz  option1
    call CRLF  
    
    cmp al,2 
    mov cx,16
    jz option2
    call CRLF  
    jmp model  
 ;----------------------------------------
  
 ;***********十进制转十六进制*****************
 
 
 ;-----------输入一个十进制数-------------
 option1: 
    call CRLF
    lea dx,inputDec
    mov ah,9
    int 21h
 rotate1:
    mov ah,1
    int 21h
    cmp al,0dh  ;回车结束十进制数的输入
    jz DectoHex
    sub al,30h
    cbw
    push ax
    mov ax,bx
    mul cx 
    mov bx,ax
    pop ax
    add bx,ax ;输入结果保存在bx中
    jmp rotate1
  ;-----------------------------------------
   
  ;---------开始转换(10->16)------------- 
 DectoHex: 
    call CRLF
    mov ch,4 
 rotatecur:
    mov cl,4
    rol bx,cl
    mov al,bl
    and al,0fh
    add al,30h
    cmp al,3ah
    jl print1
    add al,7h
 print1:
   mov dl,al
   mov ah,2
   int 21h
   dec ch
   jnz rotatecur 
   
   call exit 
 ;**************十六进制转十进制******************* 
 
 ;-------------输入一个十六进制数------------------  
 option2: 
    call CRLF
    lea dx,inputHex
    mov ah,9
    int 21h
 rotate2:
    mov ah,1
    int 21h
    cmp al,0dh  ;回车结束十六进制数的输入
    jz HextoDec  
     
    sub al,30h
    cmp al,9
    jg Other  
 L:
    cbw
    push ax
    mov ax,num
    mul cx 
    mov num,ax
    pop ax
    add num,ax ; 输入结果保存在num中
    jmp rotate2  
    
 Other: 
    sub al,7h
    jmp L
 HextoDec: 
     call  CRLF
     mov cx,1000 
     ;----------重复的输出代码。。。-----
     mov ax, num
     mov dx, 0
     div cx
     mov num, dx
     mov dl, al

     add dl,30h
     mov ah,02h
     int 21h  
     ;--------------------
     mov cx,100
      ;----------
     mov ax, num
     mov dx, 0
     div cx
     mov num, dx
     mov dl, al

     add dl,30h
     mov ah,02h
     int 21h  
     ;----------
     mov cx,10
     ;----------
     mov ax, num
     mov dx, 0
     div cx
     mov num, dx
     mov dl, al

     add dl,30h
     mov ah,02h
     int 21h  
     ;----------
     mov cx,1
      ;----------
     mov ax, num
     mov dx, 0
     div cx
     mov num, dx
     mov dl, al

     add dl,30h
     mov ah,02h
     int 21h  
     ;----------
     
     call exit 
    
     
;----------取每一位------------------

;-----------------------------------

 
 ;--------回车换行------------------   
  CRLF proc near
          push ax
          push dx
          pushf
          
          mov dl,0dh;回车
          mov ah,2
          int 21h
          
          mov dl,0ah;换行
          mov ah,2
          int 21h
          
          popf
          pop dx
          pop ax
          
          ret
   CRLF endp   
  ;-----------------------------------------
    
  ;-----------------退出---------------------
  exit proc
    mov ax, 4c00h ; exit to operating system.
    int 21h  
  exit endp 
  ;------------------------------------------     
  
  

  
ends

end start ; set entry point and stop the assembler.
原文地址:https://www.cnblogs.com/Tach-ac/p/4422581.html