Disassembly Analysis of auto-increment operations

Source: Internet
Author: User

This kind of problem is often encountered during the examination or question preparation. The teacher who gives the question is willing to drill the tips on this point, that is, the combined operation of the auto-increment and auto-increment operations. I personally think that, it doesn't make much sense to take such a question, because different compilers have different results, and this is misleading, without the essence of language learning, but we still have to face it ...... Sad. The following is an example of a compilation:

# Include "stdio. H"
Int main ()
{
Int I = 0, n = 0;
N = (++ I) + (++ I );
Printf ("n = % d/N", N );
Printf ("I = % d/N", I );
Return 0;
}

I compiled it in VC ++. Net and the result is: n = 9, I = 3
Let me use the studio2003.net debugger to analyze it:
The related disassembly code is as follows: (each statement is followed by a comment)
Int I = 0, n = 0;
00411a4e mov dword ptr [I], 0; I initialized to 0
00411a55 mov dword ptr [N], 0; n Initialization is 0
N = (++ I) + (++ I );
00411a5c mov eax, dword ptr [I]; eax = 0;
00411a5f add eax, 1; eax = eax + 1, so eax = 1
00411a62 mov dword ptr [I], eax; assign 1 to I
00411a65 mov ECx, dword ptr [I]; load I into ECx. In this case, ECx = 1
00411a68 add ECx, 1; ECx = ECx + 1, ECx = 2
00411a6b mov dword ptr [I], ECx; assign ECx to I, at this time I = 2
00411a6e mov edX, dword ptr [I]; load I to edX, (EDX) = 2
00411a71 add edX, 1; edX = edX + 1, (EDX) = 3
00411a74 mov dword ptr [I], EDX; Set edX back to I, I = 3
00411a77 mov eax, dword ptr [I]; eax = 3
00411a7a add eax, dword ptr [I]; eax = eax + I = 3 + 3 = 6
00411a7d add eax, dword ptr [I]; eax = eax + 3 = 6 + 3 = 9
00411a80 mov dword ptr [N], eax; n = (eax) = 9
From the above disassembly process, we can see that n = 9, I = 3, and the same is true after the output. The output assembly code will not be pasted.
The output results of different compilers may be different. I think the code may be different from the decompiled code, so the results are naturally different. It can be seen from this that the ideas will be much clearer through disassembly code analysis. Studio2003.net compiler, for (++ I) + (++ I); this operation is to calculate I first, we will calculate the three ++ I values first, and the result is equal to 3. Then we will calculate the addition of the parentheses. The result is n = 3 + 3 + 3 = 9.

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.