Assembly 汇编代码问题中的十进制到十六进制转换

Assembly 汇编代码问题中的十进制到十六进制转换,assembly,x86,hex,dos,Assembly,X86,Hex,Dos,我这里有一个汇编程序,它必须将十进制数转换成十六进制数。我用一个数字123测试了它,结果显示正确。但当我把它改为1234时,它显示的是D2而不是真正的4D2。你能帮我解决这个问题吗。我希望你能修改它让我更了解它。谢谢。代码如下: .model small .stack 90h .data counter db 0 curValue db 0 prevValue db 0 hexa db 0 msg db "Enter a decimal number:

我这里有一个汇编程序,它必须将十进制数转换成十六进制数。我用一个数字123测试了它,结果显示正确。但当我把它改为1234时,它显示的是D2而不是真正的4D2。你能帮我解决这个问题吗。我希望你能修改它让我更了解它。谢谢。代码如下:

.model small
.stack 90h   

.data 
   counter db 0
   curValue db 0
   prevValue db 0 
   hexa db 0
   msg db "Enter a decimal number: $"
   hexmsg db 13,10,"In hexadecimal: $"

.code
    mov ax, @data           ;initialize DS
    mov ds, ax 

                            ;load and display the string msg
    mov ah, 09h
    lea dx, msg         
    int 21h

accept: 
    mov ah, 01          
    int 21h                 ;read a digit

    cmp al, 13              ;compare al with 13
    je hexAccept            ;jump to label exit if input is 13 

    sub al, 48              ;subract al with 48
    mov curValue, al        ;move al to curValue

    cmp counter, 1          ;compare counter with 1
    jl inc_ctr              ;jump to label inc_ctr if al<1

    mov al, prevValue       ;move prevValue to al
    mov bl, 10
    mul bl

    add al, curValue        ;add curValue to al

    mov prevValue, al       ;move al tp prevValue

    inc counter             ;inc_ctr counter
    jmp accept              ;jump to label accept

inc_ctr:
    mov prevValue, al       ;move al to prevValue 

    inc counter             ;inc_ctr counter
    jmp accept              ;jump to label accept 

hexAccept:    
    mov bl,prevValue        ;move prevValue to bl
    mov hexa, bl            ;move bl to hexa
    mov bl, 0               ;move 0 to bl

    mov ah, 09h             ;load and display the string hexmsg
    lea dx, hexmsg
    int 21h 

    mov bl,hexa             ;move hexa to bl
    ;mov octal, bl          ;move bl to octal
     xor bx, bx             ;clear bx

    mov bh, 240             ;move 240 to bh
    and bh, hexa            ;multiply hexa with bh
    mov bl, 15              ;move 15 to bl 
    and bl, hexa            ;multiply hexa with bl

    mov cl, 4               ;move 4 to cl
    rol bh, cl              ;rotate bh 4x

    cmp bh, 9               ;compare bh with 9
    jg Letters              ;jump to Letters if bh>9
    add bh, 48              ;add 48 to bh
    mov ah, 02h             ;set the output function
    mov dl, bh              ;move bh to dl
    int 21h                 ;print the character
    jmp Second_digit        ;jump to Second_digit

Letters:
    add bh, 55              ;add 55 to bh

    mov ah, 02h             ;set the output function
    mov dl, bh              ;move bh to dl
    int 21h                 ;print the character

Second_digit:       
    cmp bl, 9               ;compare bl with 9
    jg dispSecond_digit       ;jump to dispSecond_digit if bl>9
    add bl, 48              ;add 48 to bl
    mov ah, 02h             ;set the outputfunction
    mov dl, bl              ;move bl to dl
    int 21h                 ;print the character
    jmp terminate           ;jump to convertTooctall

dispSecond_digit:
    add bl, 55              ;add 55 ot bl
    mov ah, 02h             ;set the output function
    mov dl, bl              ;move bl to dl
    int 21h                 ;print the character

terminate:

    mov ah, 04ch            ;return control to DOS
    int 21h 

1234应该如何适应prevValue或al或bl,即8位?此外,您有一个名为dispSecond_digit的标签这一事实似乎令人怀疑。我没有仔细阅读代码,但从名称上看,您的代码似乎只打印固定数量的十六进制数字2。与非常相似,但存在相同的问题-数据类型太小。这些关于使用DOS或BIOS调用的代码的问题是否有标签,而不是在asm中编写普通的现代程序?DOS标签本身?无论哪种方式,如果您没有调试程序,只需一步。看见