单目运算符

void Fun()
{

    int i =10;
    
    int k = i++;

    printf("%d %d\n",k,i);

}



打印结果为10,11,先赋值,在运算,查看汇编代码

0040D768   mov         dword ptr [ebp-4],0Ah     将10的值给I
0040D76F   mov         eax,dword ptr [ebp-4]      将10放到eax
0040D772   mov         dword ptr [ebp-8],eax      将eax的值给k  也就是k=10
0040D775   mov         ecx,dword ptr [ebp-4]      在将i也就是10 放到ecx
0040D778   add         ecx,1                 ecx = ecx + 0x1 = 11
0040D77B   mov         dword ptr [ebp-4],ecx      将ecx = 11 放到i  所以 I = 11
0040D77E   mov         edx,dword ptr [ebp-4]       
0040D781   push        edx
0040D782   mov         eax,dword ptr [ebp-8]
0040D785   push        eax
0040D786   push        offset string "%d\n" (0042201c)
0040D78B   call        printf (004010c0)
0040D790   add         esp,0Ch


再看++在前
void Fun()
{

    int i =10;
    
    int k = ++i;

    printf("%d %d\n",k,i);

}

打印结果为11,11,先运算,在赋值,查看汇编代码
0040D768   mov         dword ptr [ebp-4],0Ah  i=10
0040D76F   mov         eax,dword ptr [ebp-4]   eax = i = 10
0040D772   add         eax,1              eax = eax +1 = 0x11
0040D775   mov         dword ptr [ebp-4],eax   i = eax = 0x11
0040D778   mov         ecx,dword ptr [ebp-4]   ecx = i = 0x11
0040D77B   mov         dword ptr [ebp-8],ecx   k = ecx = 0x11
0040D77E   mov         edx,dword ptr [ebp-4]

发表评论 / Comment

用心评论~