代码之家  ›  专栏  ›  技术社区  ›  DaveDev

如果在伊利诺伊州看起来像什么?

  •  3
  • DaveDev  · 技术社区  · 14 年前

    什么是 if

    这是C#中一个非常简单的构造。sombody能给我一个更抽象的定义吗?

    4 回复  |  直到 14 年前
        1
  •  10
  •   Timwi    14 年前

    这里有一些 if 陈述及其如何翻译成IL:

    ldc.i4.s 0x2f                      var i = 47;
    stloc.0 
    
    ldloc.0                            if (i == 47)
    ldc.i4.s 0x2f
    bne.un.s L_0012
    
    ldstr "forty-seven!"                   Console.WriteLine("forty-seven!");
    call Console::WriteLine
    
    L_0012:
    ldloc.0                            if (i > 0)
    ldc.i4.0 
    ble.s L_0020
    
    ldstr "greater than zero!"             Console.WriteLine("greater than zero!");
    call Console::WriteLine
    
    L_0020:
    ldloc.0                            bool b = (i != 0);
    ldc.i4.0 
    ceq 
    ldc.i4.0 
    ceq 
    stloc.1 
    
    ldloc.1                            if (b)
    brfalse.s L_0035
    
    ldstr "boolean true!"                  Console.WriteLine("boolean true!");
    call Console::WriteLine
    
    L_0035:
    ret
    

    这里需要注意的一点是:IL指令总是相反的。 if (i > 0) i <= 0 ,然后跳过 阻止。

        2
  •  4
  •   Mark Cidade    14 年前

    使用的分支指令将根据堆栈顶部的值跳转到目标指令。

    brfalse Branch to target if value is zero (false)
    brtrue  Branch to target if value is non-zero (true)
    beq     Branch to target if equal
    bge     Branch to target if greater than or equal to
    bgt     Branch to target if greater than
    ble     Branch to target if less than or equal to
    blt     Branch to target if less than
    bne.un  Branch to target if unequal or unordered
    
        3
  •  3
  •   Andrew Hare    14 年前

    if . 例如,如果您正在对照 null 编译器将发出 brfalse 指令(或a) brtrue

    这个 如果 ILDASM 或者反射器是更好的学习工具。

        4
  •  1
  •   Femaref    14 年前

    一个简单的例子:

    ldloc.1                    // loads first local variable to stack
    ldc.i4.0                   // loads constant 0 to stack
    beq                        // branch if equal
    

    这等于

    if(i == 0) //if i is the first local variable
    

    有一篇关于 codeproject 关于这个。