我试图了解有关.NET MSIL的更多信息,因此我想编写自定义IL程序集。我坚持使用以下示例。
首先我在OSX上,使用Mono" Mono JIT编译器版本4.8.1(mono-4.8.0-branch / 22a39d7 Fri Apr 7 12:00:08 EDT 2017)"我的il组装看起来像。完整档案:
.assembly extern mscorlib
{
.ver 4:0:0:0
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
}
.assembly 'Application'
{
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module sample.exe // GUID = {B239DF75-1D4B-4A83-B8B6-99BDEFD7B8A6}
.class public auto ansi MainType
extends [mscorlib]System.Object
{
// method line 1
.method public specialname rtspecialname
instance default void '.ctor' () cil managed
{
// Method begins at RVA 0x20ec
// Code size 7 (0x7)
.maxstack 8
ldarg.0
call instance void object::'.ctor'()
ret
} // end of method MainType::.ctor
// method line 2
.method public static hidebysig
default void Main (string[] A_0) cil managed
{
// Method begins at RVA 0x20f4
.entrypoint
// Code size 87 (0x57)
.maxstack 6
.locals init (
int32[] V_0,
int32 V_1)
// Allocating array of size 200
ldc.i4 200
newarr int32[]
stloc.0
// Pushing the numbers I want to output
ldc.i4 100
ldc.i4 101
ldc.i4 102
// 3 is the total number of previous pushed numbers
// it functions in this programm as a counter which
// will be decremented
ldc.i4 3
stloc.1 // storing the 3 in the temp variable V_1
ldloc.0 // push the array reference onto stack
ldc.i4 0 // push the index onto the stack
ldloc.1 // loading the 3 from the temp int32 var (V_1), push it onto the stack
stelem.i4 // save the value into the array at index 0 (defined above aka pushed on the stack)
// when it enters first, on the top of the stack should
// be the value 102 which we want to print out:
loop: call void class [mscorlib]System.Console::WriteLine(int32) // the call should consume 102 from the stack and print it out
// loading the counter from array
ldloc.0
ldc.i4 0
ldelem.i4
ldc.i4 1 // pushing 1
sub // subtract the counter by 1
// Storing the result which is on the stack
// in the array index 0 (our counter):
stloc.1
ldloc.0
ldc.i4 0
ldloc.1
stelem.i4
// Loading the counter value again onto stack to perform
// the check for branching:
ldloc.0
ldc.i4 0
ldelem.i4
ldc.i4 0 // pushing 0 to see if the counter is 0
ceq
brfalse loop // if its not yet 0 then we jump to the loop label; which means it should on the next call to WriteLine consume the next value which is on the stack, which should be 101
ret
} // end of method MainType::Main
} // end of class MainType
有趣或有问题的"部分在这里:
.method public static hidebysig
default void Main (string[] A_0) cil managed
{
// Method begins at RVA 0x20f4
.entrypoint
// Code size 87 (0x57)
.maxstack 6
.locals init (
int32[] V_0,
int32 V_1)
// Allocating array of size 200
ldc.i4 200
newarr int32[]
stloc.0
// Pushing the numbers I want to output
ldc.i4 100
ldc.i4 101
ldc.i4 102
// 3 is the total number of previous pushed numbers
// it functions in this programm as a counter which
// will be decremented
ldc.i4 3
stloc.1 // storing the 3 in the temp variable V_1
ldloc.0 // push the array reference onto stack
ldc.i4 0 // push the index onto the stack
ldloc.1 // loading the 3 from the temp int32 var (V_1), push it onto the stack
stelem.i4 // save the value into the array at index 0 (defined above aka pushed on the stack)
// when it enters first, on the top of the stack should
// be the value 102 which we want to print out:
loop: call void class [mscorlib]System.Console::WriteLine(int32) // the call should consume 102 from the stack and print it out
// loading the counter from array
ldloc.0
ldc.i4 0
ldelem.i4
ldc.i4 1 // pushing 1
sub // subtract the counter by 1
// Storing the result which is on the stack
// in the array index 0 (our counter):
stloc.1
ldloc.0
ldc.i4 0
ldloc.1
stelem.i4
// Loading the counter value again onto stack to perform
// the check for branching:
ldloc.0
ldc.i4 0
ldelem.i4
ldc.i4 0 // pushing 0 to see if the counter is 0
ceq
brfalse loop // if its not yet 0 then we jump to the loop label; which means it should on the next call to WriteLine consume the next value which is on the stack, which should be 101
ret
} // end of method MainType::Main
我得到的错误如下:
Unhandled Exception:
System.InvalidProgramException: Invalid IL code in MainType:Main (string[]): IL_0056: ret
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in MainType:Main (string[]): IL_0056: ret
" IL_0056:ret"指向该行(当我反汇编生成的可执行文件时):
IL_0044: ldc.i4 0
IL_0049: ldelem.i4
IL_004a: ldc.i4 0
IL_004f: ceq
IL_0051: brfalse IL_0028
IL_0056: ret //// <<<< HERE
} // end of method MainType::Main
一些重要说明: *存放时分配到数组似乎我总是做一些不必要的&#34;舞蹈&#34;。我这样做是因为最后我想创建一个堆栈机器,它只适用于堆栈中的值。这也是代码看起来像这样的原因。在编写之前,我想看看如何在MSIL中完成它。
如果有人能告诉我这里有什么问题,我真的很感激。非常感谢你。
这是我的第一个问题,我希望我已经提供了所有必要的信息以帮助我: - )
PS:我还尝试使用MonoDevelop和#34; Debug Application&#34;但是我无法从中获得任何有意义的数据,gdb也不能很好地为我工作。
PPS:我当时认为我仍然在堆栈上留下了价值但是当我在最后添加一个流行音乐(或更多)时,它只是告诉我&#39; pop&#39;是无效的。
修改
我尝试打印某种输出以查看出现了什么问题,并且我发现在“循环”之间出现了问题。标签它没有正确地从堆栈中弹出值。我还不知道为什么,但这里有一个示例输出,清楚地显示它:
102
LOOP START
101
---
101
---
101
---
exit now
100
即使我进一步修改il代码,它也会打印更多值:
LOOP START
102
101
100
---
102
101
100
---
102
101
100
---
exit now
它不会从堆栈中弹出。出于某些奇怪的原因,打电话给WriteLine&#39;没有弹出值,或者它看起来像是备份&amp;恢复堆栈,但这只是我的假设。这有什么用:.NET CIL manipulation of evaluation stack(&#34; Backward Branch Constraints&#34;,Link:http://jilc.sourceforge.net/ecma_p3_cil.shtml)