代码大全 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代码,工程化的管理方式,还可生成目标文件,比记事本方便好用。
转载于:https://www.cnblogs.com/JamesLi2015/p/3174196.html
代码大全 MSIL语言程序设计相关推荐
- 医院信息管理系统c语言代码大全,C语言程序设计医院信息管理系统附源代码.doc...
实用文档 PAGE 文案大全 专业设计报告 课程名称: C 语 言 程 序 设 计 课题名称: 医院信息管理系统 专业班别: 11本计算机科学与技术二班 姓 名: 学 号: 指导教师: 设计日期: 2 ...
- c 语言整人代码大全,C 语言整人代码大全.doc
C 语言整人代码大全 C 语言整人代码大全 WScript.Echo("嘿,谢谢你打开我哦,我等你很久 拉!"&TSName) WScript.Echo("你是可 ...
- python课设代码,《Python语言程序设计》课设代码
<Python语言程序设计>课设代码是一个不错的学习资源,大小为3.45 MB,由钱高昂 提供,Python类资源中评分为9.5. 资源介绍 电子科技大学2020年<Python语言 ...
- c语言编程代码大全(c语言简单代码大全)
html代码和c语言等编程语言有什么联系吗? HTML叫做超文本标记语言(标准通用标记语言下的一个应用)或超文本链接标示语言,是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言. 怎么修改C语 ...
- c语言注册登录系统代码6,C语言程序设计(图书管理系统)源代码.doc(6)
h"#include#include/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- 健康调查系统c语言代码大全,C语言问卷调查(示例代码)
你对自己的未来有什么规划?做了哪些准备?努力做一名程序员,课余时间看看代码. 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?学习可以了解许多东西.现在学习动力不足.因为懒. 你感觉自己什么 ...
- c语言分手代码大全,C语言代码大全
从今天开始,我将C语言的代码实例从发表在博客上.供技术交流.比较基础,遵循循序渐进的原则,由简单到复杂,为以后的程序员生涯打好基础. 1.编写一个C程序输出以下信息. **************** ...
- 取消预约的c语言代码大全,C语言机房机位预约系统课设(附源码).doc
PAGE PAGE 1 C语言机房机位预约系统课设 项目说明 本系统基于C语言开发,适用于刚入门的C语言新手项目课设,开发软件采用VC++6.0开发,VS,DEV C++等均可运行.(书生) 项目运行 ...
- 用户登入系统的c语言代码大全,C语言 登录系统代码
<C语言 登录系统代码>由会员分享,可在线阅读,更多相关<C语言 登录系统代码(3页珍藏版)>请在人人文库网上搜索. 1.include #include typedef st ...
最新文章
- AB(apache benchmark)压力测试
- 题目1160:放苹果
- gson解析天气json_几种常用JSON解析库性能比较
- iOS 在类实现定义中声明成员变量的怪异方式
- 下拉日期控件 (转)
- lol转服务器维护,LOL转区系统活动_LOL转区系统活动地址_玩游戏网
- linux内核打印前有buildroot,buildroot-linux内核
- Ubuntu桌面主题美化推荐(Unity Tweak Tool + Flatabulous)
- sqlyog简单入门使用
- SpringCloud 学习(二)-2 :Securing The Eureka Server
- 机器学习、深度学习、自然语言处理、计算机视觉顶级期刊的论文资料分享(附CVPR2017-2019,ECCV 2018论文下载链接)
- 笔记本电脑进入BIOS设置快捷键大全
- 如何快速将显示未签收的单号物流归类为签收件
- 文明重启战局服务器维护中是什么意思,文明重启为什么改了密码别人还能登?...
- 视觉SLAM十四讲第二章学习与课后题与随笔日记
- 学习数学建模之优化类----蒙特卡洛迭代法(自学)+多元线性回归模型(STATA学习应用)+自己听论文排版的课程2022-01-28
- 用友数据库“可能发生了架构损坏。请运行 DBCC CHECKCATALOG。”错误修复
- C# 控制台程序 打开网页
- 正点原子alpha开发板关闭桌面进程
- Apollo入门教程(Windows)
热门文章
- Guitar Por如何演奏刮弦
- python函数-装饰器
- Android 触摸手势基础 官方文档概览
- 关于split与StringTokenizer的理解
- IE(=8)版本不支持getElementsByClassName()
- U盘装XP系统(含截图,2012最新原创超简单方法)
- 关于dhtmlxScheduler的使用说明(ADD EDIT DEL,自定义CelendarBox)
- WebService Software Factory 设计草图
- verycd重整——CBT系列
- 删除sessionstorage_localStorage 和 sessionStorage 介绍