我发现了IL code
一个简单的程序:
long x = 0;
for(long i = 0;i< int.MaxValue * 2L; i++)
{
x = i;
}
Console.WriteLine(x);
我在发布模式下构建此代码并IL code
生成:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 28 (0x1c)
.maxstack 2
.locals init ([0] int64 x,
[1] int64 i)
IL_0000: ldc.i4.0
IL_0001: conv.i8
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: conv.i8
IL_0005: stloc.1
IL_0006: br.s IL_000f
IL_0008: ldloc.1
IL_0009: stloc.0
IL_000a: ldloc.1
IL_000b: ldc.i4.1
IL_000c: conv.i8
IL_000d: add
IL_000e: stloc.1
IL_000f: ldloc.1
IL_0010: ldc.i4.s -2
IL_0012: conv.u8
IL_0013: blt.s IL_0008
IL_0015: ldloc.0
IL_0016: call void [mscorlib]System.Console::WriteLine(int64)
IL_001b: ret
} // end of method Program::Main
我弄清楚了几乎所有的指令,除了这个:
IL_0010: ldc.i4.s -2
现在这个指令应该推入int.MaxValue * 2L
堆栈,然后blt.s
将它与 进行比较i
,如果i
小于该值返回到IL_0008
。但是,我无法弄清楚为什么它加载-2?如果我改变如下循环:
for(long i = 0;i < int.MaxValue * 3L; i++)
{
x = i;
}
它加载期望值:
IL_0010: ldc.i8 0x17ffffffd
那么-2
这段代码中的含义是什么?