我写了这个,但它不起作用(n>6 时崩溃):
section .data
one: dq 1.0
two: dq 2.0
section .text
fib:
call print_float
fld qword [two]
fcomip
ja fin
fld st0 ; copy arg
fld qword [one]
fsub
call fib ; st0=fib(n-1), st1=arg
fld st1
fld qword [two]
fsub
call fib ; st0=fib(n-2), st1=fib(n-1), st2=arg
fadd ; st0=fib(n-2)+fib(n-1), st1=arg
fstp st1 ; st0=fib(n-2)+fib(n-1)
fin:
ret
我做错了什么和/或我应该怎么做?
注意:我知道有一条加载 1.0
的指令。我这样做只是为了保持一致性,以便最大限度地减少我必须处理的 x86 子集。
最佳答案
x87 FPU 寄存器的作用类似于堆栈,但只有 8 个;在 fib
条目和第一个调用 fib
之间,您将在 FPU 寄存器堆栈上留下一个附加项。
您应该仅在执行计算所需时使用 FPU 寄存器 - 使用主堆栈来存储递归调用中的值。
关于assembly - x86 汇编器中带有 float 的 Fibonacci 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963265/