初めましてこのサイトの管理人「利都」です。今回はプログラムの基礎である「HalloWorld」を行いたいと思います。
//.assembly extern mscorlib {} /*入れても入れなくても良い。*/
.assembly hello {}
.method static public void main() cil managed
{
.entrypoint
ldstr "Hello world!"
call void [mscorlib]System.Console
::WriteLine(class System.String)
ret
}
実行結果は次のようになります。
簡単ですね。でも、私は初めからこれにたどり着いたわけではありませんでした。最初はC#のコードを逆アセンブルしてみました。
using System; class Program { public static void Main(string []argv) { Console.WriteLine("hello,world"); } }
上が元にしたC#のソースコードです。
コンパイル、逆アセンブル。出てきたコードは次のものです。
// Microsoft (R) .NET Framework IL Disassembler.
// Version 1.1.4322.573
// Copyright (C) Microsoft Corporation
// 1998-2002. All rights reserved.
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 1:0:5000:0
}
.assembly hello
{
// --- 次のカスタム属性が自動的に追加されました。
// コメントを外さないでください。 -------
// .custom instance void [mscorlib]
// System.Diagnostics.DebuggableAttribute::
// .ctor(bool,bool) = ( 01 00 00 01 00 00 )
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module hello.exe
// MVID: {07292EED-4D10-4EBA-A03D-10531F539421}
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
.corflags 0x00000001
// Image base: 0x00d90000
//
// ============== CLASS STRUCTURE DECLARATION ==================
//
.class private auto ansi beforefieldinit Program
extends [mscorlib]System.Object
{
} // end of class Program
// =============================================================
// =============== GLOBAL FIELDS AND METHODS ===================
// =============================================================
// =============== CLASS MEMBERS DECLARATION ===================
// note that class flags, 'extends' and 'implements' clauses
// are provided here for information only
.class private auto ansi beforefieldinit Program
extends [mscorlib]System.Object
{
.method public hidebysig static void
Main(string[] argv) cil managed
{
.entrypoint
// コード サイズ 11 (0xb)
.maxstack 1
IL_0000: ldstr "hello,world"
IL_0005: call void [mscorlib]
System.Console::WriteLine(string)
IL_000a: ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// コード サイズ 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]
System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctor
} // end of class Program
// =============================================================
//*********** 逆アセンブリが完了しました ***********************
// WARNING: Created Win32 resource file hello.res
これだけしか見せなくても分かってしまう人もいるでしょう。ですが私はあんまりよく分かっていないので次回から色々と実験をしてみたいと思います。