【问题标题】:ASM printing big numbersASM 打印大数字
【发布时间】:2013-12-18 13:16:57
【问题描述】:

我应该在 NASM 中编写程序(并在 DosBox 下对其进行测试),该程序将在一个约束条件下计算阶乘:结果将存储在最多 128 位中。因此要计算的最大值是阶乘(34)。 我的问题是我不知道如何打印这么大的数字。我唯一的提示是使用 DOS 中断,但经过长时间的研究,我没有发现任何对我有帮助的东西。

我已经做的部分计算阶乘是:

        org 100h
section     .text
factorial   dw 1200h

        xor edx, edx
        xor eax, eax
        mov ax, 6d      ;we'll be calculating 6!
        mov word [factorial+16], 1d     ;current result is 1

wloop:
        mov cx, 08h     ;set loop iterator to 8
        xor edx, edx    ;clear edx
    mloop:                  ;iterate over cx (shift)
        mov bx, cx      ;copy loop iterator to bx (indirect adressing will be used)
        add bx, bx      ;bx*=2
        push ax         ;store ax (number to multiply by)
        mul word[factorial+bx]  ;multiply ax and result
        mov word[factorial+bx], ax  ;store new result in [factorial+2*cx]
        pop ax          ;restore previous ax
        push dx         ;transfer from mul is stored in stack
        loop mloop      ;loop over cx until it's 0

        mov cx, 7h      ;set loop iterator to 7
    tloop:                  ;iterate over cx, adding transfers to result
        pop dx          ;pop it from stack
        mov bx, cx      ;bx = cx
        add bx, bx      ;bx = bx+bx
        adc [factorial+bx],dx ;add transfer to [factorial+2*cx]
        loop tloop      ;loop over cx until it's 0

        pop bx          ;one redundant transfer is removed from stack
        dec ax          ;decrease ax (number to multiply by)
        cmp ax, 0       ;if ax is non-zero...
        jne wloop       ;...continue multiplying

        movzx eax, word[factorial+16] ;load last 32 bits of result...
        call println    ;...and print them

        jmp exit        ;exit program

嗯...我知道如何打印 32 位数字,但这可能不是我想要打印 128b 数字的方式:

println:                    ;Prints from eax
        push eax
        push edx
        mov ecx, 10
    loopr:  
        xor edx, edx
        div ecx         ; eax <- eax/10, edx <- eax % 10
        push eax        ; stack <- eax (store, because DOS will need it)
        add dl, '0'     ; edx to ASCII
        mov ah,2        ; DOS char print
        int 21h         ; interrupt to DOS
        pop eax         ; stack -> eax (restoring)
        cmp eax, 0
        jnz loopr

        mov dl, 13d     ;Carriage Return
        mov ah, 2h
        int 21h
        mov dl, 10d     ;Line Feed
        mov ah, 2h
        int 21h

        pop edx
        pop eax
        ret

谁能帮助我并给出一些提示如何处理它?我什至不知道阶乘是否计算正确,因为我无法打印大于 32b 的任何内容...

【问题讨论】:

  • DOS 中断?你是过去的人吗?
  • 不,只是来自弗罗茨瓦夫科技大学(波兰);)

标签: assembly printing nasm factorial bignum


【解决方案1】:

您的操作方式与处理小数的方式完全相同,即依次除以十并获得最小的数字作为余数。不过,在打印时不要忘记颠倒数字的顺序(堆栈就派上用场了。)

多字除以 10 很容易实现,方法是通过从最高有效到最低有效遍历扩展整数中的字,除以 10,并将中间余数作为高位字进行下一个除法(在 DX 中,其中DIV 除以 DX:AX。)

这里的参考是 C 中的等效算法,您可以将其用作起点。其中 128 位数字存储为 8 个 little-endian 16 位字。

uint16_t extended_div(uint16_t *num, uint16_t den, size_t len) {
    ldiv_t acc = { 0 };
    num += len;
    do {
        acc = ldiv(acc.rem << 16 | *--num, den);
        *num = acc.quot;
    } while(--len);
    return acc.rem;
}

为了获得额外的积分,您可以使用 x86 的 BCD 指令(在本例中为 AAM)作为替代,并直接以十进制格式计算阶乘的乘法。


首先,如果您还没有一个可用的调试器,请务必为自己准备一个可用的调试器,这样您就可以逐步完成测试用例并在纸上跟进。我个人以前使用过 Turbo Debugger,我相信 NASM 能够生成兼容的符号信息。我并不是要唠叨,但以前有关 16 位 DOS 汇编的海报往往会忽略该过程的这一关键步骤。

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2013-05-03
    • 2021-06-22
    • 1970-01-01
    • 2021-02-20
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多