Thursday, February 21, 2008

.Net Intermediate Language

.Net Intermediate Language or IL is simple, object oriented, stack based assembly language of .Net.
All .Net languages gets converted to IL on compilation.

IL is case sensitive and and follows the same case pattern that of C#, ie. initial capital Eg. System.Console.WriteLine where S C W and L and capitalized respectively.

This blog assumes that you are familiar with C# if not please refer my blog Exploring C# Language : The Console Way.

IL statements can be broadly classified into two IL directives and IL instructions. Any statement that starts with . is an IL directive examples are .assembly, .module, etc. and others are IL instructions.

Namespaces ares specified inside square brackets.

Hello World

.assembly '1Hello'{}

.method void Main()
{
.entrypoint
ldstr "Hello World" call void [mscorlib] System.Console::WriteLine(string)
ret
}



For creating an assembly out of the IL we can use ilasm (Intermediate Language assembler).















Memory Variable Console::ReadLine and String::Concat methods


In IL all local variables are defined inside .locals directive. One nice thing about IL generated by .Net compilers are the variable names are mangled. Here it creates a string memory variable.

.assembly '2MemVar'{}

.method void Main()
{
.entrypoint

.locals init (string V_0)
ldstr "Enter your name : " call void [mscorlib]System.Console::WriteLine (string)
call string [mscorlib]System.Console::ReadLine()
stloc.0
ldstr "Hello "
ldloc.0
call string [mscorlib]System.String::Concat (string, string)
call void [mscorlib]System.Console::WriteLine(string)
ret
}


Integer Memory Variable and add

.assembly '4Add'{}

.method public hidebysig static void Main() cil managed
{
.entrypoint

.locals init (int32 V_0, int32 V_1, int32 V_2)

ldstr "Enter first integer :"
call void [mscorlib]System.Console::WriteLine(string)

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

[mscorlib]System.Int32::Parse(string)
stloc.0
ldstr "Enter second integer :"
call void [mscorlib]System.Console::WriteLine(string)

call string [mscorlib]System.Console::ReadLine()
call int32 [mscorlib]System.Int32::Parse(string)
stloc.1
ldloc.0
ldloc.1
add
stloc.2
ldloca.s V_2
call instance string [mscorlib]System.Int32::ToString()
call void [mscorlib]System.Console::WriteLine(string)
ret
}



Form
.assembly extern System.Windows.Forms
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 }
.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0
}
.assembly '4Form' { }
.module '4Form.exe'
.class private auto ansi beforefieldinit frmHello extends [System.Windows.Forms] System.Windows.Forms.Form
{
.method public hidebysig static void Main() cil managed
{
.entrypoint newobj instance void frmHello::.ctor() call void [System.Windows.Forms] System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
ret
}
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
ldarg.0 call instance void [System.Windows.Forms] System.Windows.Forms.Form::.ctor()
ret
}
}

Button and Form Text Property
.assembly extern System.Windows.Forms {
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 }
.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 } .assembly '5Button' { }
.module '5Button.exe'
.class private auto ansi beforefieldinit frmHello extends [System.Windows.Forms]System.Windows.Forms.Form
{
.field private class [System.Windows.Forms]System.Windows.Forms.Button btnHello
.method public hidebysig static void Main() cil managed
{
.entrypoint newobj instance void frmHello::.ctor()
call void [System.Windows.Forms]System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
ret
}
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
ldarg.0 call instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor() ldarg.0
ldstr "Shalvin"
callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
ldarg.0 newobj instance void [System.Windows.Forms]System.Windows.Forms.Button::.ctor() stfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello
ldarg.0 ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello ldstr "Hello"callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
ldarg.0
call instance class [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection [System.Windows.Forms]System.Windows.Forms.Control::get_Controls() ldarg.0 ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection::Add(class [System.Windows.Forms]System.Windows.Forms.Control)
ret
}
}

Event Handling
.assembly extern System.Windows.Forms
{ .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 }
.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 2:0:0:0 } .assembly extern System.Drawing {
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ).ver 2:0:0:0 }.assembly '6Click' { } .module '6Click.exe' .class private auto ansi beforefieldinit frmHello extends [System.Windows.Forms]System.Windows.Forms.Form {
.field private class [System.Windows.Forms]System.Windows.Forms.Button btnHello .method public hidebysig static void Main() cil managed
{
.entrypoint newobj instance void frmHello::.ctor() call void [System.Windows.Forms]System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
ret
}
.method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
{
ldarg.0 call instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor() ldarg.0ldstr "Shalvin" callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
ldarg.0 newobj instance void [System.Windows.Forms]System.Windows.Forms.Button::.ctor() stfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello ldarg.0 ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello ldc.i4.s 50 ldc.i4.s 50 newobj instance void [System.Drawing]System.Drawing.Point::.ctor(int32, int32) callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Location(valuetype [System.Drawing]System.Drawing.Point)
ldarg.0 ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHelloldarg.0
ldftn instance void frmHello::btnHello_Click(object, class [mscorlib]System.EventArgs) newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::add_Click(class [mscorlib]System.EventHandler)
ldarg.0
ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello
"Hello"
callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
ldarg.0
call instance class [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection [System.Windows.Forms]System.Windows.Forms.Control::get_Controls()
ldarg.0
ldfld class [System.Windows.Forms]System.Windows.Forms.Button frmHello::btnHello callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control/ControlCollection::Add(class [System.Windows.Forms]System.Windows.Forms.Control)
ret
}
.method private hidebysig instance void btnHello_Click(object sender, class [mscorlib]System.EventArgs e) cil managed
{
ldstr "Hello" call valuetype [System.Windows.Forms]System.Windows.Forms.DialogResult [System.Windows.Forms]System.Windows.Forms.MessageBox::Show(string) popret
}
}

Related Blogs
Creating and Consuming Class Library in C#, Object Initializer
C# WPF Creating a Class with Constructors

No comments:

Post a Comment