【发布时间】:2014-01-25 16:55:31
【问题描述】:
我在互联网上找到了这个递归斐波那契汇编代码,我正在尝试改进它,以便将结果打印出来。问题是,如果我添加 _main 函数并简单地放
call _fibo
在那里,程序将崩溃(更不用说添加更多代码)。我认为调用者必须清理参数,但在我看来,如果没有进一步的代码要执行,它应该可以工作。有什么指点吗?
_fibo:
push ebp
mov ebp, esp
sub esp, 16 ; ...AA-A-A-ND we've built the stack frame
mov eax, [ebp+8]
cmp eax, 2
jae .recur
xor edx, edx
jmp .done
.recur:
sub eax, 2
push eax ; compute fib(n-2)
call _fibo
mov [ebp-8], eax ; save returned value in 8-byte local variable...
mov [ebp-4], edx ; ...in Little-Endian byte order.
mov eax, [ebp+8] ; get argument again
dec eax
push eax ; compute fib(n-1)
call _fibo
mov [ebp-16], eax ; save returned value in 8-byte local variable...
mov [ebp-12], edx ; ...in Little-Endian byte order.
; the next steps are not as efficient as they could be...
mov eax, [ebp-8]
mov edx, [ebp-4] ; retrieve 1st computed value
add eax, [ebp-16]
adc edx, [ebp-12] ; add 2nd computed value
.done:
mov esp, ebp
pop ebp
ret
;----------------------------------------------------------------
http://montcs.bloomu.edu/Code/Asm.and.C/Asm.Nasm/fibonacci.shtml
【问题讨论】:
-
只是为了学习它的基础知识,当然它不是用来编写可以拆分成部分的程序
-
代码在我看来不是'C',你确定它是C语言吗???