Understanding Microsoft Intermediate Language (MSIL)


The concept of having an intermediate code has been used successfully by the Java programming environment. Java includes a concept of bytecodes that are then interpreted (or JIT compiled) on execution. MSIL is similar to Java bytecode; the main difference is that IL is always compiled into machine code before execution. The compilation process can be done manually or automatically by the runtime. MSIL implements the Common Intermediate Language (CIL), which is part of the ECMA CLR Specification. MSIL itself is an object-oriented language and includes support of standard object-oriented features such as abstraction, inheritance, polymorphism, exception handling, events, properties, fields, and enumerations. Typically, you would write your application in a .NET programming language (such as C#, Visual Basic .NET, J#, or Managed C++), and your code would be converted into IL (as shown in Figure 2.4). However, if you are really enthusiastic or would like to know the inner details of IL, you can program in IL. IL code has one of the following entry points: DllMain, WinMain, or Main.

Figure 2.4. Creating and using MSIL.

Even though you start programming in .NET in the next chapter, you should try in this chapter to understand how MSIL works and what it looks like. For the sake of understanding, you can first create a simple .NET application, the classic Hello World.

 
 using System; namespace hks {  public class Hello  {    public static void Main()    {       Console.WriteLine("Hello .NET");    }  } } 

That is it; this is a simple C#-based .NET Hello World application. You can compile this application by using the command-line C# compiler.

 
 csc HelloWorld.cs 

If you run HelloWorld, you should be able to see the magical "Hello .NET" appear on your screen

 
 E:\hks\DotNet\Chapter 2>HelloWorld Hello .NET 

To understand how HelloWorld looks internally in MSIL, you can use the ildasm (.NET Framework IL Disassembler) utility provided with .NET SDK to dump the underlying MSIL code.

SHOP TALK : WOULD YOU PROGRAM IN MSIL?

You can actually program in MSIL. MSIL resembles the conventional assembly language for typical programming languages. Although it is always possible for someone to program in assembly language, and sometimes it might be that you could potentially write faster-performing code as well, you hardly ever do that because it is much more productive to write programs in a higher-level programming language (such as C#, for instance) and have the compiler generate the right MSIL code for you (in this scenario). However, understanding MSIL is very critical for tool developers. For instance, if you were to write a compiler that converts xyz programming language into MSIL, thus making it "CLR-enabled," you would definitely need to know MSIL in true depth. Apart from that, knowing MSIL could be useful but is definitely not necessary.


 
 ildasm /OUT=HelloWorld.il HelloWorld.exe //  Microsoft (R) .NET Framework IL Disassembler.  Version 1.1.4322.573 //  Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. .assembly extern mscorlib {   .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )  .ver 1:0:5000:0 } .assembly HelloWorld {   ...   .hash algorithm 0x00008004   .ver 0:0:0:0 } .module HelloWorld.exe ... // ============== CLASS STRUCTURE DECLARATION ================== // .namespace hks {   .class public auto ansi beforefieldinit Hello          extends [mscorlib]System.Object   {   } // end of class Hello } // end of namespace hks ... .namespace hks {   .class public auto ansi beforefieldinit Hello          extends [mscorlib]System.Object   {     .method public hidebysig static void             Main() cil managed     {       .entrypoint       // Code size       11 (0xb)       .maxstack  1       IL_0000:  ldstr      "Hello .NET"       IL_0005:  call       void [mscorlib]System.Console::WriteLine(string)       IL_000a:  ret     } // end of method Hello::Main     .method public hidebysig specialname rtspecialname             instance void  .ctor() cil managed     {        ...    } // end of method Hello::.ctor   } // end of class Hello ... 

You can actually program in MSIL. For instance, try changing the text Hello .NET to Hello World in the il code and then use the MSIL assembler tool ( ilasm .exe), also available with the .NET Framework, to recompile back to a .NET assembly. If you run the application now, you should see the modified text appear.

 
 ilasm HelloWorld.il HelloWorld.exe Hello World 

If you observe the generated MSIL code, you should also be able to see the metadata regarding the various assemblies contained and referenced by this application.

 
 .assembly extern mscorlib {     ...  .ver 1:0:5000:0 } .assembly HelloWorld {    ...  .hash algorithm 0x00008004    .ver 0:0:0:0 } 

If you are wondering whether you can really disassemble .NET applications so easily, what about intellectual property? There is a price to pay for a metadata-based execution model, but hold your question for a while. Chapter 14, "Security," answers this question by introducing Obfuscator technologies available for .NET assemblies to make the disassembly and deriving actual business logic much harder, still keeping the metadata-based model alive .

COMMON LANGUAGE SPECIFICATION

Common Language Specification (CLS) provides basic rules for language integration and interoperability. The specification contains information typically targeted toward compiler developers.




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net