Peek Under the Hood With ILDASM
Use the IL Disassembler to understand Common Intermediate Language and improve your code.
October 30, 2009
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 CommonIntermediate Language and improve your code.
By Ken McNamee
This might sound crazy, but it's true: You can become asignificantly better ASP.NET developer by learning the Common IntermediateLanguage (CIL), which all .NET language compilers generate. Your best friend inthis endeavor is a .NET Framework tool named ILDASM (or, the IL Disassembler).
If you're not too familiar with the CIL, here's a quickexplanation. .NET language compilers create assemblies and executables that arenot fully compiled down to native machine code. Instead, the compiler parsesthe source-code files and outputs a DLL or EXE file that contains CIL - anon-processor-specific language. When you run the DLL or EXE, the .NETJust-In-Time (JIT) compiler converts this intermediate code to native machinecode.
Why should you learn CIL? After all, the compilers knowhow to deal with it already. Well, the developers who created ASP.NET did anexcellent job of abstracting away most of the gory details that go intohandling an HTTP request using an object-oriented platform. Perhaps they evenmade it too easy - you really need to know those gory details to takeyour 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. Everyother .NET language offers only a subset of what you can do with CIL. Thatsaid, however, don't be seduced into thinking you should start writing all yourWeb pages in CIL from now on. Instead, learn to read CIL well enough at leastto get the gist of what is going on in a block of code. For example, take thesimple Web page displayed in Figure 1, which is written in C#. You simply havea Label control instantiated using the tag, and you thenassign a value to its Text property during the Page's Load event.
<%@ Page Language="C#" %>
protected override voidOnLoad(EventArgs args){ lblExample.Text ="Hello World"; }
Figure 1. This simple Web page hides some of the gorydetails that actually occur during the execution of the page.
In Figure 2, you can see only the OnLoad method, exceptthis time it's in CIL. Looks pretty ugly, doesn't it? But once you begin to understandthe commands and how variables and object values are operated on, it becomesmuch 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 sourcefor the Load event in the simple Web page doesn't look quite so simple.
The first thing you might notice is that CIL is moreverbose than high-level languages such as VB .NET and C#. This is because CILhides nothing from you and consequently expects you to give it moreinstructions - CIL assumes very little. Also, CIL has no equivalent to Importsor using statements. Every class and method call must be spelled out with thefully qualified namespace and the assembly in which the class is located. So,EventArgs args in C# becomes [mscorlib] System.EventArgs args in CIL. As youcan imagine, some of the lines can get very, very long because of thisrequirement.
After the method declaration, you'll encounter the.maxstack instruction. This statement begins the major hurdle to comprehendingCIL effectively because it involves the evaluation stack, which is somethingyou never need to worry about in most high-level languages. You can think ofthe evaluation stack as the placeholder for string or numerical values youwould like to perform some operation on. The .maxstack instruction simply tellsthe compiler what the maximum number of items will be that can ever be placedon the stack during that method's lifetime.
Using the example in Figure 1, you can assign a stringvalue to the Text property of a Label control in VB .NET or C# in onestraightforward line of code. In CIL, things aren't so simple. As you can seein Figure 2, you first must load the instance of the Label control onto theevaluation stack, then load the string value, and finally call the set_Textmethod of the Label class, which pops the two values off the stack and assignsthe value. It's not intuitive, but this is what is actually going on in everyproperty assignment line of code you write. Also notice that in Figure 2, you callthe set_Text method of the Label class and don't assign the Text property valuedirectly. This is because the .NET Framework converts your properties and getsand 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 Worddocuments containing the technical details of the platform's architecture,including CIL. Usually, you can find these documents in Tool DevelopersGuideDocs, underneath your FrameworkSDK root. Also, many books are beingpublished that are fully devoted to CIL, and others have chapters or sectionswith good CIL information.
But you don't need to go to the bookstore to gain a deeperunderstanding of CIL. You can start by writing simple Web pages, executingthem, then opening the generated assemblies in the ILDASM tool, which you canfind in the bin folder under your FrameworkSDK root. As you can see in Figure3, ILDASM is a GUI tool that gives you a tree-view representation of all thenamespaces, classes, and methods located within an assembly or executable. Ifyou double-click on a class's member, you are presented with the member's CILcode. 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-clickon an assembly using Windows Explorer and see the layout of the classesimmediately.
Figure 3. The ILDASM tool displays a highly convenient andeasy-to-navigate tree-view representation of all the namespaces, classes, andmethods contained within a .NET assembly or executable.
ILDASM has other options that allow you to see more orless detail as you need it. You also can export the entire assembly as CILusing the Dump option, which can be much less tedious than double-clicking oneach class member. With the Dump Treeview command, you can get a poor man'sdocumentation 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 simplelisting of your assembly's contents.
Although not absolutely necessary, I highly recommendusing ILDASM on a regular basis and learning as much about CIL as you can. It'snot nearly as arcane as it looks at first glance, and understanding CIL reallyis about understanding how .NET works as a platform. Because ASP.NET is notmuch different than any other .NET application that runs in an AppDomain, themore you know about the platform, the better a Web developer you can be.
The sample code in thisarticle is available for download.
Ken McNamee is an independent consultant who works withcompanies in need of highly scalable, data-driven Web applications. And whodoesn't need one of those these days? Prior to this, he led a team ofdevelopers 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 aboutthis article to [email protected] include the article title and author.
Read more about:
MicrosoftAbout the Author
You May Also Like