MSIL学习笔记(01):ilasm和ildasm
我跟每個.NET程序員一樣,MSIL多少都接觸過一點,個別語句也能看明白,但是沒有系統學習過
更沒有寫過IL代碼 和 編譯過
IL我覺得學習方法應該跟其他語言不同,因為這個語言根本上不是用來手寫的,因為IL就語言層次上相當高級,手工寫又相當麻煩,C#好像也就是薄薄的包裝了一層
還是寫C#代碼看IL來學習吧
1 using System;2
3 namespace ILSample
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("Hello");
10 }
11 }
12 }
這么一段代碼,編譯生成?ILSample.exe
然后Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio Command Prompt (2010)
在黑屏里 輸入 執行下面的命令?
ildasm ILSample.exe /out=ILSample.ilildasm是IL語言的反編譯工具,就是可以把DLL和EXE文件反編譯成IL代碼。執行這個命令會將ILSample.exe的IL原文件保存在ILSample.il中
?
如果直接輸入
ildasm ILSample.exe或者
ildasm
會打開ildasm圖形界面 ,比如
但是加上“/out=ILSample.il” 就直接反編譯成IL代碼文件,圖形界面就不出來了
打開這個文件,內容如下
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.1// Copyright (c) Microsoft Corporation. All rights reserved.
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly ILSample
{
.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
// --- The following custom attribute is added automatically, do not uncomment -------
// .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 0:0:0:0
}
.module ILSample.exe
// MVID: {76CE82AE-87E2-4509-B42A-AF8E007539D5}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000003 // ILONLY 32BITREQUIRED
// Image base: 0x00390000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ILSample.Program
extends [mscorlib]System.Object
{
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello"
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
{
// Code size 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 ILSample.Program
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ILSample.res
生成這么一大堆東西,雖然很多地方搞不清楚什么意思,能不能刪除
但是起碼有一部分可以看明白 比如?
.class private auto ansi beforefieldinit ILSample.Programextends [mscorlib]System.Object
因該是
class Program翻譯過來的?
可以從IL中看出來類Program繼承自 System.Object 是私有的 等等
以及IL定義的一個方法Main(string[] args),.ctor()是構造函數。
具體細節以后日子還長,現在的任務是把生成的IL代碼 重新編譯成 .EXE文件
還是在
開始菜單->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio Command Prompt (2010)
在黑屏里 輸入 執行下面的命令?
ilasm ilsample.il /output=ILSample2.exe會生成一個文件ILSample2.exe 執行這個文件 如果正確執行就搞定了。
今天的任務完成了
就是從C#的源代碼->.EXE文件->反編譯
轉載于:https://www.cnblogs.com/wormday/archive/2012/01/06/2315093.html
總結
以上是生活随笔為你收集整理的MSIL学习笔记(01):ilasm和ildasm的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [恢]hdu 1860
- 下一篇: 工作周报:2011-11-14至2011