Peek Under the Hood With ILDASM

Use the IL Disassembler to understand Common Intermediate Language and improve your code.

ToolKit

LanguageS: All .NET Languages | CIL

ASP.NET VERSIONS: 1.0 | 1.1

 

Peek Under the Hood With ILDASM

Use the IL Disassembler to understand Common Intermediate Language and improve your code.

 

By Ken McNamee

 

This might sound crazy, but it's true: You can become a significantly better ASP.NET developer by learning the Common Intermediate Language (CIL), which all .NET language compilers generate. Your best friend in this endeavor is a .NET Framework tool named ILDASM (or, the IL Disassembler).

 

If you're not too familiar with the CIL, here's a quick explanation. .NET language compilers create assemblies and executables that are not fully compiled down to native machine code. Instead, the compiler parses the source-code files and outputs a DLL or EXE file that contains CIL - a non-processor-specific language. When you run the DLL or EXE, the .NET Just-In-Time (JIT) compiler converts this intermediate code to native machine code.

 

Why should you learn CIL? After all, the compilers know how to deal with it already. Well, the developers who created ASP.NET did an excellent job of abstracting away most of the gory details that go into handling an HTTP request using an object-oriented platform. Perhaps they even made it too easy - you really need to know those gory details to take your ASP.NET knowledge to the next level.

 

Take the Red Pill

VB .NET and C# don't actually expose the full power of the .NET Framework. In fact, the only language that offers this power is CIL. Every other .NET language offers only a subset of what you can do with CIL. That said, however, don't be seduced into thinking you should start writing all your Web pages in CIL from now on. Instead, learn to read CIL well enough at least to get the gist of what is going on in a block of code. For example, take the simple Web page displayed in Figure 1, which is written in C#. You simply have a Label control instantiated using the tag, and you then assign a value to its Text property during the Page's Load event.

 

<%@ Page Language="C#" %>

  

Figure 1. This simple Web page hides some of the gory details that actually occur during the execution of the page.

 

In Figure 2, you can see only the OnLoad method, except this time it's in CIL. Looks pretty ugly, doesn't it? But once you begin to understand the commands and how variables and object values are operated on, it becomes much easier.

 

.method family hidebysig virtual instance void OnLoad(

  class [mscorlib]System.EventArgs args) cil managed

{

  .maxstack  2

  ldarg.0

  ldfld    class [System.Web] System.Web.UI.WebControls.Label

    ASP.example1_aspx::lblExample

  ldstr    "Hello World"

  callvirt instance void [System.Web]

    System.Web.UI.WebControls.Label::set_Text(string)

  ret

}

Figure 2. The CIL source for the Load event in the simple Web page doesn't look quite so simple.

 

The first thing you might notice is that CIL is more verbose than high-level languages such as VB .NET and C#. This is because CIL hides nothing from you and consequently expects you to give it more instructions - CIL assumes very little. Also, CIL has no equivalent to Imports or using statements. Every class and method call must be spelled out with the fully qualified namespace and the assembly in which the class is located. So, EventArgs args in C# becomes [mscorlib] System.EventArgs args in CIL. As you can imagine, some of the lines can get very, very long because of this requirement.

 

After the method declaration, you'll encounter the .maxstack instruction. This statement begins the major hurdle to comprehending CIL effectively because it involves the evaluation stack, which is something you never need to worry about in most high-level languages. You can think of the evaluation stack as the placeholder for string or numerical values you would like to perform some operation on. The .maxstack instruction simply tells the compiler what the maximum number of items will be that can ever be placed on the stack during that method's lifetime.

 

Using the example in Figure 1, you can assign a string value to the Text property of a Label control in VB .NET or C# in one straightforward line of code. In CIL, things aren't so simple. As you can see in Figure 2, you first must load the instance of the Label control onto the evaluation stack, then load the string value, and finally call the set_Text method of the Label class, which pops the two values off the stack and assigns the value. It's not intuitive, but this is what is actually going on in every property assignment line of code you write. Also notice that in Figure 2, you call the set_Text method of the Label class and don't assign the Text property value directly. This is because the .NET Framework converts your properties and gets and sets them to corresponding get_ and set_ methods.

 

Using ILDASM

You have many options for understanding how CIL works. First, every copy of the .NET Framework SDK includes several large Word documents containing the technical details of the platform's architecture, including CIL. Usually, you can find these documents in \Tool Developers Guide\Docs, underneath your FrameworkSDK root. Also, many books are being published that are fully devoted to CIL, and others have chapters or sections with good CIL information.

 

But you don't need to go to the bookstore to gain a deeper understanding of CIL. You can start by writing simple Web pages, executing them, then opening the generated assemblies in the ILDASM tool, which you can find in the \bin folder under your FrameworkSDK root. As you can see in Figure 3, ILDASM is a GUI tool that gives you a tree-view representation of all the namespaces, classes, and methods located within an assembly or executable. If you double-click on a class's member, you are presented with the member's CIL code. I started learning CIL simply by doing side-by-side comparisons of my C# code and the resultant CIL generated by ILDASM. To make things a little easier, I even associated the .dll extension with ILDASM so I simply can double-click on an assembly using Windows Explorer and see the layout of the classes immediately.

 


Figure 3. The ILDASM tool displays a highly convenient and easy-to-navigate tree-view representation of all the namespaces, classes, and methods contained within a .NET assembly or executable.

 

ILDASM has other options that allow you to see more or less detail as you need it. You also can export the entire assembly as CIL using the Dump option, which can be much less tedious than double-clicking on each class member. With the Dump Treeview command, you can get a poor man's documentation that displays all the classes and methods in a simple text file. This doesn't export any CIL, but it can be a handy way to create a simple listing of your assembly's contents.

 

Although not absolutely necessary, I highly recommend using ILDASM on a regular basis and learning as much about CIL as you can. It's not nearly as arcane as it looks at first glance, and understanding CIL really is about understanding how .NET works as a platform. Because ASP.NET is not much different than any other .NET application that runs in an AppDomain, the more you know about the platform, the better a Web developer you can be.

 

The sample code in this article is available for download.

 

Ken McNamee is an independent consultant who works with companies in need of highly scalable, data-driven Web applications. And who doesn't need one of those these days? Prior to this, he led a team of developers in re-architecting the Home Shopping Network's e-commerce site, HSN.com, to 100 percent ASP.NET with C#. E-mail him at [email protected].

 

Tell us what you think! Please send any comments about this article to [email protected]. Please include the article title and author.

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish