[英]What is the meaning of -2 in this IL instruction?
I was discovering the IL code
of a simple program: 我发现了一个简单程序的
IL code
:
long x = 0;
for(long i = 0;i< int.MaxValue * 2L; i++)
{
x = i;
}
Console.WriteLine(x);
I build this code in Release mode and this IL code
is generated: 我在发布模式下构建此代码,并生成此
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
I figure out pretty much all the insructions except this: 除了这个,我几乎找出了所有的解释:
IL_0010: ldc.i4.s -2
Now this insruction should push int.MaxValue * 2L
onto the stack and then blt.s
will compare it with i
, if i
is less than the value go back to the IL_0008
.But, what I can't figure out is that why it loads -2 ? 现在这个insruction应该将
int.MaxValue * 2L
推入堆栈然后blt.s
将它与i
进行比较,如果i
小于该值则返回到IL_0008
。但是,我无法弄清楚的是为什么它载荷-2 ? If I change the loop like below: 如果我改变循环如下:
for(long i = 0;i < int.MaxValue * 3L; i++)
{
x = i;
}
It loads the expected value: 它加载预期值:
IL_0010: ldc.i8 0x17ffffffd
So what is the meaning of -2
in this code? 那么这个代码中
-2
的含义是什么?
int.MaxValue * 2L
is a 64-bit number, which however still fits into 32-bits ( 4,294,967,294
, or 0xFFFFFFFE
). int.MaxValue * 2L
是一个64位数字,但仍然适合32位( 4,294,967,294
或0xFFFFFFFE
)。 So, the compiler is loading 0xFFFFFFFE
(which is equal to -2 when interpreted as Int32
) and then extending it to an unsigned 64-bit value. 因此,编译器加载
0xFFFFFFFE
(当解释为Int32
时等于-2),然后将其扩展为无符号的64位值。
The reason it used the signed form is that the number, when interpreted as a signed value -2
, fits into a single signed byte ( -128
to 127
), meaning that the compiler was able to emit the short form ldc.i4.s
opcode to load a 32-bit value from a single byte. 它使用带符号形式的原因是,当被解释为有符号值
-2
时,该数字适合单个带符号的字节( -128
到127
),这意味着编译器能够发出短形式ldc.i4.s
操作码从单个字节加载32位值。 It only took 2 bytes to load the 32-bit signed integer, and additional 1 byte to convert it to a 64-bit value - this is far better than using a 64-bit load instruction followed by a full 8 byte unsigned integer. 它只需要2个字节来加载32位有符号整数,另外1个字节将其转换为64位值 - 这比使用64位加载指令后跟一个完整的8字节无符号整数要好得多。
It looks like the compiler is using bitwise mathematics to its advantage. 看起来编译器正在使用按位数学来发挥它的优势。 It just so happens that the Two's Complement value of -2 is equal to the unsigned integers value of (int.MaxValue*2L)
碰巧的是, Two的Complement值-2等于(int.MaxValue * 2L)的无符号整数值
In the bitwise representation: 在按位表示:
- 1111 1111 1111 1111 1111 1111 1111 1110 (int)
- 1111 1111 1111 1111 1111 1111 1111 1110 (uint)
- 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1110 (long
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.