2009年1月1日 星期四

The shift problem

什麼鬼!!!
http://mkl-note.blogspot.com/2008/12/blog-post_27.html

int main(){
int i,j;
/*
$ cc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu11' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu11)
*/

//cc test.c -o test -O0
//objdump test -S

#define TEST1
#define TEST2
#define TEST3
#define TEST4
#define TEST5

#ifdef TEST1
printf("test 1\n");
printf("%.8x\n",(0x12345678)>>0);
//12345678
/*
80483d5: c7 44 24 04 78 56 34 movl $0x12345678,0x4(%esp)
80483dc: 12
80483dd: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
80483e4: e8 0f ff ff ff call 80482f8 <printf@plt>
*/

printf("%.8x\n",(0x12345678)>>8);
//00123456
/*
80483e9: c7 44 24 04 56 34 12 movl $0x123456,0x4(%esp)
80483f0: 00
80483f1: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
80483f8: e8 fb fe ff ff call 80482f8 <printf@plt>
*/

printf("%.8x\n",(0x12345678)>>16);
//00001234
/*
80483fd: c7 44 24 04 34 12 00 movl $0x1234,0x4(%esp)
8048404: 00
8048405: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
804840c: e8 e7 fe ff ff call 80482f8 <printf@plt>
*/

printf("%.8x\n",(0x12345678)>>24);
//00000012
/*
8048411: c7 44 24 04 12 00 00 movl $0x12,0x4(%esp)
8048418: 00
8048419: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
8048420: e8 d3 fe ff ff call 80482f8 <printf@plt>
*/

printf("%.8x\n",(0x12345678)>>32);
//test.c:60: warning: right shift count >= width of type
//00000000
/*
8048425: c7 44 24 04 00 00 00 movl $0x0,0x4(%esp)
804842c: 00
804842d: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)
8048434: e8 bf fe ff ff call 80482f8 <printf@plt>
*/
#endif

#ifdef TEST2
printf("test 2\n");
i=0x12345678>>0;
printf("i=%.8x\n",i);
//i=12345678
/*
80483d5: c7 45 f8 78 56 34 12 movl $0x12345678,-0x8(%ebp)
80483dc: 8b 45 f8 mov -0x8(%ebp),%eax
80483df: 89 44 24 04 mov %eax,0x4(%esp)
80483e3: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
80483ea: e8 09 ff ff ff call 80482f8 <printf@plt>
*/

i=0x12345678>>8;
printf("i=%.8x\n",i);
//i=00123456
/*
80483ef: c7 45 f8 56 34 12 00 movl $0x123456,-0x8(%ebp)
80483f6: 8b 45 f8 mov -0x8(%ebp),%eax
80483f9: 89 44 24 04 mov %eax,0x4(%esp)
80483fd: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
8048404: e8 ef fe ff ff call 80482f8 <printf@plt>
*/

i=0x12345678>>16;
printf("i=%.8x\n",i);
//i=00001234
/*
8048409: c7 45 f8 34 12 00 00 movl $0x1234,-0x8(%ebp)
8048410: 8b 45 f8 mov -0x8(%ebp),%eax
8048413: 89 44 24 04 mov %eax,0x4(%esp)
8048417: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
804841e: e8 d5 fe ff ff call 80482f8 <printf@plt>
*/

i=0x12345678>>24;
printf("i=%.8x\n",i);
//i=00000012
/*
8048423: c7 45 f8 12 00 00 00 movl $0x12,-0x8(%ebp)
804842a: 8b 45 f8 mov -0x8(%ebp),%eax
804842d: 89 44 24 04 mov %eax,0x4(%esp)
8048431: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
8048438: e8 bb fe ff ff call 80482f8 <printf@plt>
*/

i=0x12345678>>32;
printf("i=%.8x\n",i);
//test.c:117: warning: right shift count >= width of type
//i=00000000
/*
804843d: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
8048444: 8b 45 f8 mov -0x8(%ebp),%eax
8048447: 89 44 24 04 mov %eax,0x4(%esp)
804844b: c7 04 24 30 85 04 08 movl $0x8048530,(%esp)
8048452: e8 a1 fe ff ff call 80482f8 <printf@plt>
*/
#endif

#ifdef TEST3
printf("test 3\n");
i=0x12345678;
/*
80483d5: c7 45 f8 78 56 34 12 movl $0x12345678,-0x8(%ebp)
*/

j=i>>0;
printf("j=%.8x\n",j);
//j=12345678
/*
80483dc: 8b 45 f8 mov -0x8(%ebp),%eax
80483df: 89 45 f4 mov %eax,-0xc(%ebp)
80483e2: 8b 45 f4 mov -0xc(%ebp),%eax
80483e5: 89 44 24 04 mov %eax,0x4(%esp)
80483e9: c7 04 24 40 85 04 08 movl $0x8048540,(%esp)
80483f0: e8 03 ff ff ff call 80482f8 <printf@plt>
*/

j=i>>8;
printf("j=%.8x\n",j);
//j=00123456
/*
80483f5: 8b 45 f8 mov -0x8(%ebp),%eax
80483f8: c1 f8 08 sar $0x8,%eax
80483fb: 89 45 f4 mov %eax,-0xc(%ebp)
80483fe: 8b 45 f4 mov -0xc(%ebp),%eax
8048401: 89 44 24 04 mov %eax,0x4(%esp)
8048405: c7 04 24 40 85 04 08 movl $0x8048540,(%esp)
804840c: e8 e7 fe ff ff call 80482f8 <printf@plt>
*/

j=i>>16;
printf("j=%.8x\n",j);
//j=00001234
/*
8048411: 8b 45 f8 mov -0x8(%ebp),%eax
8048414: c1 f8 10 sar $0x10,%eax
8048417: 89 45 f4 mov %eax,-0xc(%ebp)
804841a: 8b 45 f4 mov -0xc(%ebp),%eax
804841d: 89 44 24 04 mov %eax,0x4(%esp)
8048421: c7 04 24 40 85 04 08 movl $0x8048540,(%esp)
8048428: e8 cb fe ff ff call 80482f8 <printf@plt>
*/

j=i>>24;
printf("j=%.8x\n",j);
//j=00000012
/*
804842d: 8b 45 f8 mov -0x8(%ebp),%eax
8048430: c1 f8 18 sar $0x18,%eax
8048433: 89 45 f4 mov %eax,-0xc(%ebp)
8048436: 8b 45 f4 mov -0xc(%ebp),%eax
8048439: 89 44 24 04 mov %eax,0x4(%esp)
804843d: c7 04 24 40 85 04 08 movl $0x8048540,(%esp)
8048444: e8 af fe ff ff call 80482f8 <printf@plt>
*/

j=i>>32;
printf("j=%.8x\n",j);
//test.c:188: warning: right shift count >= width of type
//j=12345678
/*
8048449: 8b 45 f8 mov -0x8(%ebp),%eax
804844c: b9 20 00 00 00 mov $0x20,%ecx
8048451: d3 f8 sar %cl,%eax
8048453: 89 45 f4 mov %eax,-0xc(%ebp)
8048456: 8b 45 f4 mov -0xc(%ebp),%eax
8048459: 89 44 24 04 mov %eax,0x4(%esp)
804845d: c7 04 24 40 85 04 08 movl $0x8048540,(%esp)
8048464: e8 8f fe ff ff call 80482f8 <printf@plt>
*/
#endif

#ifdef TEST4
printf("test 4\n");
i=0;
j=0x12345678>>i;
printf("j=%.8x\n",j);
//j=12345678
/*
80483d5: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
80483dc: 8b 4d f8 mov -0x8(%ebp),%ecx
80483df: b8 78 56 34 12 mov $0x12345678,%eax
80483e4: d3 f8 sar %cl,%eax
80483e6: 89 45 f4 mov %eax,-0xc(%ebp)
80483e9: 8b 45 f4 mov -0xc(%ebp),%eax
80483ec: 89 44 24 04 mov %eax,0x4(%esp)
80483f0: c7 04 24 70 85 04 08 movl $0x8048570,(%esp)
80483f7: e8 fc fe ff ff call 80482f8 <printf@plt>
*/
i=8;
j=0x12345678>>i;
printf("j=%.8x\n",j);
//j=00123456
/*
80483fc: c7 45 f8 08 00 00 00 movl $0x8,-0x8(%ebp)
8048403: 8b 4d f8 mov -0x8(%ebp),%ecx
8048406: b8 78 56 34 12 mov $0x12345678,%eax
804840b: d3 f8 sar %cl,%eax
804840d: 89 45 f4 mov %eax,-0xc(%ebp)
8048410: 8b 45 f4 mov -0xc(%ebp),%eax
8048413: 89 44 24 04 mov %eax,0x4(%esp)
8048417: c7 04 24 70 85 04 08 movl $0x8048570,(%esp)
804841e: e8 d5 fe ff ff call 80482f8 <printf@plt>
*/
i=16;
j=0x12345678>>i;
printf("j=%.8x\n",j);
//j=00001234
/*
8048423: c7 45 f8 10 00 00 00 movl $0x10,-0x8(%ebp)
804842a: 8b 4d f8 mov -0x8(%ebp),%ecx
804842d: b8 78 56 34 12 mov $0x12345678,%eax
8048432: d3 f8 sar %cl,%eax
8048434: 89 45 f4 mov %eax,-0xc(%ebp)
8048437: 8b 45 f4 mov -0xc(%ebp),%eax
804843a: 89 44 24 04 mov %eax,0x4(%esp)
804843e: c7 04 24 70 85 04 08 movl $0x8048570,(%esp)
8048445: e8 ae fe ff ff call 80482f8 <printf@plt>
*/

i=24;
j=0x12345678>>i;
printf("j=%.8x\n",j);
//j=00000012
/*
804844a: c7 45 f8 18 00 00 00 movl $0x18,-0x8(%ebp)
8048451: 8b 4d f8 mov -0x8(%ebp),%ecx
8048454: b8 78 56 34 12 mov $0x12345678,%eax
8048459: d3 f8 sar %cl,%eax
804845b: 89 45 f4 mov %eax,-0xc(%ebp)
804845e: 8b 45 f4 mov -0xc(%ebp),%eax
8048461: 89 44 24 04 mov %eax,0x4(%esp)
8048465: c7 04 24 70 85 04 08 movl $0x8048570,(%esp)
804846c: e8 87 fe ff ff call 80482f8 <printf@plt>
*/
i=32;
j=0x12345678>>i;
printf("j=%.8x\n",j);
//(No Warning!!)
//j=12345678
/*
8048471: c7 45 f8 20 00 00 00 movl $0x20,-0x8(%ebp)
8048478: 8b 4d f8 mov -0x8(%ebp),%ecx
804847b: b8 78 56 34 12 mov $0x12345678,%eax
8048480: d3 f8 sar %cl,%eax
8048482: 89 45 f4 mov %eax,-0xc(%ebp)
8048485: 8b 45 f4 mov -0xc(%ebp),%eax
8048488: 89 44 24 04 mov %eax,0x4(%esp)
804848c: c7 04 24 70 85 04 08 movl $0x8048570,(%esp)
8048493: e8 60 fe ff ff call 80482f8 <printf@plt>
*/
#endif

#ifdef TEST5
printf("test 5\n");
i=0xffffff;
for(i=0;i<40;i+=4) printf("0x12345678>>%d=%.8x\n",i,(0x12345678)>>i);
i=0xffffff;
/*
0x12345678>>0=12345678
0x12345678>>4=01234567
0x12345678>>8=00123456
0x12345678>>12=00012345
0x12345678>>16=00001234
0x12345678>>20=00000123
0x12345678>>24=00000012
0x12345678>>28=00000001
0x12345678>>32=12345678
0x12345678>>36=01234567
*/
/*
080483c4 <main>:
(.......................)
80483d5: c7 45 f8 ff ff ff 00 movl $0xffffff,-0x8(%ebp)

80483dc: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
80483e3: eb 25 jmp 804840a <main+0x46> (804840a)
80483e5: 8b 4d f8 mov -0x8(%ebp),%ecx
80483e8: b8 78 56 34 12 mov $0x12345678,%eax
80483ed: d3 f8 sar %cl,%eax
80483ef: 89 44 24 08 mov %eax,0x8(%esp)
80483f3: 8b 45 f8 mov -0x8(%ebp),%eax
80483f6: 89 44 24 04 mov %eax,0x4(%esp)
80483fa: c7 04 24 f0 84 04 08 movl $0x80484f0,(%esp)
8048401: e8 f2 fe ff ff call 80482f8 <printf@plt>
8048406: 83 45 f8 04 addl $0x4,-0x8(%ebp)
804840a: 83 7d f8 27 cmpl $0x27,-0x8(%ebp)
804840e: 7e d5 jle 80483e5 <main+0x21> (80483e5)

8048410: c7 45 f8 ff ff ff 00 movl $0xffffff,-0x8(%ebp)
*/
#endif


return 0;
}



http://docs.sun.com/app/docs/doc/802-1948/6i5uqa9oh?l=zh_TW&a=view#02.Instruction-Set_Mapping-26
http://oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_6/CH06-3.html#HEADING3-120

沒有留言: