MSIL Trivia - 4 (Spot the difference)
I have created a very simple C# console application which gives me the following MSIL code when compiled... The next screenshot has almost the same code but with some special difference. Can you tell me what could have caused it and is it good or bad?
I will answer this in the comments (or next post as necessary) .
Until then...
Cheers,
Rahul
Quote of the day:
I'm a great believer in luck, and I find the harder I work the more I have of it. - Thomas Jefferson
Comments
Anonymous
March 11, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/03/12/msil-trivia-4-spot-the-difference-2/Anonymous
March 11, 2008
You local variable (V_0) is of type Int16. One of those 16 bits is used for the sign, leaving 15 for a maximum value of 2^15 or 0x7FFF. You then create a Int32 value, set to 0x7FFF, and then assign it to the above local. Next you output the value. Up to this point, everything is OK. Next, you increment V_0 by one. As already noted, signed 16-bit values have a maximum value of 32767, so it naturally overflows to -32768. In your next version, you use checked addition and checked conversion (back to an Int16). This check detects the overflow and throws the appropriate exception. Good or bad, it really depends on what you are trying to do. Some algorithms rely on rollovers. Human arithmetic typically do not expect such behavior.Anonymous
March 12, 2008
Perhaps, either you used C#(unless you used the Checked option, which is not the default in C#, by default it is unchecked) in the first and VB.Net in the second. There is a check for overflow (add.ovf) only in VB.Net (as opposed to add in C# by default). Conv.ovf.i2 Converts an int16 on the stack to int32 and throws an exception on overflow. So if your data overflowed in the second case you will get an exception, and in the 1st case you may see an unexpected result. This is what i feel.Anonymous
March 13, 2008
The comment has been removed