[彙編] 十進位和十六進位數之間的轉換

來源:互聯網
上載者:User

標籤:

剛接觸彙編,很多細節搞得還不是很清楚,寫了一個十進位和十六進位之間相互轉換的程式(有點挫。。)就當練手了。

貼出代碼,希望路過的大牛可以給一些指導。

思路:(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 :$‘endsstack segment    dw   128  dup(0)endscode 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   ;------------------------------------------           endsend start ; set entry point and stop the assembler.

 

[彙編] 十進位和十六進位數之間的轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.