.net的一个很重要的特性就是跨语言的编程,任何语言都可以相互的调用,(比如C++,可以调用c#写的类),但前提相互调用的语言的约定只能规矩的遵守"CLS(公共语言规范)"的所有特性才可以,否则这中跨语言的编程将死悄悄拉...

net的汇编ilasm则继承了"CLS(公共语言规范)"所有特性.对于其他语言则也都集成了最小"CLS"规范,相关"GLS"规范可以看 .NET Frame 文档...
如果你想成为一个多语中编程的高手,想在.net平台下跨语言的编程,就必须熟悉 相关"CLS"的特性规范...

ilasm汇编不同与x86汇编,它的虚拟字节指令更抽象,更加想面向对象指令...

现在很多论坛对ilasm汇编很热,偶也初学.net,也对这中il汇编有兴趣.

下面 看看一个 输出"hello ilasm" 的Window Console 的ilasm结构:

.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                       
  .ver 1:0:5000:0
}
.assembly helloil1
{
  
}

.module helloil1.exe
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 4096
.corflags 0x00000001



.namespace helloil1   //定义名字空间helloil1 
{
  .class private auto ansi beforefieldinit Class1 extends [mscorlib]System.Object 
  {
    .method private hidebysig static void Main(string[] args) cil managed
    {
      .entrypoint //程序进入点标志
      .custom instance void [mscorlib]System.STAThreadAttribute::.ctor()  //单线城对象模型构造初始化
      .maxstack  1 //堆栈定义
        ldstr      "Hello ilasm" //压入字符串,堆栈压操作
        call       void [mscorlib]System.Console::WriteLine(string)
//这里输出"Hello ilasm" 调用
        call       string [mscorlib]System.Console::ReadLine()
//检测回车
        pop
        ret
    } 

    .method public hidebysig specialname rtspecialname instance void  .ctor() cil managed //这是console类构造
    {
      
      .maxstack  1
        ldarg.0
        call       instance void [mscorlib]System.Object::.ctor()
        ret
    } 

  } 


 

ilasm汇编编译器在路径:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\ilasm.exe

编译成功,,输出" hello  ilasm" ,它是个Window Console程序,至于生成一个
Window Form 程序的ilasm汇编结构,我会在下一遍给予介绍..

偶大略的说说.详细可以看看.NET Frame 文档...


static void Main(string[] args) cil managed

这是程序托管入口,static void Main(),




.assembly 特性定义一个程序集,可以模糊的认为程序集是.net环境中生成的EXE,或DLL,但只中EXE 或DLL,与.net之前的有又不同,它们更象组件相互封装,配属...具体看.NET Frame 文档...

.module 特性区别与.assembly,可以把.module理解为.net之前的exe或dll..

.imagebase 影象基地址..

.subsystem 连接系统类型

.file alignment 文件对齐数值

.corflags  指令设置运行库头文件标志.默认情况下,.corflags 指令指定值 1

.namespace 定义名字空间


由于初学.net有描述错误,大家请指点...

实例下载。

  • 标 题: ilasm 汇编小则 (二) (高手勿进)
  • 作 者:qiweixue
  • 时 间:2005-09-09 16:04

第二篇.IL汇编,,
在第一篇(是凑数的.. )有了对il汇编的了解,,

偶只是简单的说一下il汇编结构,,不是讲解.net 
大家记得.NET 反编译工具ildasm,,大家可以反编译,熟悉熟悉il汇编虚拟指令
详细的il汇编虚拟指令说明在:
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemreflectionemitopcodesclassbrfalse_stopic.asp

下面 Win Form il 汇编如下(有些特性,偶也是用ildasm反编译,加ilasm正向编译得到的):

.assembly extern System.Windows.Forms
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         
  .ver 1:0:5000:0
}
.assembly extern System
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         
  .ver 1:0:5000:0
}
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         
  .ver 1:0:5000:0
}
.assembly extern System.Drawing
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         
  .ver 1:0:5000:0
}
.assembly HelloIL
{
  .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyKeyNameAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyKeyFileAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyDelaySignAttribute::.ctor(bool) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .hash algorithm 0x00008004
  .ver 1:0:2078:25277
}
.mresource public HelloIL.Form1.resources{}
.module HelloIL.exe
.imagebase 0x00400000
.subsystem 0x00000002
.file alignment 4096
.corflags 0x00000001



.namespace HelloIL
{
  .class public auto ansi beforefieldinit Form1 extends [System.Windows.Forms]System.Windows.Forms.Form
  {
    .field private class [System]System.ComponentModel.Container components
    .method public hidebysig specialname rtspecialname instance void  .ctor() cil managed
    {
      
      .maxstack  2
        ldarg.0
        ldnull
        stfld      class [System]System.ComponentModel.Container HelloIL.Form1::components
        ldarg.0
        call       instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
        ldarg.0
        call       instance void HelloIL.Form1::InitializeComponent()
        ret
    } 

    .method family hidebysig virtual instance void Dispose(bool disposing) cil managed
    {
      
      .maxstack  2
          ldarg.1
          brfalse.s  IL_jmp1
          ldarg.0
          ldfld      class [System]System.ComponentModel.Container HelloIL.Form1::components
          brfalse.s  IL_jmp1
          ldarg.0
          ldfld      class [System]System.ComponentModel.Container HelloIL.Form1::components
          callvirt   instance void [System]System.ComponentModel.Container::Dispose()
IL_jmp1:  ldarg.0
          ldarg.1
          call       instance void [System.Windows.Forms]System.Windows.Forms.Form::Dispose(bool)
          ret
    } 
    .method private hidebysig instance void InitializeComponent() cil managed
    {
      
      .maxstack  4
        ldarg.0
        ldc.i4.6
        ldc.i4.s   14
        newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,int32)
        callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Form::set_AutoScaleBaseSize(valuetype [System.Drawing]System.Drawing.Size)
        ldarg.0
        ldc.i4     0x108
        ldc.i4     0xa6
        newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,int32)
        call       instance void [System.Windows.Forms]System.Windows.Forms.Form::set_ClientSize(valuetype [System.Drawing]System.Drawing.Size)
        ldarg.0
        ldstr      "Form1"
        call       instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Name(string)
        ldarg.0
        ldstr      "HelloIL"
        callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
        ret
    } 
    .method private hidebysig static void Main() cil managed
    {
      .entrypoint
      .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
      .maxstack  1
      newobj     instance void HelloIL.Form1::.ctor()
      call       void [System.Windows.Forms]System.Windows.Forms.Application::Run(class [System.Windows.Forms]System.Windows.Forms.Form)
      ret
    } 

  } 




il虚拟汇编指令偶不想注释了...大家可以到
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemreflectionemitopcodesclassbrfalse_stopic.asp

在msdn中 il opcode 说明很详细,,大家可以慢慢翻译....

用 ilasm 编译 它成功之后,,就出出现一个win form 窗口程序,,很幼稚,
错误很多,大家请指正哈..

实例下载。(其中资源是在vs.net编译的..)