With the front: + + (-) There are too many confusing places, (i++) + (i++) and (++i) + (++i) What is the difference? If you understand it from the machine's point of view, it will be enlightened.
Let's take a look at the procedure:
int main()
{
int i=3;
int j=(i++)+(i++);
// int j=(++i)+(++i);
printf("%d,%d\n",i,j);
}
(1) Under VC 6.0:
for (i++) + (i++):
Result: i=5,j=6
The corresponding assembly code is (with detailed comments):
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
for (++i) + (++i):
Result: i=5,j=10
The corresponding assembly code is:
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) under GCC 3.2.2:
for (i++) + (i++):
Results: The corresponding assembly code for I=5,J=6 is:
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
for (++i) + (++i):
Result: i=5,j=10
The corresponding assembly code is:
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
Visible, the results are consistent for VC6.0 and GCC, but the assembly code generated by GCC 3.2.2 is significantly more efficient and concise than VC6.0. This may be because VC 6.0 appeared earlier reasons.