Microsoft Intermediate Language

The CLI also specifies an environment for executing the code targeting the CLI. The environment is called the Virtual Execution System (VES). Among other things, the VES defines a hypothetical machine with an associated machine model and instructions. The machine instructions are defined in a language called Common Intermediate Language (CIL). A detailed description of the CIL instruction set can be found in the ECMA specifications.

As you may have guessed, Microsoft's implementation of CIL is called Microsoft Intermediate Language (MSIL).

Consider the following C# code to multiply two numbers:

public static int Multiply(int i, int j) {
  int k = i * j;
  return k;
}

When this code is compiled, the compiler creates the following MSIL code in the generated assembly (recall that you can use the IL disassembler to view the contents of an assembly). I have added comments to each instruction for better readability.

.method public hidebysig
     static int32  Multiply(int32 i, int32 j) cil managed
 {
   .maxstack  2 ; maximum number of items this
                ; method will push on the stack
   .locals (int32 V_0) ; local variables this method needs
                 ; V_O is the first local variable
    ldarg.0     ; push the first argument onto the stack
    ldarg.1     ; push the second argument onto the stack
    mul       ; multiply the two arguments
    stloc.0     ; pop the result from the stack to V_0
    ldloc.0     ; push V_0 to the stack (return value)
    ret         ; return from the method
 } // end of method MyApp::Multiply

Note that while traditional machine language instruction sets use registers and stacks, the MSIL instruction set uses only the stack. In the above code, the two arguments are pushed onto the stack and the multiply instruction is called. This instruction pops the two values from the stack and pushes the result back onto the stack. The code then pops the value from the stack to the local variable. The local variable is pushed back to the stack as the stack is also used to place the return value of the method. Finally, the method returns to its caller.

Under MSIL, copying values from memory to the stack is referred to as loading and copying values from the stack to memory is referred to as storing.

More information on the IL instruction set can be found in the ECMA CLI documents at www.ecma.ch. A copy can also be found under the subdirectory “Tools Developers Guide” in the directory where the .NET Framework SDK is installed.

Protecting the Intellectual Property

Given that it is so easy to disassemble an assembly, it seems feasible for someone to reverse engineer the logic of your program. Is there any form of protection for your code?

Microsoft is currently working on a basic obfuscator that will mangle all non-public names in an assembly. Although not a fool-proof scheme, it will at least make disassembled programs harder to comprehend.


Hello World in IL

It is possible to write a program in MSIL although most programmers would prefer using a higher level language. The following MSIL code shows how to display “Hello World!” to the console:

/*************************************************
  Hello World program written in MSIL
  NOTE: Anything that starts with a period is
        a directive for the assembler
*************************************************/

// This module is the holder of assembly manifest
.assembly hello {}

.method static public void MyMain() cil managed
{
     // Mark this method as the entry point
     .entrypoint

     // Push a string onto the stack
     ldstr "Hello World!"

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

     // return from the method
     ret
}

The entrypoint directive informs the IL assembler to mark the specified method as an entry point. When the execution engine loads the application, it starts executing code from the specified entry point. When we write code in a higher-level language such as C#, the compiler automatically assumes that the entry point is static method named Main.

Obviously, the entry point is needed only for an EXE-based assembly. For a DLL-based assembly, the application responsible for loading it dictates what method(s) to call.

The framework provides a tool called IL assembler (ilasm.exe) for assembling MSIL files. Assuming the above code is stored in a file HelloWorld.il, the following command line will generate an assembly named HelloWorld.exe.

ilasm.exe HelloWorld.il

This concludes our short introduction to MSIL. Let's now examine the process of executing managed code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.243