代码大全 MSIL语言程序设计

.NET平台的编译器会将高级语言(C#,VB.NET,F#)编译成MSIL(微软中间语言)格式。熟悉MSIL语言,可以读懂一些加密程序混淆过的算法,这些算法几乎不能还原成高级语言,但是可以还原成MSIL语言。也可以知道一些高级语言之外的关于CLR的特性,比如多模块程序集,全局静态方法等等。一些.NET保护加密程序也是运用MSIL平台的特性。

阅读本篇文章,假设您已经对这个语言有基本的了解,我会列举这个语言的基本语言应用例子,供参考。

1 Hello world

.method static void main()
{.entrypoint.maxstack 1ldstr "Hello world!"call void [mscorlib]System.Console::WriteLine(string)ret
}

在控制台上打印Hello world字符串。MSIL以entrypoint表示入口方法,而不一定是C#中规定的Main方法。

2 使用局部变量

.locals init (int32 first,int32 second,int32 result)

上面的语法,定义了三个局部变量,它的名称分别是first,sencond,result。

下面的代码读取用户在控制台上的输入值,并调用Parse方法,把结果保存在first局部变量中。

ldstr "First number: "
call void [mscorlib]System.Console::Write(string)
call string [mscorlib]System.Console::ReadLine()
call int32 [mscorlib]System.Int32::Parse(string)
stloc first
 

调用add方法,将frist和second的值加起来,保存到resutl局部变量中

ldloc first
ldloc second
add
stloc result
 

最后,在控制台上打印结果值

ldstr "{0} + {1} = {2}"
ldloc first
box int32
ldloc second
box int32
ldloc result
box int32
call void [mscorlib]System.Console::WriteLine(string, object, object, object)

因为三个局部变量是int32类型,调用WriteLine方法时要传入object类型,所以要装箱(box)。

 

3 定义类型

新建一个calss/enum/struct即为定义一种新的程序类型,扩展.NET本身已有的类型和功能。

.class Kerr.RealEstate.House
{.method public void .ctor(){.maxstack 1ldarg.0 // push "this" instance onto the stackcall instance void [mscorlib]System.Object::.ctor()ret}
}

定义一个静态类型

.class abstract sealed Kerr.RealEstate.MortgageCalculator
{/* members */ 
}

注意下面的代码,它展示了MSIL命名空间的用法。可以直接把calss放在namespace里面,用大括号括起来,或是像本段的第一个代码所表达的,直接写完整的命名空间(C#中不支持这样的写法)。

.namespace Kerr.RealEstate
{.class abstract sealed MortgageCalculator{/* members */ }
}

下面的代码演示新定义的类型继承于现有的类型,和Java的语法相似。

.class Kerr.RealEstate.RoomListextends [System.Windows.Forms]System.Windows.Forms.ListViewimplements Kerr.IView
{/* members */
}

定义一个接口,然后实现这个接口

.class interface Kerr.IView
{/* members */ 
}
.class Kerr.RealEstate.HouseDataextends [mscorlib]System.ValueType
{/* members */
}

 

4  定义类型成员

我在学习C++时,C++把类型成员区分为数据成员和方法成员,前者表示字段,后者表示方法。标准的C++书籍中从来不会把方法称作函数,所以一直以来养成习惯,函数只用来指SQL Server脚本中的函数,.NET代码中只有方法。

假设,我们正在定义下面的类型,将要为它添加方法

.class abstract Kerr.Sample.Object
{
}

静态构造方法和构造方法

.method static void .cctor()
{.maxstack 1ldstr ".cctor"call void [mscorlib]System.Console::WriteLine(string)ret
}
.method public void .ctor()
{.maxstack 1ldarg.0call instance void [mscorlib]System.Object::.ctor()ldstr ".ctor"call void [mscorlib]System.Console::WriteLine(string)ret
}

静态构造方法的调用时机时,当该类型的成员第一次被调用之前,先调用静态构造方法。

创建类型的实例,并存储在局部变量obj中

.locals (class TypeName obj)
newobj void TypeName::.ctor()
stloc obj

定义静态方法

.method static void StaticMethod() { /* impl */ }

定义实例方法

.method void InstanceMethod() { /* impl */ }
 
下面的代码演示如何调用静态方法和实例方法
call void TypeName::StaticMethod()
ldloc obj
call instance void TypeName::InstanceMethod()

定义虚拟方法,这种情况主要用在继承层次中,动态调用继承层次中重写的方法

.class House
{.method public virtual void Buy(){.maxstack 1ldstr "House::Buy"call void [mscorlib]System.Console::WriteLine(string)ret}/* etc */
}
.class TownHouseextends House
{.method public virtual void Buy(){.maxstack 1ldstr "TownHouse::Buy"call void [mscorlib]System.Console::WriteLine(string)ret}/* etc */
}

下面的代码演示了多态的应用,MSIL版本,请参考下面代码

newobj instance void House::.ctor()
stloc house
newobj instance void TownHouse::.ctor()
stloc townHouse
ldloc house
call instance void House::Buy()
ldloc townHouse
call instance void TownHouse::Buy()
ldloc townHouse
call instance void House::Buy()
ldloc townHouse
callvirt instance void House::Buy()

最后在控制台上的输入结果是

House::Buy
TownHouse::Buy
House::Buy
TownHouse::Buy


 

5  异常处理

MSIL是一种面向对象的语言,它的异常处理的基本指令格式

.try
{/* protected code */leave.s _CONTINUE
}
<exception handler>
_CONTINUE:

来看一个例子,它读取字符串值,调用Int32.Parse分析字符串,返回字符串代表的整型值

.try
{ldstr "I'm not a number"// ldnull// ldstr "123"call int32 [mscorlib]System.Int32::Parse(string)leave.s _CONTINUE
}
catch [mscorlib]System.ArgumentNullException
{callvirt instance string [mscorlib]System.Exception::get_Message()call void [mscorlib]System.Console::WriteLine(string)leave.s _CONTINUE
}
catch [mscorlib]System.FormatException
{callvirt instance string [mscorlib]System.Exception::get_Message()call void [mscorlib]System.Console::WriteLine(string)leave.s _CONTINUE
}

上面的代码会抛出格式异常,异常会被FormaException截获,它会在控制台上打印异常信息。

异常过滤器

.try
{// ldstr "I'm not a number"ldnull// ldstr "123"call int32 [mscorlib]System.Int32::Parse(string)leave.s _CONTINUE
}
filter
{ldstr "filter evaluation\n\t"call void [mscorlib]System.Console::Write(string)callvirt instance string [mscorlib]System.Exception::get_Message()call void [mscorlib]System.Console::WriteLine(string)ldc.i4.1endfilter
}
{ldstr "filter handler\n\t"call void [mscorlib]System.Console::Write(string)callvirt instance string [mscorlib]System.Exception::get_Message()call void [mscorlib]System.Console::WriteLine(string)leave.s _CONTINUE
}

try 语句中的代码会抛出null异常,过滤器拦截此异常,并把true压入堆栈,表示已经处理此异常,方法返回。

finally语句用最终都会被执行,比如要释放非托管资源,数据库连接等等

.try
{/* protected code */leave.s _CONTINUE
}
finally
{/* cleanup code */endfinally
}

fault处理语句,try语句执行完毕后,进入fault语句,只能与try语句块一起使用。与C#中的using(using(Object i=new Ojbect()); )用法相似,保证Dispose方法一定会被调用。

.try
{/* protected code */leave.s _CONTINUE
}
fault
{/* cleanup code */endfault
}

6 控制流程

IF-ELSE语句

C#方法定义如下

void Send(string message)
{if (null == message){throw new ArgumentNullException("message");}/* impl */
}

 

翻译成MSIL语言,代码如下

.method void Send(string message)
{.maxstack 2ldnullldarg messageceqldc.i4.0ceqbrtrue.s _CONTINUEldstr "message"newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string)throw_CONTINUE: /* impl */ret
}

FOR语句

C#语句的写法

for (int index = 0; 10 != index; ++index)
{Debug.WriteLine(index);
}

翻译成MSIL语言的写法

    int index = 0;goto _CONDITION;_LOOP:++index;_CONDITION:if (10 != index){// for statementsDebug.WriteLine(index);goto _LOOP;}

再来看一个FOR语句的例子

    .locals init (int32 index)br.s _CONDITION_LOOP:ldc.i4.1ldloc indexaddstloc index _CONDITION:ldc.i4.s 10ldloc indexbeq _CONTINUE// for statementsldloc indexbox int32call void [System]System.Diagnostics.Debug::WriteLine(object)br.s _LOOP_CONTINUE:
 

7 类型转换

MSIL代码例子,请看下面的代码

.locals init (int32 small,int64 big)// Int32 small = 123;
ldc.i4.s 123
stloc small// Int64 big = small;
ldloc small
conv.i8
stloc big// small = static_cast<Int32>(big);
ldloc big
conv.i4
stloc small

对应的C#语句是

Int32 small = 123;
Int64 big = small;small = static_cast<Int32>(big);

逐语句的对比分析

.locals init (int32 small,int64 big)// Int32 small = 123;
ldc.i4.s 123
stloc small// Int64 big = small;
ldloc small
conv.i8
stloc big// small = static_cast<Int32>(big);
ldloc big
conv.i4
stloc small

8  FOREACH语句

FOREACH语句应该是C#发明的,未见其它语言有此语言,以安全快速的方法遍历一个集合。

来看下面的这个例子,C++语言的例子

array<int>^ numbers = gcnew array<int> { 1, 2, 3 };for each (int element in numbers)
{Console::WriteLine(element);
}

翻译成MSIL语言之后,代码如下面所示

   .locals init (int32[] numbers,int32 index)// Create the arrayldc.i4.3newarr int32stloc numbers// Populate the arrayldloc numbers ldc.i4.0 // indexldc.i4.1 // valuestelem.i4ldloc numbers ldc.i4.1 // index ldc.i4.2 // value stelem.i4ldloc numbers ldc.i4.2 // index ldc.i4.3 // valuestelem.i4br.s _CONDITION_LOOP:ldc.i4.1ldloc indexaddstloc index _CONDITION:ldloc numbersldlenldloc indexbeq _CONTINUE// for each statementsldloc numbersldloc indexldelem.i4call void [mscorlib]System.Console::WriteLine(int32)br.s _LOOP_CONTINUE:

再来看稍微复杂一点的例子

Collections::ArrayList numbers(3);
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);for each (int element in %numbers)
{Console::WriteLine(element);
}

翻译成MSIL语言的代码如下面所示

   .locals init (class [mscorlib]System.Collections.ArrayList numbers,class [mscorlib]System.Collections.IEnumerator enumerator)// Create the arrayldc.i4.3newobj instance void [mscorlib]System.Collections.ArrayList::.ctor(int32)stloc numbers// Populate the arrayldloc numbers ldc.i4.1 box int32callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)popldloc numbers ldc.i4.2 box int32callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)popldloc numbers ldc.i4.2 box int32callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)pop// Get the enumeratorldloc numberscallvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.IEnumerable::GetEnumerator()stloc enumeratorbr.s _CONDITION_CONDITION:ldloc enumeratorcallvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()brfalse.s _CONTINUE// for each statementsldloc enumeratorcallvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()call void [mscorlib]System.Console::WriteLine(object)br.s _CONDITION_CONTINUE:

 

Visual Studio不支持MSIL格式的源代码文件语法高亮,推荐用Visual Microsoft Intermediate Language编辑器来阅读IL代码,工程化的管理方式,还可生成目标文件,比记事本方便好用。

image

转载于:https://www.cnblogs.com/JamesLi2015/p/3174196.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/433383.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python 条件概率_机器学习中的概率问题

机器学习的过程可以理解为计算机通过分析大量的数据获得模型&#xff0c;并通过获得的模型进行预测的过程。机器学习的模型可以有多种表示&#xff0c;例如线性回归模型&#xff0c;SVM模型&#xff0c;决策树模型&#xff0c;贝叶斯模型。概率类型在理解概率模型之前&#xff…

java string 类 上机_java上机试题

请将下列关键字填写到适当的位置中。newextendsimplementsstaticsuperpublicabstractthisimportpackage(1)Fruits;//声明包语句(2)java.util.*;abstractclassFruit//定义抽象类Fruit{D...请将下列关键字填写到适当的位置中。new extends implements static superpublic abstrac…

LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别

LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量&#xff0c;二者的含义和作用分别如下&#xff1a; LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径&#xff0c;例如&#xff0c;指定gcc编译需要用到的动态链接库的目录。 LD_LIBRARY_PA…

下列python语言、返回结果不是uc_MKAN1-UC 5103作业代写、代做Analytics作业、Java,Python,c/c++程序语言作业代做...

MKAN1-UC 5103作业代写、代做Analytics作业、Java&#xff0c;Python&#xff0c;c/c程序语言作业代做日期&#xff1a;2020-05-21 11:08Marketing Analytics Spring 2020 Final ExamMKAN1-UC 5103 Marketing Analytics Spring 2020 Final ExamInstructions (total 3 points):?…

以太网交换机

以太网交换机是基于以太网传输数据的交换机&#xff0c;以太网采用共享总线型传输媒体方式的局域网。以太网交换机的结构是每个端口都直接与主机相连&#xff0c;并且一般都工作在全双工方式。交换机能同时连通许多对端口&#xff0c;使每一对相互通信的主机都能像独占通信媒体…

java中Class对象详解

java中把生成Class对象和实例对象弄混了&#xff0c;更何况生成Class对象和生成instance都有多种方式。所以只有弄清其中的原理&#xff0c;才可以深入理解。首先要生成Class对象&#xff0c;然后再生成Instance。那Class对象的生成方式有哪些呢&#xff0c;以及其中是如何秘密…

python分组求和法_python – 如何将groupby值的总和除以另一个值的count

我想通过’label’和’month’分组来计算每个月和每个标签的销售数量.我正在尝试’groupby和apply’方法来实现这一目标,但不确定如何计算每个标签的月份.比方说,对于标签值AFFLELOU(DOS),我有7个月的两个值.所以,我应该将销售数量相加并除以2.在第9个月和第10个月,我只有一个值…

Microsoft Visual Studio 打开代码出现乱码解决方案

在用VS编写代码时&#xff0c;文本的字符集可能和编译器的字符集不同&#xff0c;在这种情况下代码会显示出乱码。 解决办法&#xff1a; 在VS的工具->选项里面找到"文本编辑器"&#xff0c;勾选“自动检测不带签名的UTF-8编码(D)”选项。 重新打开VS编译器&#…

java正则表达式及api_Java API 之 正则表达式

一、基本概念在项目中我们经常性做的一件事是“匹配”字符串比如&#xff1a;1、我们要验证用户输入的手机号是否合法&#xff1f;2、验证设置的密码是否符合规则&#xff1f;3、或者替换指定字符串中的一些内容。这么一看&#xff0c;似乎正则表达式就是在查找、处理字符串。二…

PB错误处理

//itemerror事件里出错处理 string ls_colname,ls_datatype Boolean ab_msg true ls_colname dwo.name ls_datatype dwo.coltype if trim(data) "" or isnull(data) then ab_msg false //之前这里使用return 3&#xff0c;所以出现这个问题 end if string d…

idea 创建java文件_idea创建java文件 格式不对

import java.io.File;import java.io.FileWriter;import java.io.IOException;public class Test {public static void main(String[] args) {//文件路径File filenew File("d:\\alphabet.txt");//如果文件存在就进行下面的操作&#xff0c;否则打印“找不到文件&…

待办事项桌面插件_让浏览器重获整洁——标签页管理插件:OneTabOneTab PlusToby...

1 什么是标签页管理插件chrome浏览器虽然为用户提供了一个非常方便的打开新标签页的方法&#xff0c;但是随着用户浏览网页的时间增长&#xff0c;用户在一个chrome窗口中打开的标签页会越来越多&#xff0c;当这些标签页过多的时候&#xff0c;用户在标签页之间进行切换就会变…

ext2 源代码解析之 “从路径名到目标结点” (一)

两个主要函数&#xff0c;path_init和path_walk&#xff0c;他们结合在一起根据给定的文件路径名称在内存中找到或者建立代表着目标文件或目录的dentry和inode结构。注意&#xff0c;最终是信息是读取到内存中的。其中有个函数__user_walk&#xff08;&#xff09;将path_init(…

为什么要自学python_为什么那么多自学Python的后来都放弃了,总结起来就这些原因...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼目前信息化产业发展势头很好&#xff0c;互联网就成为了很多普通人想要涉及的行业&#xff0c;因为相比于传统行业&#xff0c;互联网行业涨薪幅度大&#xff0c;机会也多&#xff0c;所以就会大批的人想要转行来学习Python开发。目…

java内部类外部类_Java内部类:如何在内部类中返回外部类对象

Nasted Class 的介绍&#xff0c;请详见参考今天讨论的不是不是内部类的概念&#xff0c;而是具体使用的一个场景&#xff0d;如何在内部类中返回外部对象(孤猪:我在程序里也碰到过一次这样的状况&#xff0c;非静态内部类可以直接调用外部类实例的成员变量&#xff0c;但如何引…

linux 中 timeval结构体

网上很多人写到&#xff0c;timeval结构解释错误 ******************************************************************* 问题如下&#xff1a; 在debian linux的man页中对gettimeofday函数的说明中&#xff0c;有这样一个说明&#xff1a; DESCRIPTION The functions get…

[置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long

这些数据类型的sizeof具体长度依赖于编译器和操作系统&#xff08;32-bit or 64-bit&#xff09; 1: 首先&#xff0c;参见c99标准 标准中没有定义这些数据类型的长度&#xff0c;而是定义了这些数据类型能表达的大小范围的最小极限。 C99链接&#xff1a; http://www.open-std…

关机时无人照管更新正在运行_了解iOS13.1后,在决定更新

苹果提前发布了首个重要更新的iOS 13.1&#xff0c;补充了多个 iOS 13 首发时缺失的重要新功能&#xff0c;可以说 iOS 13.1 才是【真正】的 iOS 13 系统。苹果发布iOS13.1,都更新了哪些内容&#xff1f;iOS13.1修复问题和改进问题&#xff1a;?信息中拟我表情可能无法正确跟踪…

java enum in class_Java 8需要一个转换,而Java 7没有 – enum.getClass/getDeclaringClass

我意识到Java 8仍然在测试版&#xff0c;但是这一点让我很奇怪&#xff1a;public class Fields> {public Fields(Set columns) {// A sample column used to find the universe of the enum of Columns.C sampleCol columns.iterator().next();// Java 8 needs a cast her…

linux C 中的volatile使用

一个定义为volatile的变量是说这变量可能会被意想不到地改变&#xff0c;这样&#xff0c;编译器就不会去假设这个变量的值了。精确地说就是&#xff0c;优化器在用到这个变量时必须每次都小心地重新读取这个变量的值&#xff0c;而不是使用保存在寄存器里的备份。下面是volati…