Assembly 十进制到十六进制的转换

Assembly 十进制到十六进制的转换,assembly,nasm,multiplication,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 ou

首先,这是家庭作业

我正试图把一个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

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

  • 8位-结果存储在ax中
  • 16位-结果存储在dx:ax中
  • 32位-结果存储在 edx:eax

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

如果要将十进制转换为十六进制,那么为什么要除以10?在转换到hexI时,您不需要乘/除10。在我发布的代码片段中,我看不到我在哪里除以10。但是我乘以10的原因是因为我一次只能读一个字符,我需要将它乘以10,这样当我再加一个数字时,它就在适当的位置了。+1用于发布家庭作业问题并显示你已经尝试过的内容。对不起,我的错。。我没有你的环境(汇编程序),你能发布一些结果吗?非常感谢,1行,它工作得很好。我没有想到dx被0过度写入,我假设它将保持相同的值。请记住,当您仅反向使用div操作码时,同样的情况也会发生。