了解IL汇编异常处理语法

从网上拷过来一个IL汇编程序,编译时先报如下错,

        看它是把空格识别为了下注红线的字符,这是字符编码的问题,用记事本替换功能替换了;

然后又报如下的错,

 

看不出来问题,拷一句正确的来,整个替换;

 

然后又报如下的错,

 

    这次一直改不了;看了一阵,只要把,

            ldstr "计算完毕, 结果为:"
            call void [mscorlib]System.Console::WriteLine(string)

    "计算完毕, 结果为:",加上冒号就可以,去掉冒号总会出上面的错,不知道是哪儿编码的问题;

然后构建好了;

 

完整的代码如下,
 

.assembly extern mscorlib {auto}
.assembly AndCount{}
.module andCount.exe
.namespace AndCount {
    .class public AndCount extends [mscorlib]System.Object{
        .method public static void AddCount() cil managed {
            .entrypoint
            .locals init (int32 retValue)
        .try{
        AskAfterNum:
            ldstr "-------------------------------------------------------\n加法载入"
            call void [mscorlib]System.Console::Write(string)
            ldstr "\n请输入第一个数字:"
            call void [mscorlib]System.Console::Write(string)
            call string [mscorlib]System.Console::ReadLine()
            call int32 [mscorlib]System.Convert::ToInt32(string)
            ldstr "\n请输入第二个数字:"
            call void [mscorlib]System.Console::Write(string)
            call string [mscorlib]System.Console::ReadLine()
            call int32 [mscorlib]System.Convert::ToInt32(string)
            add
            stloc retValue
            ldloc retValue
            brfalse Error
            ldstr "计算完毕, 结果为:"
            call void [mscorlib]System.Console::WriteLine(string)
            ldloc retValue
            br PrintCount
        Error:
            ldstr "error"
        PrintCount:
            call void [mscorlib]System.Console::WriteLine(int32)
            ldloc retValue
            brtrue AskAfterNum
            } 
           catch [mscorlib]System.Exception
            {
                pop
                ldstr "\n临时错误!\n"
                call void [mscorlib]System.Console::Write(string)
                br AskAfterNum
            }
            ret
        }
    }
}
.field public static valuetype CharArray8 Format at FormatData
.data FormatData = bytearray(25 64 00 00 00 00 00 00)
.class public explicit CharArray8
    extends [mscorlib]System.ValueType { .size 8 }
.method public static pinvokeimpl("msvcrt.dll" cdecl)
    vararg int32 sscanf(string,int8*) cil managed{ }

但是运行又会出错;不知道是哪儿的问题; 

 

 此程序包括有异常处理,先了解一下IL汇编的异常处理语法;

头部的指令,

.assembly extern mscorlib { auto } 定义了一个名为AssemblyRef的元素据项,它标识了这个程序中使用的外部托管应用程序(程序集);在这里使用的是Mscorlib.dll,这是.NET Framework类库的主程序集;

.assembly Xxxx {} 定义了一个名为Assembly(程序集)的元数据项;

.module Xxxxx.exe定义了一个名为Module(模块)的元数据项,它标识了当前的模块;

先了解一下;

猜你喜欢

转载自blog.csdn.net/bcbobo21cn/article/details/132138451
今日推荐