It is often necessary to convert between types. The conv instruction casts between primitive types. The instruction has a single operand, which is the destination type of the cast. A conversion can cause an overflow when the memory size of the source is larger than the target. For example, this could happen when casting from a 64-bit integer to a 32-bit integer. The high-order bits are trimmed when an overflow occurs, which naturally changes the normal results. It’s important to note that when overflows are ignored, subtle bugs are possible. The conv.ovf instruction prevents an overflow from going undetected. When an overflow occurs, an overflow exception is raised.
The following code contains an overflow condition, where the cast conversion raises the overflow exception. This code is not protected from exceptions, and the exception will terminate the application:
.assembly extern mscorlib {} .assembly application {} .namespace Donis.CSharpBook { .class Starter { .method static public void Main() il managed { .entrypoint ldc.i8 4000000000 conv.ovf.i4 pop ret } } }
18.118.7.102