Saturday, May 10, 2014

C#, VB, and CIL Rosetta Stone

In the last post I showed the disassembled C# code created by Telerik JustDecompile, this time we will look at the underlying CIL (Common Intermediate Language) for the same program.  If you haven’t looked at CIL, the thought might be a bit overwhelming, but it isn’t nearly as bad as you think.  Below is the output associated with the main class from the command line compilation of the program:

And here is the output from the Visual Studio compilation of the same program:

The command line included a couple of nop (no or null operation) instructions to (I believe) align the instructions for better cache hits.  Be that as it may, looking at this code you can see a nearly one to one relationship between it and the C#:
using System;

internal class Hello
{
    public Hello()
    {
    }

    private static void Main()
    {
        Console.WriteLine("Hello, World");
    }
}

In fact, since JustDecompile allows it let’s look at the generated Visual Basic.NET code equivalent:
Imports System
 
Friend Class Hello
    Public Sub New()
        MyBase.New()
    End Sub
 
    Private Shared Sub Main()
        Console.WriteLine("Hello, World")
    End Sub
End Class

For a program this simplistic, all three look close enough to the same that you can follow from one to the other.  C# is a terse language and CIL is a high level assembly.

The next section of the specification has a bit larger program included to introduce the idea of name spaces and scope.  We will investigate that next.

No comments:

Post a Comment