网站与网页设计教程,扫黄除恶网站构造结构怎么做,租用网站的服务器,网站建设规划设计.NET平台的编译器会将高级语言(C#,VB.NET,F#)编译成MSIL(微软中间语言)格式。熟悉MSIL语言#xff0c;可以读懂一些加密程序混淆过的算法#xff0c;这些算法几乎不能还原成高级语言#xff0c;但是可以还原成MSIL语言。也可以知道一些高级语言之外的关于CLR的特性#xff….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 .cctorcall void [mscorlib]System.Console::WriteLine(string)ret
}
.method public void .ctor()
{.maxstack 1ldarg.0call instance void [mscorlib]System.Object::.ctor()ldstr .ctorcall 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::Buycall void [mscorlib]System.Console::WriteLine(string)ret}/* etc */
}
.class TownHouseextends House
{.method public virtual void Buy(){.maxstack 1ldstr TownHouse::Buycall 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 Im not a number// ldnull// ldstr 123call 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 Im not a numberldnull// ldstr 123call int32 [mscorlib]System.Int32::Parse(string)leave.s _CONTINUE
}
filter
{ldstr filter evaluation\n\tcall 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\tcall 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 inew 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 messagenewobj 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_castInt32(big);
ldloc big
conv.i4
stloc small 对应的C#语句是 Int32 small 123;
Int64 big small;small static_castInt32(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_castInt32(big);
ldloc big
conv.i4
stloc small 8 FOREACH语句 FOREACH语句应该是C#发明的未见其它语言有此语言以安全快速的方法遍历一个集合。 来看下面的这个例子C语言的例子 arrayint^ numbers gcnew arrayint { 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