将十进制转换为十六进制

发布于 2024-10-30 01:13:07 字数 1042 浏览 5 评论 0原文

首先,这是家庭作业。

我正在尝试将 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

First off, this is homework.

I'm trying to read a 5 digit number into the register bx. The number is assumed to be no greater than 65535 (16 bits). Below is how I am attempting to do so.

However, when I attempt to print the number, I am only printing the very last digit that was entered. Which leads me to guess that when I add another number to bx it is overwriting the previous number, but I am unable to see the problem. Any help would be appreciated, I'm almost certain that it is something small I'm overlooking :-/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2024-11-06 01:13:07

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

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

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

When using the MUL opcode, there are three different results:

  • 8 bit - results are stored in ax
  • 16 bit - results are stored in dx:ax
  • 32 bit - results are stored in
    edx:eax

So when you perform your multiplication, the instruction overwrites dx with zero in your case. This means that each subsequent use of the mul opcode is multiplying by zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文