变量比较

发布于 2024-08-26 06:42:41 字数 617 浏览 3 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-09-02 06:42:41

第二个 ceq 操作码将第一个操作码的结果与 0 进行比较。 (false)

然后将此结果放入一个变量中,从该变量加载,如果为 true,则跳过 WriteLine 调用。

我认为发布模式会生成更高效的代码,但我懒得检查。

The second ceq opcode compares the result of the first one to 0. (false)

This result is then put in a variable, loaded from the variable, and, if it was true, the WriteLine call is skipped.

I would assume that Release mode generates more efficient code, but I'm too lazy to check.

起风了 2024-09-02 06:42:41

关于与 ceq 进行比较来自 MSDN

如果它们相等,则将整数值 1 (int32) 压入计算堆栈;否则 0 (int32) 被压入计算堆栈。

第二个ceq检查第一次比较是否失败,如果失败,则跳转到退出点。

About comparing values with ceq From MSDN:

If they are equal, the integer value 1 (int32) is pushed onto the evaluation stack; otherwise 0 (int32) is pushed onto the evaluation stack.

The second ceq checks whether the first comparison failed, if so, it jumps to the exit point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文