了解IL汇编异常处理语法

165 篇文章 18 订阅

从网上拷过来一个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(模块)的元数据项,它标识了当前的模块;

先了解一下;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值