int x = 3;
x += x++;
So after we've passed this starting point, allow me to take one step forward and suggest the following, highly readable, lines of code:
int x = 10;
x = --x + x + --x;
Ready? If your answer was "26", then you're correct. Why? well, there's a subtle hint in the post's header.
What's confusing about this expression is that we always need to keep track of where our variable's value is stored. Whether its in the main memory? a CPU register? or perhaps in a totally different location? In fact, during execution, the program actually reserves 4 bytes on the thread's stack to keep the result of part of the computation.
In the first step, we decrement x's value by 1, and update it's value in main memory (could effectively be in the CPU cache, but this part isn't relevant to our case). At this point, x's value is set to 9. Next, we multiply x's value by 2 (x+x) and store the resulting value on a new variable located on the stack. Do not confuse, we don't update the value of the original x variable at this time. Afterwords, we decrement x's value again by 1 (now x is set to 8), then then combine it to the value of the temporary variable. The result of this last computation goes to x. So by the end of this single line of code, x is set to 26.
Looking at the generated assembler code, things might look a bit clearer:
00000019 mov dword ptr [ebp-4],0Ah // x = 10
00000020 dec dword ptr [ebp-4] // x = 9
00000023 mov eax, dword ptr [ebp-4] // x = 9, eax = 9
00000026 add eax, dword ptr [ebp-4] // x = 9, eax = 18
00000029 mov dword ptr [ebp-8],eax // x = 9, eax = 18, temp = 18
0000002c dec dword ptr [ebp-4] // x = 8, eax = 18, temp = 18
0000002f mov eax, dword ptr [ebp-8] // x = 8, eax = 18, temp = 18
00000032 add dword ptr [ebp-4],eax // x = 26
No comments:
Post a Comment