递归的汇编实现(8)- -

来源:互联网 发布:淘宝钱盾一直认证失败 编辑:程序博客网 时间:2024/05/15 11:53

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, @functionfactorial:        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





























原创粉丝点击