【发布时间】:2012-10-06 16:08:00
【问题描述】:
我搜索了x++ vs ++x,找到了一个很好的答案here,所以我决定看看gcc的汇编输出,看看x++和++x是如何实现的:
main() { int s = 0; ++s;返回0; }
编译示例:
gcc mul.c -masm=intel -o mul.asm
++s的输出:
.file "mul.c"
.intel_syntax
.text
.p2align 4,,15
.globl main
.type main, @function
main:
lea %ecx, [%esp+4]
and %esp, -16
push DWORD PTR [%ecx-4]
push %ebp
mov %ebp, %esp
push %ecx
sub %esp, 16
mov DWORD PTR [%ebp-8], 0
add DWORD PTR [%ebp-8], 1
mov %eax, 0
add %esp, 16
pop %ecx
pop %ebp
lea %esp, [%ecx-4]
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
x++ 的输出:
.file "mul.c"
.intel_syntax
.text
.p2align 4,,15
.globl main
.type main, @function
main:
lea %ecx, [%esp+4]
and %esp, -16
push DWORD PTR [%ecx-4]
push %ebp
mov %ebp, %esp
push %ecx
sub %esp, 16
mov DWORD PTR [%ebp-8], 0
add DWORD PTR [%ebp-8], 1
mov %eax, 0
add %esp, 16
pop %ecx
pop %ebp
lea %esp, [%ecx-4]
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
所以,我在问 x++ 和 ++x 是否有不同的含义,为什么 GCC 为它们输出一些程序集,不应该有不同的输出?
【问题讨论】:
-
当你的整个语句只是增量时,它们会做同样的事情 - 它们会增加
s。
标签: c gcc compiler-construction