Hi Axel,
What is it I'm seeing if I'm watching the Disassembly window? Isn't that
the final JIT'ed code?
The Disassembly window shows assembly code corresponding to the
instructions created by the compiler. If you are debugging managed code,
these assembly instructions correspond to the native code created by the
Just-in-Tim(JIT) compiler, not the Microsoft intermediate language(MSIL)
generated by the Visual Studio compiler.
I have performed a test on the following section of code:
int i = 3;
decimal d;
d = i;
In theory, the value of i will be implicitly converted to a decimal value
before being assigned to d. Press F5 to run the program and I get the
following corresponding assembly code from the Disassembly window:
d = i;
00000026 lea ecx,[ebp-2Ch]
00000029 mov edx,ebx
0000002b call dword ptr ds:[79C41B40h]
00000031 lea edi,[ebp-1Ch]
00000034 lea esi,[ebp-2Ch]
00000037 movs dword ptr [edi],dword ptr [esi]
00000038 movs dword ptr [edi],dword ptr [esi]
00000039 movs dword ptr [edi],dword ptr [esi]
0000003a movs dword ptr [edi],dword ptr [esi]
Then I modify the code to explicitly convert the value of i to a decimal
value as follows:
d = (decimal)i;
The corresponding assembly code is like blow:
d = (decimal)i;
00000026 lea ecx,[ebp-2Ch]
00000029 mov edx,ebx
0000002b call dword ptr ds:[79C41B40h]
00000031 lea edi,[ebp-1Ch]
00000034 lea esi,[ebp-2Ch]
00000037 movs dword ptr [edi],dword ptr [esi]
00000038 movs dword ptr [edi],dword ptr [esi]
00000039 movs dword ptr [edi],dword ptr [esi]
0000003a movs dword ptr [edi],dword ptr [esi]
As we could see, the two sections of assembly code are the same. In fact,
either of the above two sections of assebly code contains the type
conversion instruction "xlat", and it means that the type conversion has
been finished at compile time and needn't to be done at run time.
As for your original question, i.e. decimal b = a + 0;, the constant
expression 0 is converted to a decimal value at compile time. Sorry for the
confusion that my first reply causes.
In fact, I didn't see differences in the generated code between using "1"
and "1m". But I see a difference when using "1.0m":
I agree with Jon. 1.0m and 1m have different representations, so their
corresponding assembly code will be different.
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support