zoukankan      html  css  js  c++  java
  • 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.il

    ildasm是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.Program
    extends [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文件->反编译

  • 相关阅读:
    Android.08.PopupWindow
    Android.07.AlertDialog
    Win 10 3D查看器 三维模型资源获取
    WebAssembly 系列(六)WebAssembly 的现在与未来
    《JavaScript高级程序设计》读书笔记(一)
    《JavaScript高级程序设计》读书笔记(二)
    WebAssembly 系列(五)为什么 WebAssembly 更快?
    《JavaScript DOM 编程艺术》读书笔记
    2.5维地图前端js api对比分析
    三维地图前端js api对比分析
  • 原文地址:https://www.cnblogs.com/wormday/p/2315093.html
Copyright © 2011-2022 走看看