LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1315|回复: 4

为什么我的gcc把printf编译成puts

[复制链接]
发表于 2007-7-31 09:34:15 | 显示全部楼层 |阅读模式
如题
//==============
cat hello.c
#include <stdio.h>
int main(void)
{
        printf("hello world!\n");
}
//========================
gcc -S hello.c -o hello.s 生成汇编代码
//========================
cat hello.s
//=========================
.file   "hello.c"
        .section        .rodata
.LC0:
        .string "hello world!"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $4, %esp
        movl    $.LC0, (%esp)
        call    puts          //========这里为什么不是printf?
        addl    $4, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)" //=======gcc版本
        .section        .note.GNU-stack,"",@progbits

//==============系统是ubuntu7.04
发表于 2007-7-31 10:15:03 | 显示全部楼层
gcc这么高级阿
大概是因为你的printf只有一个参数,执行效果和puts一样,而puts因为不需要处理格式字符串,效率应该比printf高
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-7-31 10:25:05 | 显示全部楼层
谢谢,增加知识了,我觉的也是因为gcc的版本问题,可能做了优化.根据你的提示,稍微修修改了一下参数.
cat hello.c
//==========================
#include <stdio.h>
int main(void)
{
        printf("hello world! addr=0x%08x\n",printf);
}
//==========================
gcc -S hello.c
//======================
cat hello.s
//===========.file   "hello.c"
        .section        .rodata
.LC0:
        .string "hello world! addr=0x%08x\n"
        .text
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $20, %esp
        movl    $printf, 4(%esp)
        movl    $.LC0, (%esp)
        call    printf   //=================编译后的汇编代码已经是printf了
        addl    $20, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
        .section        .note.GNU-stack,"",@progbits
//====================
谢谢楼上!
回复 支持 反对

使用道具 举报

发表于 2007-8-4 17:25:53 | 显示全部楼层
一定是用了一个挺猛的-O参数!
回复 支持 反对

使用道具 举报

发表于 2007-8-5 15:42:22 | 显示全部楼层
Post by wsw
一定是用了一个挺猛的-O参数!
刚刚试验了一下,根本就不需要使用-O参数,或者指定-O0,也是这种结果。
我测试用的是gcc 4.0.2
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表