首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在CLR上学习IL

如何在CLR上学习IL
EN

Stack Overflow用户
提问于 2012-04-21 23:22:25
回答 4查看 6.9K关注 0票数 23

因为这些IL代码我看到的更多,所以我想学习如何正确地解释它们。

我找不到像C#编译器这样的文档或其他文档,所以我认为在我学习了下面这些常用的文档之后,我可以很好地处理剩下的事情:

下面是一些示例IL代码,其中包含我需要知道的内容:

示例1:

代码语言:javascript
复制
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derivedClass)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.DerivedClass::WriteOutput()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main

示例2:

代码语言:javascript
复制
.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "Hello"
  IL_0006:  stfld      string EnumReflection.DerivedClass::hello
  IL_000b:  ldarg.0
  IL_000c:  ldstr      "World"
  IL_0011:  stfld      string EnumReflection.DerivedClass::world
  IL_0016:  ldarg.0
  IL_0017:  ldc.i4.s   123
  IL_0019:  stfld      int32 EnumReflection.DerivedClass::age
  IL_001e:  ldarg.0
  IL_001f:  call       instance void EnumReflection.BaseClass::.ctor()
  IL_0024:  nop
  IL_0025:  ret
} // end of method DerivedClass::.ctor

我知道这些代码是做什么的,因为我产生了它们:-)然而,我想了解更多关于相应的IL代码。

这些示例包含像这样的IL代码,您能用问号来解释命令吗?还有,这些命令代表什么?这样我们就可以很容易地记住它们。

  • nop (用于调试- no operation)
  • newobj (似乎正在heap)
  • stloc.0中创建新对象?
  • ldloc.0 ?
  • ret ?
  • ldarg.0?

<代码>H121ldstr?<代码>H222<代码>H123stfld?<代码>H224<代码>H125ldc.i4.s?<代码>H226<代码>H127.ctor-构造函数<代码>H228<代码>F229

理解IL很重要,因为它揭示了特定编译器如何在特定情况下生成代码和执行操作。

然而,我找不到一个很好的文档,里面也包含了关于IL的例子。CLR with C# 3.0是一本很好的书,但是它最终不是一本IL书,所以它没有解释关于IL的一切。

编辑:

我找到了规范,他们告诉我:,感谢@

  • nop (用于调试- no operation)
  • newobj -创建新对象
  • stloc.0 -从堆栈到本地stloc.0的弹出值?-将本地变量加载到堆栈
  • ret-从
  • 返回ldarg.0-将参数0加载到堆栈。
  • ldstr -将文本字符串

stfld-

  • 加载到对象的字段中
  • ldc.i4.s -将num作为int32 (缩写)推送到堆栈中。
  • .ctor -构造函数
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10260272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档