Library tutorials & articles

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()
{
	.entrypoint
	ret
}

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()
{
	.entrypoint
	.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)
	ret
}

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 Part.il
> My name is Granville!!
Indeed it is!!

Comments

  1. 16 Oct 2009 at 03:38

    can u please do sove this program.......and send it at my e-mail add...rach_dizon2002@yahoo.com 1. create a program called MathInteger.java that presents a menu tothe user nwith choices for (1) adding first n integer (2) multiplying the first n integer and (3) quitting.use do while loop.Also switch statement for option 1 and 2. 2. create a program called SumIntegers.java allows the user to enter exactly 10 n positive integers.If the user accidentally enters negative integer the program should use continue statement to skip the entry.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Common Intermediate Language.

Leave a comment

Sign in or Join us (it's free).

Granville Barnettt 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)

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

We'd love to hear what you think! Submit ideas or give us feedback