Wednesday, August 12, 2009

:: Microsoft Intermediate Language ::

Microsoft Intermediate Language (MSIL)
Introduction

MSIL stands for Microsoft Intermediate Language, similar to that of Java Byte code that is formed when you compile a Source Code. The main purpose of this Intermediate code formation is to have a platform independent code. That is once MSIL/Java Byte Code is available you can run on any platform provided appropriate run time environments are installed on the specific platform you wish to run. JVM in case of Java and CLR in case of .NET

The ILDasm (Intermediate Language Disassembler) program that ships with the .NET Framework SDK (You can find it in C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\ildasm.exe) allows the user to see MSIL code in human-readable format. By using this utility, we can open any .NET executable file (EXE or DLL) and see MSIL code.

Just go to the path and click on "ildasm.exe" file and you will see a window which is shown below



Then you can open the .NET executable file by going to "File menu and click on Open link". In the open wizard select the dll that you want to view. Here i opened PringString.exe file and then the above window will look like as shown below.



Then you can manifest of the file by clicking on the MANIFEST and which is shown in below image.



You can view function by clicking on function name and is shown in the below image.



The ILAsm program (Intermediate Language Assembler) generates an executable file from the MSIL language. We can find this program in the WINNT\Microsoft.NET\Framework\vn.nn.nn directory.

Learning MSIL

Learning MSIL gives a user the chance to understand some things that are hidden from a programmer working with C# or VB.NET. Knowing MSIL gives more power to a .NET programmer. We never need to write programs in MSIL directly, but in some difficult cases it is very useful to open the MSIL code in ILDasm and see how things are done.

All operations in MSIL are executed on the stack. When a function is called, its parameters and local variables are allocated on the stack. Function code starting from this stack state may push some values onto the stack, make operations with these values, and pop values from the stack.
Execution of both MSIL commands and functions is done in three steps:
  1. Push command operands or function parameters onto the stack.
  2. Execute the MSIL command or call function. The command or function pops their operands (parameters) from the stack and pushes onto the stack result (return value).
  3. Read result from the stack.

Steps 1 and 3 are optional. For example, the void function doesn't push a return value to the stack.

The stack contains objects of value types and references to objects of reference type. Reference type objects are kept in the heap.

MSIL commands used to push values onto the stack are called ld... (load). Commands used to pop values from the stack are called st... (store), because values are stored in variables. Therefore, we will call the push operation loading and the pop operation storing.

Sample Program

.assembly PrintString {}

/* Console.WriteLine("Hello, World)"*/

.method static public void main() il managed

{ .entrypoint // this function is application entry point
.maxstack 8

// *****************************************************

// Console.WriteLine("Hello, World)";

// *****************************************************

ldstr "Hello, World" // load string onto stack

// Call static System.Console.Writeline function

// (function pops string from the stack)

call void [mscorlib]System.Console::WriteLine(class System.String)

// *****************************************************************

ldstr "Press Enter to continue"

call void [mscorlib]System.Console::WriteLine(class System.String)

// Call the System.Console.Read function

call int32 [mscorlib]System.Console::Read()

// The pop instruction removes the top element from the stack.

// (remove number returned by Read() function)

pop

// *****************************************************************
ret

}

Save this file as PrintString.il on your desktop. "il" is the extension of the intermediate language file.

Then go to VS command prompt from "Start/Programs/Visual Studio .Net 2003/Visual Studio .Net tool/Visual Studio .Net 2003 Command Prompt". Then point to your desktop from the command prompt and execute your PrintString.il file by typing the following command.

-- ilasm printstring.il

Then a PrintString.exe file is created on the desktop. Clicking on the .exe file we can see the output of the program that we have written. The same .exe i opened with the ILDASM for viewing the code in the begining.

Even we can view the IL code of the .Net compatible file by tying following command in the command prompt of Visual Studio.

-- ildasm printstring.exe

By pressing the enter key on keyboard will will see the window shown in the second image above. So this way is very easy than the above one.

1 comment: