assembly - 将十进制转换为十六进制

标签 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

最佳答案

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

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

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

    关于assembly - 将十进制转换为十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533119/

    相关文章:

    python - 将单独的 pandas 数据框中的列相乘

    java - 使用线程将两个矩阵相乘

    c - C 和 asm 中的 imulq 和 unsigned long long 溢出检测

    c++ - 从 C 调用的 nasm 函数最终会出现段错误

    c++ - 整数乘法真的以与现代 CPU 上的加法相同的速度完成吗?

    assembly - 为什么 `call dword 0x12345678` 会编译成 [66,E8,72,56,34,12]?

    linux - NASM scanf 未定义引用 (LINUX)

    assembly - 多态的代价

    debugging - 如何从ghidra的相应指令中打入x64dbg?

    无法加载我的操作系统内核