recursion - IA32 汇编实现中的递归斐波那契
问题描述
我目前有以下代码用于递归计算汇编中斐波那契数列的第 n 项,但是它不能正常工作(仅适用于前 3 项)。任何人都可以提供任何见解吗?
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax #retrieve argument passed to function in eax
cmpl $1,%eax #identify if its base case
jle fin
decl %eax
pushl %eax #push n-1 to stack for next function call
call _fibonacci
movl %eax,%ecx #store fib(n-1) in ecx
movl 8(%ebp),%eax
subl $2,%eax
pushl %eax #push n-2 to stack for next function call
call _fibonacci
addl %ecx,%eax #add fib(n-2)+fib(n-1)
fin: #end label
movl %ebp,%esp
popl %ebp
ret
解决方案
call _fibonacci
addl %ecx,%eax #add fib(n-2)+fib(n-1)
您的_fibonacci
函数使用%ecx
而不保存它(在非基本情况下),因此递归调用会覆盖%ecx
.
您应该将第一次调用的返回值保存在_fibonacci
堆栈而不是寄存器中。
推荐阅读
- python - 如何在google colab中将文件夹转换为python包
- cassandra - 在 cassandra UDF 中使用 javax.crypto API 时出错
- javascript - 从 IE 11 中的 iframe 获取 html 代码作为字符串
- python - 我不知道如何在我的程序中添加过滤功能来删除相同的元素,尤其是 '(', ')' 和相同的词
- reactjs - eslint 缩进规则没有被禁用
- java - Mac、Windows 和 Linux 上的 Java 桌面应用程序安装程序
- crystal-lang - 为什么 Crystal .should 会产生虚假错误
- python-3.x - Python中置换树上深度优先搜索的实现
- dump - 转储中的虚拟地址
- shell - 当目标服务器在网关后面时如何在jenkins中执行远程命令