将十进制转换为十六进制

时间:2011-04-03 23:02:31

标签: assembly nasm multiplication

首先,这是家庭作业。

我正在尝试将5位数字读入寄存器bx。假设该数字不大于65535(16位)。以下是我试图这样做的方法。

但是,当我尝试打印该号码时,我只打印输入的最后一位数字。这让我想到,当我向bx添加另一个号码时,它会覆盖以前的号码,但我无法看到问题。任何帮助将不胜感激,我几乎可以肯定它是一个小我忽略的东西: - /

mov cx,0x05 ; loop 5 times
    mov bx,0    ; clear the register we are going to store our result in
    mov dx,10   ; set our divisor to 10

read:
    mov ah,0x01     ; read a character function
    int 0x21        ; store the character in al
    sub al,0x30     ; convert ascii number to its decimal equivalent
    and ax,0x000F   ; set higher bits of ax to 0, so we are left with the decimal
    push ax         ; store the number on the stack, this is the single digit that was typed
    ; at this point we have read the char, converted it to decimal, and pushed it onto the stack
    mov ax,bx       ; move our total into ax
    mul dx          ; multiply our total by 10, to shift it right 1
    pop bx          ; pop our single digit into bx
    add bx,ax       ; add our total to bx
    loop read       ; read another char

1 个答案:

答案 0 :(得分:4)

使用MUL操作码时,有三种不同的结果:

  • 8位 - 结果存储在ax
  • 16位 - 结果存储在dx:ax
  • 32位 - 结果存储在 EDX:EAX

因此,当您执行乘法运算时,指令会在您的情况下用零覆盖dx。这意味着mul操作码的每次后续使用都会乘以零。