张银的博客


Eat to live, but do not live to eat.

导航

MSIL条件跳转(简单注释)

Posted on 2012-01-15 00:17  张银  阅读(578)  评论(0编辑  收藏  举报

c# code:

using System;
public class aidd2008
{
public static void Main(String[] argv)
{
int x = 10;
int y = 6;
if (x > y)
{
Console.WriteLine(x);
}
else
{
Console.WriteLine(y);
}
Console.Read();
}
}

MSIL code:

// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit aidd2008
extends [mscorlib]System.Object
{
.method public hidebysig static void Main(string[] argv) cil managed
{
.entrypoint
// 代码大小 44 (0x2c)
.maxstack 2
.locals init (int32 V_0,
int32 V_1,
bool V_2)//定义三个变量
IL_0000: nop
IL_0001: ldc.i4.s 10//把整型值10载入堆栈
IL_0003: stloc.0//把刚才载入堆栈的10赋值给第一个local变量,int x=10
IL_0004: ldc.i4.6//把整型值6载入堆栈
IL_0005: stloc.1//把刚才载入堆栈的6赋值给第二个local变量,int y=6
IL_0006: ldloc.0
IL_0007: ldloc.1//这两句,把x,y两个local变量载入堆栈
IL_0008: cgt//比较x,y的大小,结果会保存在堆栈最上方(结果以0或1表示true ,false)
IL_000a: ldc.i4.0//把0载入堆栈
IL_000b: ceq//比较0和cgt的运算结果是否相等,结果会保存在堆栈最上方(结果以0或1表示true ,false)
IL_000d: stloc.2//ceq的运算结果保存入bool变量中
IL_000e: ldloc.2//再把ceq运算结果载入
IL_000f: brtrue.s IL_001c//判断跳转,若跳转显式Y,不跳转显式X
IL_0011: nop
IL_0012: ldloc.0
IL_0013: call void [mscorlib]System.Console::WriteLine(int32)
IL_0018: nop
IL_0019: nop
IL_001a: br.s IL_0025

IL_001c: nop
IL_001d: ldloc.1
IL_001e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0023: nop
IL_0024: nop
IL_0025: call int32 [mscorlib]System.Console::Read()
IL_002a: pop
IL_002b: ret
} // end of method aidd2008::Main