030 函数的递归调用

/********************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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值