两道题怎么理解不了了呢
1 假设有int i=2,k;则执行k=(i++)+(++i)+(i++);语句后i和k的值分别是_______. 答案:5和92 假设有int i=2,k;则执行k=(i++)+(++i)+(++i);语句后i和k的值分别是
_______. 答案:5和10
file "tes.c" .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr" LC0: .ascii "%d\12\0" .text .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl $24, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax movl %eax, -12(%ebp) movl -12(%ebp), %eax call __alloca call ___main ;从这里开始看起 movl $2, -4(%ebp) ;为局部变量i分配空间并赋初值2 leal -4(%ebp), %eax incl (%eax) ;i++ leal -4(%ebp), %eax incl (%eax) ;i++ 可以看出首先i自增两次变为4 movl -4(%ebp), %eax movl -4(%ebp), %edx addl %eax, %edx ;2+4=6 保存在eax中 leal -4(%ebp), %eax incl (%eax) ;i++ 这时候i自增为5 movl %edx, %eax addl -4(%ebp), %eax ;6+3=9 保存在eax中 movl %eax, -8(%ebp) ;给变量j赋值,也即此时eax中的9 movl -8(%ebp), %eax movl %eax, 4(%esp) movl $LC0, (%esp) call _printf leave ret .def _printf; .scl 3; .type 32; .endef
VC6 5: int h=(i++)+(++i)+(++i); 0040D74F mov eax,dword ptr [ebp-4] i的值2放入eax 0040D752 add eax,1 //eax+1 0040D755 mov dword ptr [ebp-4],eax // eax的值赋值给i,此时i=3 0040D758 mov ecx,dword ptr [ebp-4] 0040D75B add ecx,dword ptr [ebp-4] // eax中的值与i相加 此时 eax的值为 6 0040D75E mov edx,dword ptr [ebp-4] //将i的值(i=3)放入edx 0040D761 add edx,1 0040D764 mov dword ptr [ebp-4],edx //edx加1后 赋值给i,此时i=4 0040D767 add ecx,dword ptr [ebp-4] ecx与i相加 此时ecx为10 0040D76A mov dword ptr [ebp-8],ecx //将ecx的值赋值给 k,k=10 0040D76D mov eax,dword ptr [ebp-4] //再取得i的值放入eax ,此时i=4 0040D770 add eax,1 //eax+1 0040D773 mov dword ptr [ebp-4],eax //eax赋值给i ,此时i=5; 6: printf("%d",h); 0040D776 mov ecx,dword ptr [ebp-8] //k的值为10 0040D779 push ecx 0040D77A push offset string "%d" (00422fa4) 0040D77F call printf (004010a0) VS2005 int h=(i++)+(++i)+(++i); 004113B5 mov eax,dword ptr [i] 004113B8 add eax,1 004113BB mov dword ptr [i],eax 004113BE mov ecx,dword ptr [i] 004113C1 add ecx,1 //上面和VC下的相同,看后面就不一样了 004113C4 mov dword ptr [i],ecx //先计算了两次++i,此时i=4 004113C7 mov edx,dword ptr [i] 004113CA add edx,dword ptr [i] 004113CD add edx,dword ptr [i] 004113D0 mov dword ptr [h],edx //到这里三个i相加结果存入h,此时h=12,i=4 004113D3 mov eax,dword ptr [i] 004113D6 add eax,1 004113D9 mov dword ptr [i],eax //i再次加1;