.NET平台的編譯器會將高級語言(C#,VB.NET,F#)編譯成MSIL(微軟中間語言)格式。熟悉MSIL語言,可以讀懂一些加密程序混淆過的算法,這些算法幾乎不能還原成高級語言,但是可以還原成MSIL語言。也可以知道一些高級語言之外的關於CLR的特性,比如多模塊程序集,全局靜態方法等等。一些.NET保護加密程序也是運用MSIL平台的特性。
閱讀本篇文章,假設您已經對這個語言有基本的了解,我會列舉這個語言的基本語言應用例子,供參考。
1 Hello world
.method static void main() { .entrypoint .maxstack 1 ldstr "Hello world!" call void [mscorlib]System.Console::WriteLine(string) ret }
在控制台上打印Hello world字符串。MSIL以entrypoint表示入口方法,而不一定是C#中規定的Main方法。
2 使用局部變量
.locals init (int32 first, int32 second, int32 result)
上面的語法,定義了三個局部變量,它的名稱分別是first,sencond,result。
下面的代碼讀取用戶在控制台上的輸入值,並調用Parse方法,把結果保存在first局部變量中。
ldstr "First number: " call void [mscorlib]System.Console::Write(string) call string [mscorlib]System.Console::ReadLine() call int32 [mscorlib]System.Int32::Parse(string) stloc first
調用add方法,將frist和second的值加起來,保存到resutl局部變量中
ldloc first ldloc second add stloc result
最后,在控制台上打印結果值
ldstr "{0} + {1} = {2}" ldloc first box int32 ldloc second box int32 ldloc result box int32 call void [mscorlib]System.Console::WriteLine(string, object, object, object)
因為三個局部變量是int32類型,調用WriteLine方法時要傳入object類型,所以要裝箱(box)。
3 定義類型
新建一個calss/enum/struct即為定義一種新的程序類型,擴展.NET本身已有的類型和功能。
.class Kerr.RealEstate.House { .method public void .ctor() { .maxstack 1 ldarg.0 // push "this" instance onto the stack call instance void [mscorlib]System.Object::.ctor() ret } }
定義一個靜態類型
.class abstract sealed Kerr.RealEstate.MortgageCalculator { /* members */ }
注意下面的代碼,它展示了MSIL命名空間的用法。可以直接把calss放在namespace里面,用大括號括起來,或是像本段的第一個代碼所表達的,直接寫完整的命名空間(C#中不支持這樣的寫法)。
.namespace Kerr.RealEstate { .class abstract sealed MortgageCalculator { /* members */ } }
下面的代碼演示新定義的類型繼承於現有的類型,和Java的語法相似。
.class Kerr.RealEstate.RoomList extends [System.Windows.Forms]System.Windows.Forms.ListView implements Kerr.IView { /* members */ }
定義一個接口,然后實現這個接口
.class interface Kerr.IView { /* members */ } .class Kerr.RealEstate.HouseData extends [mscorlib]System.ValueType { /* members */ }
4 定義類型成員
我在學習C++時,C++把類型成員區分為數據成員和方法成員,前者表示字段,后者表示方法。標准的C++書籍中從來不會把方法稱作函數,所以一直以來養成習慣,函數只用來指SQL Server腳本中的函數,.NET代碼中只有方法。
假設,我們正在定義下面的類型,將要為它添加方法
.class abstract Kerr.Sample.Object { }
靜態構造方法和構造方法
.method static void .cctor() { .maxstack 1 ldstr ".cctor" call void [mscorlib]System.Console::WriteLine(string) ret } .method public void .ctor() { .maxstack 1 ldarg.0 call instance void [mscorlib]System.Object::.ctor() ldstr ".ctor" call void [mscorlib]System.Console::WriteLine(string) ret }
靜態構造方法的調用時機時,當該類型的成員第一次被調用之前,先調用靜態構造方法。
創建類型的實例,並存儲在局部變量obj中
.locals (class TypeName obj) newobj void TypeName::.ctor() stloc obj
定義靜態方法
.method static void StaticMethod() { /* impl */ }
定義實例方法
.method void InstanceMethod() { /* impl */ }
下面的代碼演示如何調用靜態方法和實例方法
call void TypeName::StaticMethod() ldloc obj call instance void TypeName::InstanceMethod()
定義虛擬方法,這種情況主要用在繼承層次中,動態調用繼承層次中重寫的方法
.class House { .method public virtual void Buy() { .maxstack 1 ldstr "House::Buy" call void [mscorlib]System.Console::WriteLine(string) ret } /* etc */ } .class TownHouse extends House { .method public virtual void Buy() { .maxstack 1 ldstr "TownHouse::Buy" call void [mscorlib]System.Console::WriteLine(string) ret } /* etc */ }
下面的代碼演示了多態的應用,MSIL版本,請參考下面代碼
newobj instance void House::.ctor() stloc house newobj instance void TownHouse::.ctor() stloc townHouse ldloc house call instance void House::Buy() ldloc townHouse call instance void TownHouse::Buy() ldloc townHouse call instance void House::Buy() ldloc townHouse callvirt instance void House::Buy()
最后在控制台上的輸入結果是
House::Buy TownHouse::Buy House::Buy TownHouse::Buy
5 異常處理
MSIL是一種面向對象的語言,它的異常處理的基本指令格式
.try { /* protected code */ leave.s _CONTINUE } <exception handler> _CONTINUE:
來看一個例子,它讀取字符串值,調用Int32.Parse分析字符串,返回字符串代表的整型值
.try { ldstr "I'm not a number" // ldnull // ldstr "123" call int32 [mscorlib]System.Int32::Parse(string) leave.s _CONTINUE } catch [mscorlib]System.ArgumentNullException { callvirt instance string [mscorlib]System.Exception::get_Message() call void [mscorlib]System.Console::WriteLine(string) leave.s _CONTINUE } catch [mscorlib]System.FormatException { callvirt instance string [mscorlib]System.Exception::get_Message() call void [mscorlib]System.Console::WriteLine(string) leave.s _CONTINUE }
上面的代碼會拋出格式異常,異常會被FormaException截獲,它會在控制台上打印異常信息。
異常過濾器
.try { // ldstr "I'm not a number" ldnull // ldstr "123" call int32 [mscorlib]System.Int32::Parse(string) leave.s _CONTINUE } filter { ldstr "filter evaluation\n\t" call void [mscorlib]System.Console::Write(string) callvirt instance string [mscorlib]System.Exception::get_Message() call void [mscorlib]System.Console::WriteLine(string) ldc.i4.1 endfilter } { ldstr "filter handler\n\t" call void [mscorlib]System.Console::Write(string) callvirt instance string [mscorlib]System.Exception::get_Message() call void [mscorlib]System.Console::WriteLine(string) leave.s _CONTINUE }
try 語句中的代碼會拋出null異常,過濾器攔截此異常,並把true壓入堆棧,表示已經處理此異常,方法返回。
finally語句用最終都會被執行,比如要釋放非托管資源,數據庫連接等等
.try { /* protected code */ leave.s _CONTINUE } finally { /* cleanup code */ endfinally }
fault處理語句,try語句執行完畢后,進入fault語句,只能與try語句塊一起使用。與C#中的using(using(Object i=new Ojbect()); )用法相似,保證Dispose方法一定會被調用。
.try { /* protected code */ leave.s _CONTINUE } fault { /* cleanup code */ endfault }
6 控制流程
IF-ELSE語句
C#方法定義如下
void Send(string message) { if (null == message) { throw new ArgumentNullException("message"); } /* impl */ }
翻譯成MSIL語言,代碼如下
.method void Send(string message) { .maxstack 2 ldnull ldarg message ceq ldc.i4.0 ceq brtrue.s _CONTINUE ldstr "message" newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string) throw _CONTINUE: /* impl */ ret }
FOR語句
C#語句的寫法
for (int index = 0; 10 != index; ++index) { Debug.WriteLine(index); }
翻譯成MSIL語言的寫法
int index = 0; goto _CONDITION; _LOOP: ++index; _CONDITION: if (10 != index) { // for statements Debug.WriteLine(index); goto _LOOP; }
再來看一個FOR語句的例子
.locals init (int32 index) br.s _CONDITION _LOOP: ldc.i4.1 ldloc index add stloc index _CONDITION: ldc.i4.s 10 ldloc index beq _CONTINUE // for statements ldloc index box int32 call void [System]System.Diagnostics.Debug::WriteLine(object) br.s _LOOP _CONTINUE:
7 類型轉換
MSIL代碼例子,請看下面的代碼
.locals init (int32 small, int64 big) // Int32 small = 123; ldc.i4.s 123 stloc small // Int64 big = small; ldloc small conv.i8 stloc big // small = static_cast<Int32>(big); ldloc big conv.i4 stloc small
對應的C#語句是
Int32 small = 123; Int64 big = small; small = static_cast<Int32>(big);
逐語句的對比分析
.locals init (int32 small, int64 big) // Int32 small = 123; ldc.i4.s 123 stloc small // Int64 big = small; ldloc small conv.i8 stloc big // small = static_cast<Int32>(big); ldloc big conv.i4 stloc small
8 FOREACH語句
FOREACH語句應該是C#發明的,未見其它語言有此語言,以安全快速的方法遍歷一個集合。
來看下面的這個例子,C++語言的例子
array<int>^ numbers = gcnew array<int> { 1, 2, 3 }; for each (int element in numbers) { Console::WriteLine(element); }
翻譯成MSIL語言之后,代碼如下面所示
.locals init (int32[] numbers, int32 index) // Create the array ldc.i4.3 newarr int32 stloc numbers // Populate the array ldloc numbers ldc.i4.0 // index ldc.i4.1 // value stelem.i4 ldloc numbers ldc.i4.1 // index ldc.i4.2 // value stelem.i4 ldloc numbers ldc.i4.2 // index ldc.i4.3 // value stelem.i4 br.s _CONDITION _LOOP: ldc.i4.1 ldloc index add stloc index _CONDITION: ldloc numbers ldlen ldloc index beq _CONTINUE // for each statements ldloc numbers ldloc index ldelem.i4 call void [mscorlib]System.Console::WriteLine(int32) br.s _LOOP _CONTINUE:
再來看稍微復雜一點的例子
Collections::ArrayList numbers(3); numbers.Add(1); numbers.Add(2); numbers.Add(3); for each (int element in %numbers) { Console::WriteLine(element); }
翻譯成MSIL語言的代碼如下面所示
.locals init (class [mscorlib]System.Collections.ArrayList numbers, class [mscorlib]System.Collections.IEnumerator enumerator) // Create the array ldc.i4.3 newobj instance void [mscorlib]System.Collections.ArrayList::.ctor(int32) stloc numbers // Populate the array ldloc numbers ldc.i4.1 box int32 callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) pop ldloc numbers ldc.i4.2 box int32 callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) pop ldloc numbers ldc.i4.2 box int32 callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) pop // Get the enumerator ldloc numbers callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.IEnumerable::GetEnumerator() stloc enumerator br.s _CONDITION _CONDITION: ldloc enumerator callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext() brfalse.s _CONTINUE // for each statements ldloc enumerator callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current() call void [mscorlib]System.Console::WriteLine(object) br.s _CONDITION _CONTINUE:
Visual Studio不支持MSIL格式的源代碼文件語法高亮,推薦用Visual Microsoft Intermediate Language編輯器來閱讀IL代碼,工程化的管理方式,還可生成目標文件,比記事本方便好用。