首页 > 解决方案 > 尝试在 Assembly 8086 32bit 中显示 32 位数字

问题描述

任务和背景:在过去的几周里,我一直在学习汇编语言,作为一个项目,我决定构建一个 32 位计算器,它可以进行乘法、减法、除法和加法运算。我得到 2 个 16 位数字(字,最大 2^16 -1)作为输入,对它们应用数学函数,然后将答案存储在 32 位变量(双字)中。

问题和我的解决方案:当尝试打印答案时,我将数字除以 10 并将余数压入堆栈,并一直保持到商为 0。但有时商大于单词的大小(2^16 -1)。我无法设法解决它。

我想要它做什么:我想将数字(存储在 32 位 var 中)除以 10,将余数存储到堆栈中,并将商存储到 32 位变量中。然后循环这个动作,直到 quotinet 等于 0。

目前:我被卡住了,我还没有找到一种方法来划分两个数字而不将五分体存储到斧头中。

print:
    mov dx, [word ptr answer+2]
    mov ax, [word ptr answer]

    mov     bx,10          ;CONST
    push    bx             ;Sentinel
    a: 
        div bx
        push dx
        xor dx,dx
        cmp ax,0
        JNE a
        ; first one in the stack one must be a digit
        pop dx
    b:  
        add dx, '0'
        mov ah, 2
        int 21h
        pop dx
        cmp dx, bx
        jne b

Ps 我发现这段代码可以打印一个 32 位数字,但我无法完全理解它是如何工作的……如果有人能解释一下它是如何工作的?,尤其是为什么它首先将前 16 位除以(除以 10)并使用提醒,在低 16 位的划分。在学习一种新的编码语言时,我有一个规则,除非我完全理解它,否则我不能使用来自网络的代码。

  mov     bx,10          ;CONST
    push    bx             ;Sentinel
.a: mov     cx,ax          ;Temporarily store LowDividend in CX
    mov     ax,dx          ;First divide the HighDividend
    xor     dx,dx          ;Setup for division DX:AX / BX
    div     bx             ; -> AX is HighQuotient, Remainder is re-used
    xchg    ax,cx          ;Temporarily move it to CX restoring LowDividend
    div     bx             ; -> AX is LowQuotient, Remainder DX=[0,9]
    push    dx             ;(1) Save remainder for now
    mov     dx,cx          ;Build true 32-bit quotient in DX:AX
    or      cx,ax          ;Is the true 32-bit quotient zero?
    jnz     .a             ;No, use as next dividend
    pop     dx             ;(1a) First pop (Is digit for sure)
.b: add     dl,"0"         ;Turn into character [0,9] -> ["0","9"]
    mov     ah,02h         ;DOS.DisplayCharacter
    int     21h            ; -> AL
    pop     dx             ;(1b) All remaining pops
    cmp     dx,bx          ;Was it the sentinel?
    jb      .b             ;Not yet

代码的原始页面

谢谢

标签: assemblyx86-16

解决方案


推荐阅读