最近由于需要,开始阅读 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
} }
}

最新文章

  1. uiautomator跑安卓端UI testing
  2. C++混合编程之idlcpp教程Lua篇(7)
  3. Android消息推送怎么实现?
  4. 在使用sqlite时淌过的坑
  5. LeetCode Longest Increasing Path in a Matrix
  6. Scrum 项目3.0
  7. LINQ to XML 实战
  8. ffmpeg/ffplay vc6 源码剖析
  9. git/ TortoiseGit 在bitbucket.org 使用证书登陆
  10. find中的-print0和xargs中-0的区别
  11. ftp以及smb的配置
  12. 【安富莱】【RL-TCPnet网络教程】第8章 RL-TCPnet网络协议栈移植(RTX)
  13. mysql文件
  14. Pandas 学习记录(一)
  15. 关于video.js不错的一篇博客,
  16. 字节码分析finally块对return返回值的影响
  17. Qt之美(一):D指针/私有实现
  18. jquery 之 extend的实现
  19. 解题:APIO 2012 派遣
  20. 在windows的IDEA运行Presto

热门文章

  1. 有哪些LabVIEW快捷键让你相见恨晚
  2. HDU2159 二维完全背包
  3. scrapy 和 scrapy_redis 安装
  4. Unity小游戏制作 - 暗影随行
  5. 如何创建一个Edge 浏览器扩展
  6. Android中Intent的用法总结
  7. 剑指Offer面试题:23.二叉树中和为某一值的路径
  8. Why MVC is Better?(翻译)
  9. [.net 面向对象程序设计深入](4)MVC 6 —— 谈谈MVC的版本变迁及新版本6.0发展方向
  10. Google软件构建工具Bazel FAQ