I have been a .Net developer since long time and I have developed fairly complex and large scale applications using different .Net framework technologies like C#, ASP.Net, Windows Forms, WCF etc. I have conducted numerous training sessions on these technologies for my team members and colleagues as well. Most of the time my focus was on to explain the language features like ‘Abstract classes’, ‘Interfaces’, ‘Virtual methods’, ‘LINQ’ etc. However I did not pay much attention on the compiler side. That is, how C# compiler compiles source code, how Intermediate Language [IL] looks like, how can I disassemble the code of a third party library; make few modifications to it and assemble it again? Are there any tools available to do such things?
So I thought to take some time out and dissect first program that almost all the developers write when they try to adopt new language, which is Hello World.
If you want to perform the steps mentioned in this article, you would need .Net framework [any version, I have 4.5] installed on your machine. Optionally you can install Visual Studio to build the program. Since we are building simple Hello World application, I assume that you have basic understanding of .Net framework and C# language.
Writing Hello World
Okay, so easy part first. Assuming you have .Net framework and Visual Studio [optionally] installed on your machine, create a C# Console Application which displays Hello World output on the desktop. Again, this is fairly basic requirement, so I am not going into any details of it.
Now you might have heard it many times, .Net is a language neutral framework. That means you can develop an application using different .Net framework compatible languages like C#, VB.Net, F#, Iron Python and many more. As a part of program execution, Common Language Runtime [CLR] first converts C# program into Microsoft Intermediate Language [MSIL] and then it converts it into machine instruction set using Just-In-Time [JIT] compiler.
Below diagram shows how language compilers, CLR and Just-In-Time compiler participates during .Net program compilation and execution.
There are useful tools available to view MSIL code like ILSpy, .Net Reflector, ILDASM, ILASM etc. While ILSpy, ILDASM and ILASM are free tools, .Net Reflector is a commercial product from Red-gate. In this article, I will explain the IPSpy, ILDASM and ILASM tools.
ILSpy is open source .Net assembly browser and de-compiler. Assuming you have Hello World program assembly built now, open it using ILSpy editor. Expand the tree structure on the left hand side and double click on ‘Main’ method; it should display the method definition on the right side panel.
Note the language dropdown in the toolbar; you can select VB or C# to display the ‘Main’ method source code in VB or IL.
Please note that we have implemented ‘Hello World’ program using C#, however we can disassemble it to VB or IL with the help of ILSpy.
Intermediate Language Disassembler [ILDASM] is a tool which gets installed along with Visual Studio and Windows SDK. As the name suggests, it is used to disassemble a portable executable file [.exe / .dll] which contains MSIL. To view the intermediate language of our Hello World program, open Visual Studio Command prompt and navigate to assembly [HelloWorld.exe] path and type below mentioned command.
This should open ILDASM tool as shown in below screenshot.
To view the MSIL for the Main method, simply double click on the Main function icon and it will display the metadata of Main function as shown below
Note, ILDASM tool does not contain all the features of ILSpy, however it allows you to export MSIL code into a text file. Simply click on File -> Dump menu option, select ‘Dump IL Code’ option and click Ok. Open the file in your favorite text editor and it should contain assembly information, .Net framework version information and the class members declaration.
Intermediate Language Assembler [ILASM] is used to generate portable executable from Microsoft Intermediate Language [MSIL]. Similar to ILDASM, this tool gets installed along with Visual Studio and Windows SDK.
Assuming that you have created an intermediate language file using ILDASM tool earlier, now we will make changes to IL file and generates portable executable using ILASM tool.
Open IL file using your favorite editor and change ‘Hello World’ input string to ‘Hello MSIL World’ and save it. Open Visual Studio Command Prompt and navigate to HelloWorld.IL file. Type ILASM command as shown below
Ilasm HelloWorld.il /output:HelloMSILWorld.exe
This should create a HelloMSILWorld.exe file at the same location where HelloWorld.IL file exists. Double click on HelloMSILWorld.exe and you should get ‘Hello MSIL World’ output on command prompt.
In this article, we have covered different tools available to view MSIL code and how you can assemble / disassemble portable executable using these tools.
I hope it was useful to you.