变量比较
以下 C# 代码片段:
var x = 1;
var y = 1;
if (x == y)
Console.Write("True");
生成此 MSIL:
.locals init (
[0] int32 x,
[1] int32 y,
[2] bool CS$4$0000)
L_0000: nop
L_0001: ldc.i4.1
L_0002: stloc.0
L_0003: ldc.i4.1
L_0004: stloc.1
L_0005: ldloc.0
L_0006: ldloc.1
L_0007: ceq
L_0009: ldc.i4.0
L_000a: ceq
L_000c: stloc.2
L_000d: ldloc.2
L_000e: brtrue.s L_001b
L_0010: ldstr "True"
L_0015: call void [mscorlib]System.Console::Write(string)
L_001a: nop
L_001b: ret
为什么有两个 ceq
调用?
谢谢
The following C#-snippet:
var x = 1;
var y = 1;
if (x == y)
Console.Write("True");
Generates this MSIL:
.locals init (
[0] int32 x,
[1] int32 y,
[2] bool CS$4$0000)
L_0000: nop
L_0001: ldc.i4.1
L_0002: stloc.0
L_0003: ldc.i4.1
L_0004: stloc.1
L_0005: ldloc.0
L_0006: ldloc.1
L_0007: ceq
L_0009: ldc.i4.0
L_000a: ceq
L_000c: stloc.2
L_000d: ldloc.2
L_000e: brtrue.s L_001b
L_0010: ldstr "True"
L_0015: call void [mscorlib]System.Console::Write(string)
L_001a: nop
L_001b: ret
Why is there two ceq
calls?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二个
ceq
操作码将第一个操作码的结果与0
进行比较。 (false
)然后将此结果放入一个变量中,从该变量加载,如果为
true
,则跳过WriteLine
调用。我认为发布模式会生成更高效的代码,但我懒得检查。
The second
ceq
opcode compares the result of the first one to0
. (false
)This result is then put in a variable, loaded from the variable, and, if it was
true
, theWriteLine
call is skipped.I would assume that Release mode generates more efficient code, but I'm too lazy to check.
关于与
ceq
进行比较来自 MSDN:第二个
ceq
检查第一次比较是否失败,如果失败,则跳转到退出点。About comparing values with
ceq
From MSDN:The second
ceq
checks whether the first comparison failed, if so, it jumps to the exit point.