http://mikeyao1.bokee.com/2587140.html
这里用汇编实现了斐波那切数的递归调用。
.include "int2str_func.s"
.text
.global _start
_start:
pushl %ebp
movl %esp, %ebp
pushl $10 # 10的斐波那切数
call factorial # 调用斐波那切数函数
#打印结果
pushl %eax
call int2str
movl %ebp, %esp
popl %ebp
movl $1, %eax
movl $0, %ebx
int $0x80
# 斐波那切函数的声明
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax # 保存传入的参数
cmp $1, %eax # 比较是否等于1
je return # 等于1递归调用结束
decl %eax # 参数递减
# 开始递归调用
pushl %eax
call factorial
imull 8(%ebp), %eax # 递归结束后, 与上一次调用所传入的参数相乘,最终得到结果存入eax
# 返回递归函数的中间结果
return:
movl %ebp, %esp # exit current stack frame
popl %ebp # return current stack frame to previous stack frame
ret
汇编语言实现斐波那切数
本文介绍了一个使用汇编语言实现斐波那切数的递归调用的例子。该程序首先定义了斐波那切数的计算函数,并通过递归方式计算指定阶数的斐波那切数。计算完成后,程序会将结果转换为字符串并输出。
2508

被折叠的 条评论
为什么被折叠?



