免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2657 | 回复: 3
打印 上一主题 下一主题

gcc -S test.c编译出的程序的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-08 12:37 |只看该作者 |倒序浏览
最近在学linux的汇编程序,我把一个最简单的C程序:

  1. int
  2. main(void)
  3. {
  4.     return 0;
  5. }
复制代码

通过gcc -S test.c编译出的程序是:

  1. .file   "test.c"
  2.         .text
  3. .globl main
  4.         .type   main, @function
  5. main:
  6.         leal    4(%esp), %ecx
  7.         andl    $-16, %esp
  8.         pushl   -4(%ecx)
  9.         pushl   %ebp
  10.         movl    %esp, %ebp
  11.         pushl   %ecx
  12.         movl    $0, %eax
  13.         popl    %ecx
  14.         popl    %ebp
  15.         leal    -4(%ecx), %esp
  16.         ret
  17.         .size   main, .-main
  18.         .ident  "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-27)"
  19.         .section        .note.GNU-stack,"",@progbits
复制代码


跟其他程序编译出来的比较了下,发现:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
这几句在每个汇编是都会有.问下,这些是linux下特有的吗?这些代码都有些什么作用?在哪方面的文档中对这些有说明的?

论坛徽章:
0
2 [报告]
发表于 2007-11-08 19:48 |只看该作者
可能是和操作系统有关

下面的程序
int main() {
    return 0;
}

int f() {
    int i=0;
    i++;
    return 0;
}


在WinXP下编译(GCC 3.4.2)的结果是
    .file    "a.c"
    .def    ___main;    .scl    2;    .type    32;    .endef
    .text
.globl _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    andl    $-16, %esp
    movl    $0, %eax
    addl    $15, %eax
    addl    $15, %eax
    shrl    $4, %eax
    sall    $4, %eax
    movl    %eax, -4(%ebp)
    movl    -4(%ebp), %eax
    call    __alloca
    call    ___main
    movl    $0, %eax
    leave
    ret
.globl _f
    .def    _f;    .scl    2;    .type    32;    .endef
_f:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $4, %esp
    movl    $0, -4(%ebp)
    leal    -4(%ebp), %eax
    incl    (%eax)
    movl    $0, %eax
    leave
    ret


同样的程序在Debian下编译(GCC 4.2.2)的结果是
    .file    "a.c"
    .text
.globl main
    .type    main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl    -4(%ecx)
    pushl    %ebp
    movl    %esp, %ebp
    pushl    %ecx
    movl    $0, %eax
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size    main, .-main
.globl f
    .type    f, @function
f:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $16, %esp
    movl    $0, -4(%ebp)
    addl    $1, -4(%ebp)
    movl    $0, %eax
    leave
    ret
    .size    f, .-f
    .ident    "GCC: (GNU) 4.2.1 (Debian 4.2.1-5)"
    .section    .note.GNU-stack,"",@progbits


至于它们之间的区别,可能和运行库的实现有关,正在研究中

论坛徽章:
0
3 [报告]
发表于 2007-11-09 08:24 |只看该作者
庆祝新版开张,发本汇编的书http://linux.chinaunix.net/bbs/thread-885941-1-10.html
看一眼这个

论坛徽章:
0
4 [报告]
发表于 2007-11-09 18:17 |只看该作者
呵呵。。好的。我也再看看。谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP