gpt4 book ai didi

c# - IL 代码,有人帮我解释一下为什么 ldarg.0 出现两次?

转载 作者:太空狗 更新时间:2023-10-29 18:06:36 25 4
gpt4 key购买 nike

这是c#代码

class SimpleIL { 
private int f = 2;
public void M1() { M2(f); }
public void M2(Object p) { Console.WriteLine(p); }
}

这是M1方法的IL

 IL_0000:  nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldfld int32 ConsoleApplication1.SimpleIL::f
IL_0008: box [mscorlib]System.Int32
IL_000d: call instance void ConsoleApplication1.SimpleIL::M2(object)
IL_0012: nop
IL_0013: ret

我的问题是:为什么两次 ldarg.0?

最佳答案

IL 代码可以这样分组:

IL_0000:  nop

IL_0001: ldarg.0

// get the value for the parameter
IL_0002: ldarg.0
IL_0003: ldfld int32 ConsoleApplication1.SimpleIL::f
IL_0008: box [mscorlib]System.Int32

// call "this.M2(...)", this is already on the stack from before
IL_000d: call instance void ConsoleApplication1.SimpleIL::M2(object)

IL_0012: nop
IL_0013: ret

要使用 IL 调用方法,您可以这样做:

load instance reference on the stack
load argument values on the stack
call method

这里先将“实例引用”加载到栈上,然后添加获取参数值的代码,这也涉及到在栈上获取实例引用,然后才是使用实例引用的实际调用。

关于c# - IL 代码,有人帮我解释一下为什么 ldarg.0 出现两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com