关于';不是一个国家';在c中?
在学习编译器优化时,我使用关于';不是一个国家';在c中?,c,gcc,assembly,compiler-optimization,logical-operators,C,Gcc,Assembly,Compiler Optimization,Logical Operators,在学习编译器优化时,我使用GCCversionGCC 4.4.5版(Ubuntu/Linaro 4.4.4-14ubuntu5.1)在Linux下用C编写代码 为了理解C语言中的而不是一个语句(nop)。我首先编写了两个代码y.C第二个x.C,然后使用gcc-S选项生成编译后的汇编代码 第一个代码y.c desktop:~$ cat y.c main() { int i=0; } desktop:~$ gcc -S y.c desktop:~$ 第二个代码x.c desktop:~$ cat
GCC
versionGCC 4.4.5版(Ubuntu/Linaro 4.4.4-14ubuntu5.1)在Linux
下用C
编写代码
为了理解C语言中的而不是一个语句
(nop)。我首先编写了两个代码y.C
第二个x.C
,然后使用gcc-S
选项生成编译后的汇编代码
第一个代码y.c
desktop:~$ cat y.c
main()
{
int i=0;
}
desktop:~$ gcc -S y.c
desktop:~$
第二个代码x.c
desktop:~$ cat x.c
main()
{
int i=0;
/* Loops and if*/
while(0);
for(;0;);
if(0);
/* Arithmetic Operations */
i * i;
i / i;
i - i;
i + i;
i % i;
+i;
-i;
/* Comparison operators */
i == i;
i != i;
i < i;
i > i;
i >= i;
i <= i;
/* Bitwise Operations*/
i ^ i;
i | i;
i & i;
~i;
i << i;
i >> i;
/* Address Operatins*/
&i;
*(&i);
(&i)[i];
/* Ternary conditional operation*/
i? i : i;
/* Other Operations*/
sizeof(i);
(char)i;
/* Not-Logical Operation*/
!i;
/* Logical AND , OR Operators */
// i && i; // Commented && operation
// i || i; // Commented || operation
}
desktop:~$ gcc -S x.c
但是当我在x.c
中取消注释最后一行(或者说添加)时
i && i;
i || i;
再次使用-S选项编译x.c,并与y.S进行比较
desktop:~$ tail x.c
sizeof(i);
(char)i;
/* Not-Logical Operation*/
!i;
/* Logical AND , OR Operators */
i && i; // unCommented && operation
i || i; // unCommented || operation
}
desktop:~$ gcc -S x.c
desktop:~$ diff x.s y.s
1c1
< .file "x.c"
---
> .file "y.c"
10,21d9
< movl -4(%ebp), %eax
< testl %eax, %eax
< je .L3
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L3:
< movl -4(%ebp), %eax
< testl %eax, %eax
< jne .L8
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L8:
desktop:~$
在RHS中的额外行是由于文件之间未对齐造成的
我还想添加JAVA
和C
编译器在没有任何标志的情况下丢弃这些表达式的信息 使用-O2
启用优化,您应该会看到额外的代码消失。我认为编译器也应该放弃最后两个表达式代码>,**不是**?请它优化,我希望它会删除它们。不知道为什么它会让它们处于未优化状态,可能它需要比没有优化的情况下对代码进行更多的分析,以确保它们没有副作用。一个重要的区别是,C逻辑运算符&&
和|
实现了短路布尔逻辑,这通常涉及到控制流。这与普通的算术或逻辑运算符非常不同。$gcc-02-sx.c
结果gcc:unrecogned option'-02'
@GrijeshChauhan它是O('Oh'),而不是0('zero')。谢谢Alexey Feldgendler,你的帖子对我很有用!
desktop:~$ tail x.c
sizeof(i);
(char)i;
/* Not-Logical Operation*/
!i;
/* Logical AND , OR Operators */
i && i; // unCommented && operation
i || i; // unCommented || operation
}
desktop:~$ gcc -S x.c
desktop:~$ diff x.s y.s
1c1
< .file "x.c"
---
> .file "y.c"
10,21d9
< movl -4(%ebp), %eax
< testl %eax, %eax
< je .L3
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L3:
< movl -4(%ebp), %eax
< testl %eax, %eax
< jne .L8
< movl -4(%ebp), %eax
< testl %eax, %eax
< .L8:
desktop:~$
desktop:~$ gcc -o2 -S y.c
desktop:~$ gcc -o2 -S x.c
desktop:~$ diff x.s y.s -y
.file "x.c" | .file "y.c"
.text .text
.p2align 4,,15 <
.globl main .globl main
.type main, @function .type main, @function
main: main:
pushl %ebp pushl %ebp
movl %esp, %ebp movl %esp, %ebp
popl %ebp | subl $16, %esp
> movl $0, -4(%ebp)
> leave