“gcc -x c”和“gcc -x c”之间的区别和“gcc -x c++”装配输出
我在文件 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++ 代码中的 setle
和 testb
而不是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您使用的是不同的编译器,因此获得不同的输出也就不足为奇了。如果您感兴趣的话,在将程序编译为 C 或 C++ 时,
clang
确实会给出完全相同的结果:我从
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:I get the same results from
gcc
andg++
on my machine as well, so it looks like it's just a quirk of your particular compiler version.