【发布时间】: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