Geeks With Blogs
Neil Smith [blog]code til ur fingers bleed

I’ve been looking into MSIL mostly comparing the generated code from both vb and c# as I’m about to start an app where execution speed is going to be important, I’ll need to keep boxing down to a minimum, c++ is not an option because of time constraints. I’ve found a few handy tools along the way.

 

Excellent tool this, version 3 of ILIDE can be downloaded from here http://nida.se/ilide/ unfortunately it appears to be tied to version 1.1 of the framework and therefore knows nothing of generics etc.  It gives intellisense functionality for IL listings and lets you compile and run IL programs from the editor, very nice.

 

.net explorer is an excellent tool along similar lines to Lutz Roeders Reflector (http://www.aisto.com/roeder/dotnet/).  The same exe integrates with all 3 framework versions and integrates seamlessly with their salamander obfuscator.

http://www.remotesoft.com/dotexplorer/download.html

 

There are some pretty cool things you can do in MSIL that you can’t do in some of the higher level languages, take this listing for instance :-

 

.assembly ns_swap {}

.namespace nsMSIL

{

.class private auto ansi beforefieldinit nsswap

extends [mscorlib]System.Object

{

.method private hidebysig static void Main(string[] args) cil managed

{

.entrypoint

.maxstack 2

.locals init (int32,int32)

IL_0000: nop

IL_0001: ldc.i4 909

IL_0006: stloc.0

IL_0007: ldc.i4 303

IL_000c: stloc.1

IL_000d: ldloc.0

IL_000e: ldloc.1

IL_000f: xor

IL_0010: stloc.0

IL_0011: ldloc.1

IL_0012: ldloc.0

IL_0013: xor

IL_0014: stloc.1

IL_0015: ldloc.0

IL_0016: ldloc.1

IL_0017: xor

IL_0018: stloc.0

IL_0019: ldstr "first = "

IL_001e: ldloc.0

IL_001f: box [mscorlib]System.Int32

IL_0024: call string [mscorlib]System.String::Concat(object, object)

IL_0029: call void [mscorlib]System.Console::WriteLine(string)

IL_002e: nop

IL_002f: ldstr "second = "

IL_0034: ldloc.1

IL_0035: box [mscorlib]System.Int32

IL_003a: call string [mscorlib]System.String::Concat(object, object)

IL_003f: call void [mscorlib]System.Console::WriteLine(string)

IL_0044: nop

IL_0045: call string [mscorlib]System.Console::ReadLine()

IL_004a: pop

IL_004b: ret

}

.method public hidebysig specialname rtspecialname instance void .ctor() cil managed

{

.maxstack 8

IL_0000: ldarg.0

IL_0001: call instance void [mscorlib]System.Object::.ctor()

IL_0006: ret

}

}

}

 

 

This swaps the value of two variables without using a third, pretty cool, the c# ildasm output will give you :-

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace nsMSIL

{

    class nsswap

    {

        static void Main(string[] args)

        {

            int first = 909;

            int second = 303;

 

            first ^= second;

            second ^= first;

            first ^= second;

 

            Console.WriteLine("first = " + first);

            Console.WriteLine("second = " + second);

            Console.ReadLine();

        }

    }

}

 

I’m trying to get hold of Expert .net 2.0 IL Assembler by Serge Lidin but can’t find it anywhere, Amazon have it listed but with “Usually despatched within 4-6 weeks”…

Posted on Tuesday, June 27, 2006 8:53 AM | Back to top


Comments on this post: Analysing MSIL

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Neil Smith | Powered by: GeeksWithBlogs.net