首页 / 软件开发 / C++ / (i++)+(i++)与(++i)+(++i)
(i++)+(i++)与(++i)+(++i)2010-06-02 博客园 arrowcat与在前面:++(--)有太多让人困惑的地方,(i++)+(i++)与(++i)+(++i)有什么不同?为什么不同?如果从机器的角度去理解,就会豁然开朗。先来看段程序:int main() { int i=3; int j=(i++)+(i++); // int j=(++i)+(++i); printf("%d,%d
",i,j); }(1)在VC 6.0下:对于(i++)+(i++):结果:i=5,j=6相应的汇编代码为(有详细注释):8B 45 FC mov eax,dword ptr [ebp-4] ;i->eax 03 45 FC add eax,dword ptr [ebp-4] ;i+i=6 89 45 F8 mov dword ptr [ebp-8],eax ;6->j 8B 4D FC mov ecx,dword ptr [ebp-4] ;i->ecx(=3) 83 C1 01 add ecx,1 ;ecx=4 89 4D FC mov dword ptr [ebp-4],ecx ;4->i 8B 55 FC mov edx,dword ptr [ebp-4] ;i->edx 83 C2 01 add edx,1 ;edx=5 89 55 FC mov dword ptr [ebp-4],edx ;5->i对于(++i)+(++i):结果:i=5,j=10相应的汇编代码为:8B 45 FC mov eax,dword ptr [ebp-4] ;i->eax (=3) 83 C0 01 add eax,1 ;eax=4 89 45 FC mov dword ptr [ebp-4],eax ;4->i 8B 4D FC mov ecx,dword ptr [ebp-4] ;i->ecx 83 C1 01 add ecx,1 ;ecx=5 89 4D FC mov dword ptr [ebp-4],ecx ;5->i 8B 55 FC mov edx,dword ptr [ebp-4] ;i->edx 03 55 FC add edx,dword ptr [ebp-4] ;edx=10 ,即i+i 89 55 F8 mov dword ptr [ebp-8],edx ;10->j(2)在gcc 3.2.2下:对于(i++)+(i++):结果:i=5,j=6相应的汇编代码为:c7 45 fc 03 00 00 00 movl $3, -4(%ebp) ;3->i 8b 55 fc movl -4(%ebp), %edx ;i->edx (=3) 8b 45 fc movl -4(%ebp), %eax ;i->eax (=3) 8d 04 10 leal (%eax,%edx), %eax ;i+i=6 ->eax 89 45 f8 movl %eax, -8(%ebp) ;6->j 8d 45 fc leal -4(%ebp), %eax ;&i->eax ff 00 incl (%eax) ;i++ ,即i=4,注意这里为寄存器间接寻址 8d 45 fc leal -4(%ebp), %eax ;&i->eax ff 00 incl (%eax) ;i++,即i=5对于(++i)+(++i):结果:i=5,j=10相应的汇编代码为:movl $3, -4(%ebp) ;3->i leal -4(%ebp), %eax ;&i->eax incl (%eax) ;i++,即i=4 leal -4(%ebp), %eax ;&i->eax incl (%eax) ;i++, i=5 movl -4(%ebp), %eax ;i->eax, eax=5 addl -4(%ebp), %eax ;i+i ->eax ,eax=10 movl %eax, -8(%ebp) ;10->j可见,对于VC6.0和gcc,二者的结果一致,但是gcc 3.2.2生成的汇编代码明显比VC6.0高效、简洁。这也许是因为VC 6.0出现较早的原因吧。
收藏该网址