MSIL Hello World
最近由于需要,开始阅读 MSIL 方面的东西。我读的是《.NET 探秘——MSIL 权威指南》(《Expert .NET 2.0 IL Assembler》中译版)。感觉没什么好说的,毕竟只要对 .NET 及其后面的东西了解一些,然后当做汇编来看,就好了。剩下的就是实践。
如书上所言,前面已经有人做出了这项研究了,如 Anders Liu、装配脑袋、Flier Lu。前辈们都是老手了,我也不好说什么,毕竟我刚刚入门。
这里就贴出昨晚写的一个 HelloWorld 程序吧。读了4天的收获。为了方便没学过 MSIL 的同志们,在上面都附上了实现同样功能的 C# 代码。
嗯,高级的东西还不敢说,要再仔细看,多练习才行啊。
.assembly extern mscorlib { auto }
.assembly MyIlApp { }
.module MyIlApp.exe
.namespace MyIlApp
{
// public struct VC
.class public value sealed auto VC
{
// public int val_int32;
.field public int32 val_int32
// public string val_string;
.field public string val_string
}
// public sealed class MainClass
.class public sealed auto ansi MainClass
{
// public static int _val;
.field public static int32 _val
// public static void check(int argument1)
.method public static void check(int32) cil managed
{
// string temp;
.locals init ([]string temp)
// if (MyIlApp.MainClass._val == argument1)
// {
// System.Console.WriteLine("The value equals to the argument.");
// }
// else
// {
// System.Console.WriteLine("The value does not equal to the argument.");
// }
ldsfld int32 MyIlApp.MainClass::_val
ldarg.0
beq TrueEqual
ldstr "The value does not equal to the argument."
call void [mscorlib]System.Console::WriteLine(string)
br ThisIsEnd
TrueEqual:
ldstr "The value equals to the argument."
call void [mscorlib]System.Console::WriteLine(string)
// temp = argument1.ToString(); // 注意这里用 call 而不是 callvirt,因为 argument1 是 int32 类型,未装箱的时候没有V表
ThisIsEnd:
ldarga.s
call instance string [mscorlib]System.Int32::ToString()
stloc.0
// System.Console.Write("The real value is:");
ldstr "The real value is: "
call void [mscorlib]System.Console::Write(string)
// System.Console.WriteLine(temp);
ldloc.0
call void [mscorlib]System.Console::WriteLine(string)
// return;
ret
}
// public static void Main()
.method public static void Main() cil managed
{
// .entrypoint 伪指令表示是程序入口点
.entrypoint
/* Test Case 1 */
// string input;
// int v;
.locals init ([] string input,
[] int32 v)
// System.Console.WriteLine("Hi. Please input a string:");
ldstr "Hi. Please input a string:"
call void [mscorlib]System.Console::WriteLine(string)
// input = System.Console.ReadLine();
call string [mscorlib]System.Console::ReadLine()
stloc.0
// System.Console.WriteLine(input);
ldloc.0
call void [mscorlib]System.Console::WriteLine(string)
// v = System.Int32.Parse(input);
ldloc.0
call int32 [mscorlib]System.Int32::Parse(string)
stloc.1
// MyIlApp.MainClass._val = 9000;
ldc.i4
stsfld int32 MyIlApp.MainClass::_val
// System.Console.WriteLine(MyIlApp.MainClass._val.ToString());
ldsflda int32 MyIlApp.MainClass::_val
call instance string [mscorlib]System.Int32::ToString()
call void [mscorlib]System.Console::WriteLine(string)
// MyIlApp.MainClass.check(v);
ldloc.1
call void [MyIlApp]MyIlApp.MainClass::check(int32)
/* Test Case 2 */
// VC vc1;
.locals init ([] valuetype [MyIlApp]MyIlApp.VC vc1)
// vc1.val_string = "Test string of VC";
ldloca.s
ldstr "Test string of VC"
stfld string MyIlApp.VC::val_string
// vc1.val_int32 = 8;
ldloca.s
ldc.i4.8
stfld int32 MyIlApp.VC::val_int32
// System.Console.WriteLine(vc1.val_string);
ldloca.s
ldfld string MyIlApp.VC::val_string
call void [mscorlib]System.Console::WriteLine(string)
// System.Console.WriteLine(vc1.val_int32.ToString()); // 《Expert .NET 2.0 IL Assembler》上说 ldflda “不能使用值类型的实例,也不能获取指向值类型实例的对象引用或指针”,有误。为了实现类似 a.X = 100(a 为 System.Drawing.Point 类型)这样的调用,需要用到 call 指令,也就需要用到 ldflda 而不是 ldfld;不过后者可以用在不访问实例函数/字段的情况下
ldloca.s
ldflda int32 MyIlApp.VC::val_int32
call instance string [mscorlib]System.Int32::ToString()
call void [mscorlib]System.Console::WriteLine(string)
// return;
ret
}
}
}
MSIL Hello World的更多相关文章
- Mono为何能跨平台?聊聊CIL(MSIL)
前言: 其实小匹夫在U3D的开发中一直对U3D的跨平台能力很好奇.到底是什么原理使得U3D可以跨平台呢?后来发现了Mono的作用,并进一步了解到了CIL的存在.所以,作为一个对Unity3D跨平台能力 ...
- 浅析MSIL中间语言——基础篇
一.开篇 研究MSIL纯属于个人喜好,说在前面MSIL应用于开发的地方很少,但是很大程度上能够帮着我们理解底层的原理,这是我了解MSIL的主要原因.托管代码表示应用程序的方法的功能,它们以微软的中间语 ...
- 写给自己的 程序集&msil 扫盲
嘴上不说 心里却想MD 这家伙在博客园装了这么久的高手 竟然连这都不会 ,我去噢. 程序集签名 .net 下 “程序集” 什么东东 ,反正就是听着挺牛x的,其实就是指“一堆程序”从我们传统的C++封装 ...
- 代码大全 MSIL语言程序设计
.NET平台的编译器会将高级语言(C#,VB.NET,F#)编译成MSIL(微软中间语言)格式.熟悉MSIL语言,可以读懂一些加密程序混淆过的算法,这些算法几乎不能还原成高级语言,但是可以还原成MSI ...
- MSIL 教程(三):类和异常处理(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857771.html 续上文[翻译]MSIL 教程(二):数组.分支.循环.使用不安全代 ...
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...
- MSIL解析一(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/15/857140.html 在网上发现了一个非常好的MSIL教程,可惜是英文版的,于是就翻译了 ...
- MSIL指令集
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- 解决:warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;找到 MSIL .netmodule 或使用 /GL 编译的模块;正在。。;LINK : warning LNK4075: 忽略“/INCREMENTAL”(由于“/LTCG”规范)
参考资料: http://blog.csdn.net/laogaoav/article/details/8544880 http://stackoverflow.com/questions/18612 ...
随机推荐
- Window.focus()让页面成为当前窗体
Window.focus()让页面成为当前窗体 最近在弄在线客服的时候,想在收到信息时候让窗体自动弹出到最前,最小化的时候也是弹出到最前.本来以为很麻烦,问了好多人,都不知道,在网上查资料也没有查到. ...
- 【转】ubuntu下最好用的输入法fcitx-sunpinyin
http://www.freetstar.com/index.php/ubuntu-most-use-friendly-fcitx-sunpinyin 今天难得折腾一会儿输入法,对于系统美化方面的 ...
- Tomcat 中响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding
户端(PC浏览器或者手机浏览器)在接受到Tomcat的响应的时候,头信息通常都会带上Content-Length ,一般情况下客户端会在接受完Content-Length长度的数据之后才会开始解析.而 ...
- 使用Junit等工具进行单元测试
一.类的定义: 类是同一事物的总称,类是封装对象的属性和行为的载体,反过来说具有相同属性和行为的一类实体被称为类. 二.Junit工具的使用: 1.首先新建一个项目叫JUnit_Test,我们编写一个 ...
- C#线程入门---转载
C#中的线程(一)入门 文章系参考转载,英文原文网址请参考:http://www.albahari.com/threading/ 作者 Joseph Albahari, 翻译 Swanky Wu 中 ...
- 剑指Offer面试题:34.翻转单词顺序VS左旋转字符串
一.题目一:翻转单词顺序 1.1 题目说明 题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理.例如输入字符串"I am a st ...
- (源码下载)高灵活度,高适用性,高性能,轻量级的 ORM 实现
我在上一篇博客中简单说明了一个面向内存数据集的“ORM”的实现方法,也提到我的设计实现或许不能称之为“ORM”,姑且称之为 S-ORM吧. 可能有些小伙伴没有理解我的思路和目的,与传统ORM框架做了简 ...
- SQL Server 索引和表体系结构(包含列索引)
包含列索引 概述 包含列索引也是非聚集索引,索引结构跟聚集索引结构是一样,有一点不同的地方就是包含列索引的非键列只存储在叶子节点:包含列索引的列分为键列和非键列,所谓的非键列就是INCLUDE中包含的 ...
- Servlet程序中玩验证码
验证码思想:所谓验证码就是产生若干随机数,存放到session中,然后在servlet中获取session中的该值与页面输入值相比较,进而判断正误. 产生验证码的方法: 随机数放在图片中,封装为一 ...
- Thrift架构~目录
回到占占推荐博客索引 概念相关 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ru ...