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

来源:互联网 发布:淘宝价格助手 编辑:程序博客网 时间:2024/06/05 17:47

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"的例子

 

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元数据指令术语解释说明.英文版)

 

 

原创粉丝点击