“gcc -x c”和“gcc -x c”之间的区别和“gcc -x c++”装配输出

发布于 2025-01-07 12:11:48 字数 1178 浏览 0 评论 0原文

我在文件 main.c 中有以下代码:

int main() {
    int i;
    for (i = 0; i < 5; i++) {
    }

    return 0;
}

当我使用 gcc -xc -m32 -S -O0 -o main.s main.c (在 Fedora 16 64 位下)编译它时,我得到这个输出:

    .file   "main.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $0, -4(%ebp)
    jmp .L2
.L3:
    addl    $1, -4(%ebp)
.L2:
    cmpl    $4, -4(%ebp)
    jle .L3
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.6.2 20111027 (Red Hat 4.6.2-1)"
    .section    .note.GNU-stack,"",@progbits

但是,当我使用 gcc -x c++ -m32 -S -O0 -o main.s main.c 时,除了这些行之外,我得到相同的输出:

.L2:
    cmpl    $4, -4(%ebp)
    setle   %al
    testb   %al, %al
    jne .L3

我的问题是:为什么会这样使用C++ 代码中的 setletestb 而不是 jle?是不是更有效?

PS 另外,有没有办法在程序集输出中删除这些 .cfi_* 指令?

I have the following code in file main.c:

int main() {
    int i;
    for (i = 0; i < 5; i++) {
    }

    return 0;
}

When I compile this with gcc -x c -m32 -S -O0 -o main.s main.c (under Fedora 16 64-bit) I get this output:

    .file   "main.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $0, -4(%ebp)
    jmp .L2
.L3:
    addl    $1, -4(%ebp)
.L2:
    cmpl    $4, -4(%ebp)
    jle .L3
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.6.2 20111027 (Red Hat 4.6.2-1)"
    .section    .note.GNU-stack,"",@progbits

However, when I use gcc -x c++ -m32 -S -O0 -o main.s main.c I get the same output except for these lines:

.L2:
    cmpl    $4, -4(%ebp)
    setle   %al
    testb   %al, %al
    jne .L3

My question is: why does it use setle and testb instead of jle in C++ code? Is it more effective?

P.S. Also, is there a way to get rid of these .cfi_* directives in an assembly output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2025-01-14 12:11:48

好吧,您使用的是不同的编译器,因此获得不同的输出也就不足为奇了。如果您感兴趣的话,在将程序编译为 C 或 C++ 时,clang 确实会给出完全相同的结果:

$ cp example.c example.cpp
$ clang -o exampleC example.c
$ clang++ -o exampleC++ example.cpp
$ otool -tV exampleC > C
$ otool -tV exampleC++ > C++
$ diff C C++
1c1
< exampleC:
---
> exampleC++:
$

我从 gcc得到相同的结果g++ 在我的机器上也是如此,所以看起来这只是您的特定编译器版本的一个怪癖。

Well, you're using a different compiler, so it's not really that surprising that you get different output. clang does happen to give exactly the same results when compiling your program as C or C++, if that's of interest to you:

$ cp example.c example.cpp
$ clang -o exampleC example.c
$ clang++ -o exampleC++ example.cpp
$ otool -tV exampleC > C
$ otool -tV exampleC++ > C++
$ diff C C++
1c1
< exampleC:
---
> exampleC++:
$

I get the same results from gcc and g++ on my machine as well, so it looks like it's just a quirk of your particular compiler version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文