这个帖子似乎应该是了结了,虽然楼上说看汇编代码能够明白了。
5 M$ l' u( c1 a( c
不过我还是想灌次水。
5 I: p5 z% _2 _/ d2 @! \ ( f; b! X% H6 V1 e& Y4 `6 [6 N
这是C99标准里
" l; h" a! W1 i" z1 v O 6.5.7 Bitwise shift operators
1 c4 U5 Y% K& L 一节中提到的:
# f, @( q# o& U+ u* ~" K6 R/ j Semantics
0 i7 a4 ?2 e; g; e ……
* ?6 V3 o6 i4 Y0 R# }/ s8 q. I, P- X
4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with
0 g/ o# t. e; b: f zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo
- `) v8 h& k' G0 M. o+ H7 w
one more than the maximum value representable in the result type. If E1 has a signed
7 X# N+ I# s2 |% r type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is
/ {% ^3 j" i* W0 ]3 Z+ `$ l. B% v ? 3 R* ^" p% \# A4 U6 q; h9 C
在GCC参考手册
) Y. D) K1 I" W$ ]* d& F9 H 4.5 Integers (Page 207)有这样一段话:
8 @. w+ @8 a: Y4 d9 b6 F) b
6 U* g. F5 x% d5 [ • The results of some bitwise operations on signed integers (C90 6.3, C99 6.5).
: w+ B& k8 t( h; m* I# s3 p; m Bitwise operators act on the representation of the value including both the sign and
, F0 \7 F7 [6 [$ I1 Z
value bits, where the sign bit is considered immediately above the highest-value value
) j6 W% z9 X4 K$ L" _( [ bit. Signed ‘>>’ acts on negative numbers by sign extension.
8 ~' w4 T) ~4 s4 E& D
GCC does not use the latitude given in C99 only to treat certain aspects of signed ‘<<’
( s* |; ~1 }6 _/ f( b
as undefined, but this is subject to change.
# w+ Y6 N6 m, W1 B, K' v
( H* h3 _7 E- \$ y2 Q
我不想翻译,很容易理解,我不知道版主讨论这个是不是因为GCC没有遵循标准,还是其他什么原因引来一群人的争吵?
4 }6 Q# d0 y! l1 {- `. ~! c 另外,我也想顺便问下,(但我不想发新帖了)为什么我的编译器汇编后的代码总是比别人的长,是不是被SUSE修改过的GCC版本。
4 r) ~" n" u+ g+ ^/ Y2 a
我用的是SUSE自带的GCC,比如上面的汇编代码,在我机器中出来的是
复制内容到剪贴板
代码:
BirdSky:/home/souldump/bin # gcc -S main.c
main.c:10:2: warning: no newline at end of file
BirdSky:/home/souldump/bin # cat main.s
.file "main.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $36, %esp
movl $32, -8(%ebp)
movl -8(%ebp), %ecx
movl $-1, %eax
sall %cl, %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
addl $36, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.0 (SUSE Linux)"
.section .note.GNU-stack,"",@progbits长了好多,很多次用自己的汇编代码和别人的比较都会长一些。