030 函数的递归调用
2015-02-13 15:38
507 查看
/********************030 函数的递归调用************************ * 将一个整数你转换成为字符串。 * C语言精彩编程百例 第30*/ #include<stdio.h> void convert(int n) { int i; i=n/10; if(i!=0) convert(i); putchar(n%10+'0'); } void main() { int number; printf("输入整数:"); scanf("%d",&number); printf("输出是:"); if(number<0) { putchar('-'); number=-number; } convert(number); putchar('\n'); }
编译器汇编的结果:
.file "030.c" .text .align 2 .globl _convert .def _convert; .scl 2; .type 32; .endef _convert: pushl %ebp movl %esp, %ebp subl $8, %esp movl 8(%ebp), %ecx # ecx=n movl $1717986919, %eax # eax=1717986919 imull %ecx # edx:eax=n*1717986919 sarl $2, %edx # edx=n*1717986919/2^34=n*0.1 movl %ecx, %eax # eax=n sarl $31, %eax # 右移31位,取n的符号 subl %eax, %edx # edx = n*0.1 - n的符号, 向零取整结果 movl %edx, %eax # eax = n*0.1 movl %eax, -4(%ebp) # i=n/10 cmpl $0, -4(%ebp) # if(i!=0) je L4 subl $12, %esp # 调用 _convert pushl -4(%ebp) call _convert addl $16, %esp L4: subl $12, %esp # 准备 printf movl 8(%ebp), %ecx # ecx=n movl $1717986919, %eax # eax=1717986919 imull %ecx # edx:eax=n*1717986919 sarl $2, %edx # edx=n*1717986919/2^34=n*0.1 movl %ecx, %eax # eax=n sarl $31, %eax # 右移31位,取n的符号 subl %eax, %edx # edx = n*0.1 - n的符号, 向零取整结果 movl %edx, %eax # edx = n/10 sall $2, %eax # eax=n*0.1*4 addl %edx, %eax # eax=n*0.1*5 addl %eax, %eax # eax=n*0.1*10 subl %eax, %ecx # ecx=n-n*0.1*10 movl %ecx, %eax # eax=n%10 // 编译器为mod做的优化 ... addl $48, %eax # n%10+'0' pushl %eax call _putchar addl $16, %esp leave ret .def ___main; .scl 2; .type 32; .endef LC0: .ascii "\312\344\310\353\325\373\312\375\243\272\0" LC1: .ascii "%d\0" LC2: .ascii "\312\344\263\366\312\307\243\272\0" .align 2 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax movl %eax, -8(%ebp) movl -8(%ebp), %eax call __alloca call ___main subl $12, %esp pushl $LC0 call _printf addl $16, %esp subl $8, %esp leal -4(%ebp), %eax pushl %eax pushl $LC1 call _scanf addl $16, %esp subl $12, %esp pushl $LC2 call _printf addl $16, %esp cmpl $0, -4(%ebp) jns L6 subl $12, %esp pushl $45 call _putchar addl $16, %esp leal -4(%ebp), %eax negl (%eax) L6: subl $12, %esp pushl -4(%ebp) call _convert addl $16, %esp subl $12, %esp pushl $10 call _putchar addl $16, %esp leave ret .def _scanf; .scl 2; .type 32; .endef .def _printf; .scl 2; .type 32; .endef .def _putchar; .scl 2; .type 32; .endef
相关文章推荐
- Java 学习笔记 (10) - Java 函数的递归调用
- 9.8 函数的递归调用
- 关于函数调用和尾递归的一点认识
- C++ && C# 函数的递归调用
- [java]函数的递归调用--代码举例
- 函数的递归调用与分治策略
- 快速排序 --递归调用一个函数初始值被写死
- 函数递归调用详解
- C++ primer 这本书上有这么两句话“派生类虚函数调用基类版本时,必须显式使用作用域操作符。如果派生类函数忽略了这样做,则函数调用会在运行时确定并且将是一个自身调用,从而导致无穷递归。”
- 用C语言解决(hanoi)汉诺塔问题——函数的递归调用
- 函数递归调用
- 函数的调用机制_用递归实现栈(Function call machenism_Recursive calls stimulate stack)
- 自编strlen()函数,并用它递归调用编写revers(S)函数,给字符串S倒序
- C++程序设计实验报告(二十七)---第二周任务一(自定义函数用递归思想,即不断调用,反向输出二进制)
- 《叩响C#之门》第6章 函数 6.5 递归调用(生日贴)
- 函数的递归调用
- C语言程序设计 函数递归调用示例
- ASP递归调用:已知节点查找根节点的函数
- C语言实现:函数的递归调用
- 函数的递归调用与栈