Difference between “gcc -x c” and “gcc -x c++” assembly output

只谈情不闲聊 提交于 2020-01-03 19:01:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9338164/difference-between-gcc-x-c-and-gcc-x-c-assembly-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!