为什么IL代码中stloc.0后面有一个ldloc.0?

2024-04-27

我正在尝试通过编写小代码片段和检查编译的程序集来学习 CIL。所以我写了这个简单的 if 语句:

    public static void Main (string[] args)
    {
        Int32 i = Int32.Parse (Console.ReadLine());
        if (i > 0)
            Console.WriteLine ("i is greater than 0");
    }

C#编译器将其编译成以下IL代码:

.method public hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 2
    .locals init (
        [0] int32 num,
        [1] bool flag)
    L_0000: nop 
    L_0001: call string [mscorlib]System.Console::ReadLine()
    L_0006: call int32 [mscorlib]System.Int32::Parse(string)
    L_000b: stloc.0 
    L_000c: ldloc.0 
    L_000d: ldc.i4.0 
    L_000e: cgt 
    L_0010: ldc.i4.0 
    L_0011: ceq 
    L_0013: stloc.1 
    L_0014: ldloc.1 
    L_0015: brtrue.s L_0022
    L_0017: ldstr "i is greater than 0"
    L_001c: call void [mscorlib]System.Console::WriteLine(string)
    L_0021: nop 
    L_0022: ret 
}

我所知,stloc将求值堆栈中的最高值放入局部变量中,如果我做对了,该值不会从堆栈中弹出,那么为什么编译器将ldloc指令就在它之后?


只有在调试模式下,您才能看到这些指令,因为编译器不会优化代码,因此您可以调试并在特定部分放置断点。

如果您在发布模式下编译此应用程序,您会发现即使在 IL 代码上也已完成优化。

.method public hidebysig static void 
  Main(
    string[] args
  ) cil managed 
{
  .entrypoint
  .maxstack 8

  // [13 13 - 13 55]
  IL_0000: call         string [mscorlib]System.Console::ReadLine()
  IL_0005: call         int32 [mscorlib]System.Int32::Parse(string)

  // [14 13 - 14 23]
  IL_000a: ldc.i4.0     
  IL_000b: ble.s        IL_0017

  // [15 17 - 15 58]
  IL_000d: ldstr        "i is greater than 0"
  IL_0012: call         void [mscorlib]System.Console::WriteLine(string)

  // [16 9 - 16 10]
  IL_0017: ret          

} // end of method Program::Main
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么IL代码中stloc.0后面有一个ldloc.0? 的相关文章

随机推荐