这个帖子似乎应该是了结了,虽然楼上说看汇编代码能够明白了。
- A) Y. f1 J. ^4 ~
不过我还是想灌次水。
( \% R2 n+ T4 v8 w& {
! ?5 V9 }0 o, B- {
这是C99标准里
" t$ m6 O8 s$ n: E 6.5.7 Bitwise shift operators
" w6 J4 n! x7 m
一节中提到的:
& ?! l5 Z# ?; J! ~, N Semantics
5 W8 d$ H% ~' i( x) {7 {
……
0 ~( ?9 s: R, n1 O% [+ s) y 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with
8 l0 _8 Q2 K& a7 L7 L% F* c1 K zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo
0 U' R q+ ?. a one more than the maximum value representable in the result type. If E1 has a signed
4 N% m. U) \, Y
type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is
@/ i7 s6 M* Y. @5 D, m2 p- e' d9 U 4 d. H+ J% k" C% _0 X6 \2 D. p
在GCC参考手册
' b( J$ m1 J8 E7 P4 f" h 4.5 Integers (Page 207)有这样一段话:
. F/ m* T+ _) i2 T! A3 h
2 H: _- ], x$ E" X • The results of some bitwise operations on signed integers (C90 6.3, C99 6.5).
$ m/ h) Q/ J+ ]: `3 R- c Bitwise operators act on the representation of the value including both the sign and
+ H2 b7 _/ m |. a- Z' U5 [ value bits, where the sign bit is considered immediately above the highest-value value
& B* B f# W# W% `: S
bit. Signed ‘>>’ acts on negative numbers by sign extension.
9 A4 J* X1 m! {* u! E( q8 d
GCC does not use the latitude given in C99 only to treat certain aspects of signed ‘<<’
( |, z5 T% p) c' B1 l0 S2 |4 X9 ^0 J as undefined, but this is subject to change.
# {6 s/ p7 _, P- e& ~& h* o) G
( y$ ]( l2 k% ]: H$ t, f
我不想翻译,很容易理解,我不知道版主讨论这个是不是因为GCC没有遵循标准,还是其他什么原因引来一群人的争吵?
* D" A2 Z/ {$ X5 W( y 另外,我也想顺便问下,(但我不想发新帖了)为什么我的编译器汇编后的代码总是比别人的长,是不是被SUSE修改过的GCC版本。
- X2 j0 L6 \" H; m 我用的是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长了好多,很多次用自己的汇编代码和别人的比较都会长一些。