■MSILとは?
MSIL(Microsoft Intermedia Language)とは中間言語のことです。
MSILは.NET環境で使用される、実行可能コードを記述するための中間言語で、プログラミング言語の一種になります。
C++/CLIなどを除く、.NETのプログラムは、コンパイル後に中間言語になり、実行時にネイティブコードになります。
そのため、MSILを学べば、少なからず.NETの知識が深まるのではないかと思います。
■MSILコードを見てみる
初心者がいきなり中間言語を書くのは無理なので、C#で簡単なアプリを作り、それをILに変換してみようと思います。
MSILへの逆アセンブルには、ildasm.exeを使用します。
C#のコードは以下のようにします。
namespace MSIL_1
{
class Program
{
static void Main(string[] args)
{
int n = 5;
Console.WriteLine(n);
}
}
}
上記コードをコンパイルして、実行ファイルを作成します。
作成した実行ファイルを早速、ildasm.exeで実行します。
C:\temp>ildasm MSIL_1.exe
実行すると、GUIが表示され、ツリーが表示されます。
しかし、欲しいのはコードなので、今度はテキストで出力してみます。
C:\temp>ildasm MSIL_1.exe /text
/text オプションを使用するとテキスト形式で、"コンソール上に"表示されます。これまた見にくいので、ファイルに出力したいと思います。
C:\temp>ildasm MSIL_1.exe /out=MSIL_1.il
出力ファイルの拡張子は、ilにします。
ilファイルと同時に、resファイルも作成されます。
以下が、出力されたilファイルになります。
一部自動生成コメントは、削除しています。
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 4:0:0:0
}
.assembly MSIL_1
{
.custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 06 4D 53 49 4C 5F 31 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 06 4D 53 49 4C 5F 31 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20 // ...Copyright ..
20 32 30 31 32 00 00 ) // 2012..
.custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 30 37 62 31 34 61 65 31 2D 61 35 65 66 // ..$07b14ae1-a5ef
2D 34 64 61 36 2D 39 61 37 61 2D 30 32 61 39 62 // -4da6-9a7a-02a9b
61 61 30 32 63 34 66 00 00 ) // aa02c4f..
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 ) // ...1.0.0.0..
.custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 29 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B // ..).NETFramework
2C 56 65 72 73 69 6F 6E 3D 76 34 2E 30 2C 50 72 // ,Version=v4.0,Pr
6F 66 69 6C 65 3D 43 6C 69 65 6E 74 01 00 54 0E // ofile=Client..T.
14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61 // .FrameworkDispla
79 4E 61 6D 65 1F 2E 4E 45 54 20 46 72 61 6D 65 // yName..NET Frame
77 6F 72 6B 20 34 20 43 6C 69 65 6E 74 20 50 72 // work 4 Client Pr
6F 66 69 6C 65 ) // ofile
// --- 次のカスタム属性が自動的に追加されました。コメントを解除しないでください -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004
.ver 1:0:0:0
}
.module MSIL_1.exe
// MVID: {2014487B-D438-4882-A6E6-EFE6E79EE04B}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000003 // ILONLY 32BITREQUIRED
// Image base: 0x00300000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit MSIL_1.Program
extends [mscorlib]System.Object
{
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// コード サイズ 9 (0x9)
.maxstack 1
.locals init (int32 V_0)
IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: call void [mscorlib]System.Console::WriteLine(int32)
IL_0008: ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// コード サイズ 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctor
} // end of class MSIL_1.Program
次回、ILの基礎を調べながら、ilコードの解析をしてみます。