我用OD跟了一下:
int main(int argc, char* argv[])
{
int aa[10];
int i;
printf("Hello!");
for(i=0;i<10;i++)
aa[i]=2;
return 0;
}
反汇编得到:
00401028 |. 68 802E4200 push test.00422E80 ; /format = "Hello!"
0040102D |. E8 4EC60000 call test.printf ; \printf
00401032 |. 83C4 04 add esp,4
00401035 |. C745 D4 00000000 mov dword ptr ss:[ebp-2C],0
0040103C |. EB 09 jmp short test.00401047
0040103E |> 8B45 D4 /mov eax,dword ptr ss:[ebp-2C]
00401041 |. 83C0 01 |add eax,1
00401044 |. 8945 D4 |mov dword ptr ss:[ebp-2C],eax
00401047 |> 837D D4 0A cmp dword ptr ss:[ebp-2C],0A
0040104B |. 7D 0D |jge short test.0040105A
0040104D |. 8B4D D4 |mov ecx,dword ptr ss:[ebp-2C]
00401050 |. C7448D D8 02000000 |mov dword ptr ss:[ebp+ecx*4-28],2
00401058 |.^ EB E4 \jmp short test.0040103E
int main(int argc, char* argv[])
{
int aa[10];
int i;
int *p;
printf("Hello!");
p=aa;
for(i=0;i<10;i++,p++)
*p=2;
return 0;
}
反汇编得到:
0040D718 |. 68 802E4200 push test.00422E80 ; /format = "Hello!"
0040D71D |. E8 5EFFFFFF call test.printf ; \printf
0040D722 |. 83C4 04 add esp,4
0040D725 |. 8D45 D8 lea eax,dword ptr ss:[ebp-28]
0040D728 |. 8945 D0 mov dword ptr ss:[ebp-30],eax
0040D72B |. C745 D4 00000000 mov dword ptr ss:[ebp-2C],0
0040D732 |. EB 12 jmp short test.0040D746
0040D734 |> 8B4D D4 /mov ecx,dword ptr ss:[ebp-2C]
0040D737 |. 83C1 01 |add ecx,1
0040D73A |. 894D D4 |mov dword ptr ss:[ebp-2C],ecx
0040D73D |. 8B55 D0 |mov edx,dword ptr ss:[ebp-30]
0040D740 |. 83C2 04 |add edx,4
0040D743 |. 8955 D0 |mov dword ptr ss:[ebp-30],edx
0040D746 |> 837D D4 0A cmp dword ptr ss:[ebp-2C],0A
0040D74A |. 7D 0B |jge short test.0040D757
0040D74C |. 8B45 D0 |mov eax,dword ptr ss:[ebp-30]
0040D74F |. C700 02000000 |mov dword ptr ds:[eax],2
0040D755 |.^ EB DD \jmp short test.0040D734
可以看到,
aa [ i ] =2;
被VC6编译成
0040104D |. 8B4D D4 |mov ecx,dword ptr ss:[ebp-2C]
00401050 |. C7448D D8 02000000 |mov dword ptr ss:[ebp+ecx*4-28],2
*p=2;
被VC6编译成
0040D74C |. 8B45 D0 |mov eax,dword ptr ss:[ebp-30]
0040D74F |. C700 02000000 |mov dword ptr ds:[eax],2
少了一个加法,乘法和减法的运算.
至于cmp dword ptr ss:[ebp-2C],0A以上的部分,反而变多了是我写程序的问题,不关指针的事.
不知道我这样解释可不可以呢?
PS.我就是那天向LZ买CPLD实验板的人,LZ多多关照哦~~~
[ 此贴被yinx在2007-05-09 12:41重新编辑 ]