From the compilation perspective, I ++ and ++ I

Source: Internet
Author: User

From the compilation perspective, I ++ and ++ I

In the background of the story, a guy in C Language asked me questions about ++ I and I ++. I didn't tell him when I went to class. I just tested the following code when I was free tonight:

Compiling environment: VS2010 language: C ++

 1 #include 
 
   2 using namespace std; 3  4 int main(void) 5 { 6     int a = 1; 7     int b = 1; 8     int c; 9 10     c = a++;11     c = ++b;12 13     return 0;14 }
 

From the perspective of assembly, Let's explain the following problem:

Maybe you have never learned assembly, but it doesn't matter. Let's take a look at the basic assembly knowledge. (I will not compile it myself, but I can understand some simple assembly code)

-----------------------------------------------------------------

1) dword ptr: dword-> double word dual-byte ptr-> pointer 2) mov a B: Indicates assigning the value of B to a 3) add x y: add the value of x to the value of y, and then put the result into x. 4) in addition, there are eight General registers of the cpu: eax, ebx, ecx, edx, esi, edi, ebp, espeax: is the "accumulator", which is the default register of many addition multiplication commands. ecx: is the "counter", which is the internal counter of repeated (REP) prefix commands and LOOP commands.

-----------------------------------------------------------------

Now, let me explain the following assembly code, which is almost the same.

Note: The following Assembly Code is interpreted, for example, eax = 1, indicating that the current value of eax is 1.

1 int a = 1; 2 00EC136E mov dword ptr [a], 1 // assign a value of 1 3 int B = 1; 4 00EC1375 mov dword ptr [B], 1 // assign B 1 5 int c; 6 7 c = a ++; 8 00EC137C mov eax, dword ptr [a] // put a = 1 into the eax = 1 register 9 00EC137F mov dword ptr [c], eax // put eax = 1 into the address of c = 1 10 00EC1382 mov ecx, dword ptr [a] // add a = 1 to register ecx = 1 11 00EC1385 add ecx, 1 // add ecx = 1 and 1, and put 12 00EC1388 mov dword ptr [a] In the ecx = 2 register, ecx // put the value in the ecx = 2 Register into a = 2 13 c = ++ B; 14 00EC138B mov eax, dword ptr [B] // put B = 1 into eax = 1 register 15 00EC138E add eax, 1 // add eax = 1 to 1, and put 16 00EC1391 mov dword ptr [B] In the eax = 2 register, eax // put the value in the eax = 2 register into B = 2 17 00EC1394 mov ecx, dword ptr [B] // put B = 2 into ecx = 2 register 18 00EC1397 mov dword ptr [c], ecx // put the values in ecx = 2 into c = 2 19 20 return 0; 21 00EC139A xor eax, eax 22}

From the above assembly code, we can clearly see that after compilation:

1) c = a ++; the value of c is 1, but the value of a has changed to 2.

2) c = ++ B. The value of c is 2, and the value of B is 2.

--------------------------------------------------------------

Address: http://www.cnblogs.com/nchar/p/3913724.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.