专业开发: MSIL语言学习笔记 - C#

Introduce 介绍

     .net 的中间语言MSIL(Microsoft Intermediate Language),

       .net下生成的程序集(exe.dll)比较特殊,它的主要内容是MSIL,需要在.net环境下连接公共语言运行库(CLR)并由它代替编译成本地代码和执行本地代码.IL 不是字节代码或机器码,但与其非常接近.为了及时编译的速度

Explain&Use  解释&使用

ilasm 
exe.dll 编译工具,位置:  [系统盘]/WINDOWS/Microsoft.NET/Framework/v2.0.50727 ,如果要移动要包括 fusion.dll

查看元数据:Ctrl + M

命令行(command lines):

/exe                                      生成 .exe

/dll                                生成 .dll

/output:<输出文件路径>   输出文件

/resource:<资源文件路径>  载入资源文件

http://msdn.microsoft.com/zh-cn/library/496e4ekx(VS.80).aspx

例子:ilasm.exe /exe /resource:test.res /output:test.exe test.il

 


ildasm 
exe.dll反编译到IL及res资源文件的工具,位置 %ProgramFiles%/Microsoft SDKs/Windows/v6.0A/Bin

命令行(command lines):

/out=<输出文件路径>    输出文件

http://msdn.microsoft.com/zh-cn/library/f7dy01k1(v=vs.80).aspx

更多命令可输入 ildasm /help 查看

例子: ildasm /out=D:/test.il D:/test.exe

 

 

Glyph
Text Output
Description

[MOD] for module heading
Informational directives, class declarations, and manifest information

[NSP]
Namespace

[CLS]
Class

[INT]
Interface

[ENU]
Enumeration

[VCL]
Value class, also known as a structure

[MET]
Method (private, public, or protected)

[STM]
Static method

[FLD]
Field (private, public, or protected) also assembly

[STF]
Static field

[EVT]
Event

[PTY]
Property (get and/or set)

 

Metadata(元数据)

    描述数据特性的数据(data that describes data),当编译时产生。元数据描述代码中的类型,包括每种类型的定义、每种类型的成员的签名、代码引用的成员和运行库在执行时使用的其他数据。CLR根据这些信息,选择相应的方法处理代码.

更多解释:http://zh.wikipedia.org/wiki/%E5%85%83%E6%95%B0%E6%8D%AE

 IL文件的结构

"Holle C sharp"的例子

 

[c-sharp]  view plain copy
  1. //  Microsoft (R) .NET Framework IL Disassembler.  Version 3.5.21022.8  
  2. //  Copyright (c) Microsoft Corporation.  All rights reserved.  
  3.   
  4.   
  5. /*定义配置类,组件,资源等*/  
  6. // Metadata version: v2.0.50727  
  7. .assembly extern mscorlib  
  8. {  
  9.   .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z/V.4..  
  10.   .ver 2:0:0:0  
  11. }  
  12. .assembly 'hello c sharp'  
  13. {  
  14.   .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0D 68 65 6C 6C 6F 20 63 20 73 68 61 72 70   // ...hello c sharp  
  15.                                                                                               00 00 )   
  16.   .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 )   
  17.   .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 )   
  18.   .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 09 4D 69 63 72 6F 73 6F 66 74 00 00 )       // ...Microsoft..  
  19.   .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 0D 68 65 6C 6C 6F 20 63 20 73 68 61 72 70   // ...hello c sharp  
  20.                                                                                                 00 00 )   
  21.   .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 1B 43 6F 70 79 72 69 67 68 74 20 C2 A9 20   // ...Copyright ..   
  22.                                                                                                   4D 69 63 72 6F 73 6F 66 74 20 32 30 30 39 00 00 ) // Microsoft 2009..  
  23.   .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 )   
  24.   .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )   
  25.   .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 34 34 61 65 65 34 64 65 2D 36 37 35 37   // ..$44aee4de-6757  
  26.                                                                                                   2D 34 65 61 64 2D 61 34 65 38 2D 61 37 32 35 31   // -4ead-a4e8-a7251  
  27.                                                                                                   34 36 35 65 31 66 32 00 00 )                      // 465e1f2..  
  28.   .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..  
  29.   
  30.   // --- 下列自定义属性会自动添加,不要取消注释 -------  
  31.   //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )   
  32.   
  33.   .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )   
  34.   .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  
  35.                                                                                                              63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.  
  36.   .hash algorithm 0x00008004  
  37.   .ver 1:0:0:0  
  38. }  
  39. /* 
  40. .module 'hello c sharp.exe' 
  41. // MVID: {A7D63026-AD10-4250-801D-E90F8B18411F} 
  42. .imagebase 0x00400000 
  43. .file alignment 0x00000200 
  44. .stackreserve 0x00100000 
  45. .subsystem 0x0003       // WINDOWS_CUI 
  46. .corflags 0x00000001    //  ILONLY 
  47. // Image base: 0x03470000 
  48.  
  49.  
  50. // =============== CLASS MEMBERS DECLARATION =================== 
  51. /*类列表*/  
  52. .class private auto ansi beforefieldinit hello_c_sharp.Program  
  53.        extends [mscorlib]System.Object  
  54. {  
  55.   .method private hidebysig static void  Main(string[] args) cil managed  
  56.   {  
  57.     .entrypoint  
  58.     // 代码大小       19 (0x13)  
  59.     .maxstack  8  
  60.     IL_0000:  nop  
  61.     IL_0001:  ldstr      "hello c charp"  
  62.     IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)  
  63.     IL_000b:  nop  
  64.     IL_000c:  call       string [mscorlib]System.Console::ReadLine()  
  65.     IL_0011:  pop  
  66.     IL_0012:  ret  
  67.   } // end of method Program::Main  
  68.   
  69.   .method public hidebysig specialname rtspecialname   
  70.           instance void  .ctor() cil managed  
  71.   {  
  72.     // 代码大小       7 (0x7)  
  73.     .maxstack  8  
  74.     IL_0000:  ldarg.0  
  75.     IL_0001:  call       instance void [mscorlib]System.Object::.ctor()  
  76.     IL_0006:  ret  
  77.   } // end of method Program::.ctor  
  78.   
  79. // end of class hello_c_sharp.Program  
  80.   
  81.   
  82. // =============================================================  

IL常用命令解释

.entrypoint

      定义函数入口点

 .maxstack

      声明函数代码所用堆栈的最大深度

ldstr <string>

        把一个字符串常量装入堆栈

call   <function(parameters)>

      调用静态函数。函数的参数必须在函数调用前装入堆栈。

pop

      取出栈顶的值。当我们不需要把值存入变量时使用。

ret

      从一个函数中返回。

ldc.i4.<num>

      把一个 32位的常量(n从0到8)装入堆栈

stloc.<num>

     把一个从堆栈中返回的值存入第n(n从0到8)个局部变量

add

      2个值相加。命令的参数必须在调用前装入堆栈,该函数从堆栈中移除参数并把运算后的结果压入堆栈。

sub

     2个值相减。

mul

        2个值相乘。

.locals <init ([0] int32 x)>

        分配一个局部变量

     例子: .locals init ([0] int32 x,[1] int32 y,[2] int32 z,  [3] string s)

更多解释与例子:

  http://www.cnblogs.com/Yahong111/archive/2007/08/15/857140.html ( MSIL 教程)

  http://www.codeguru.com/Csharp/.NET/net_general/il/article.php/c4635 ( MSIL 教程英文版)

  http://www.kuqin.com/dotnet/20090406/44365.html (认识元数据和IL)

  http://www.dotblogs.com.tw/ajun/archive/2008/01/26/1011.aspx  (玩玩.net的ildasm與ilasm )

  http://bbs.pediy.com/showthread.php?threadid=16764  (ilasm 汇编小则)

  http://hi.baidu.com/zhangshourui/blog/item/ed73a30fa72da9e9aa6457d3.html  (通过MSIL了解CLR的运行原理)

  http://www.cnblogs.com/qyjun/articles/736102.html (详细解释两个il例子)

 

附件:

 

    http://download.csdn.net/source/3229951 (.net元数据指令术语解释说明.英文版)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值