Common Intermediate Language

Hello World in MSIL

Before I get an email or comment saying "why? why? are you covering MSIL?!" - for a good reason! MSIL is the only language that can use all of the CLR goodness! And in this yet to be determined part series I intend to not only to familiarize you with MSIL but also to write something in it!!

There are a few tools that you will need to become accustomed with, one of which you should already know very well!

  • ILDasm.exe - disassembler for MSIL executables
  • ILAsm.exe - tool used to compile MSIL into an executable

So then lets just jump in at the deep end. You guessed it a console app that prints something out to the console!! Genius!

Note: unlike C++ and C you can't just go ahead and say __asm { ... } and write some assembly within that block in C# or any other managed high level language - that would be a great feature and may be in future versions of the C#, C++/CLI, or VB.NET languages (looking past Orcas).

My name is Granville!

Just like with any executable you have an entry point - commonly this is the main method which is static, so lets go ahead and define the basic outline of that method now in MSIL.

Note: Unlike in a high level managed language like C# you don't have to define a method as part of a class, you can define a method globally.

.method static main()

The only other thing to note so far is that we use the ret keyword to return execution back to the controller.

Let's go ahead and print a few strings out with the help of the System.Console.WriteLine(...) method.

.method static void main()
	.maxstack 1
	ldstr "My name is Granville!!"
	call void [mscorlib]System.Console::WriteLine(string)
	ldstr "Indeed it is!!"
	call void [mscorlib]System.Console::WriteLine(string)

Just a quick run down of the added stuff. .maxstack states that we only require one slot on the stack - I know there are two strings and I will explain that next. First we push a string up onto the stack then call the System.Console.WriteLine method explicitly using the overloaded string argument version - the first string has now been popped off of the stack. We then load the second string and push that onto the stack then we pop it off again on the second call to WriteLine.

If you try compiling this using ilasm you will get an error as we need to include a manifest for our exe.

// Metadata version: v2.0.50215
.assembly extern mscorlib { }

.assembly Blog
  .ver 1:0:0:0

.module Part1.exe

Of any real note here is that we are referencing the mscorlib.dll where the System.Console.WriteLine method is defined, copy and paste that code above the main method and now invoke the ilasm tool.

Note: .il is the default extension for MSIL code.

> ilasm /exe
> My name is Granville!!
Indeed it is!!

You might also like...


About the author

Granville Barnettt United Kingdom

My name is Granville Barnett I have been a programmer now for quite some time mainly focusing on .NET technologies (C#), C++ and general research (algorithms, compilers etc)

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”