.maxstack在ildasm中 [英] .maxstack in ildasm

查看:85
本文介绍了.maxstack在ildasm中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
{
    static void Main(string[] args)
    {
        int first = 10;
        int second = 20;
        int third = 30;
        int forth = 40;
        int fifth = 50;

    }
}


.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       18 (0x12)
  .maxstack  1
  .locals init ([0] int32 first,
           [1] int32 second,
           [2] int32 third,
           [3] int32 forth,
           [4] int32 fifth)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   10
  IL_0003:  stloc.0
  IL_0004:  ldc.i4.s   20
  IL_0006:  stloc.1
  IL_0007:  ldc.i4.s   30
  IL_0009:  stloc.2
  IL_000a:  ldc.i4.s   40
  IL_000c:  stloc.3
  IL_000d:  ldc.i4.s   50
  IL_000f:  stloc.s    fifth
  IL_0011:  ret
} // end of method Program::Main


我的问题是:
第一,第二,第三,第四和第五个变量被分配为堆栈上的局部变量
我认为对于堆栈上的五个变量.maxstack应该为5
但是.maxstack显示1 是什么意思?


My question is that:
first ,second, third ,forth and fifth variables are allocates as local variables on stack
I think for five variables on stack .maxstack should 5
but .maxstack shows 1 what it means?

推荐答案

必须对此进行查找,我已经精确地写了IL的0行,但您的问题很有趣:
.maxstack是最大评估堆栈深度/高度,通过IL,您在评估堆栈上只有1的最大值:

Had to look this up, I''ve written precisely 0 lines of IL, but your question is interesting:
.maxstack is the maximum evaluation stack depth/height, going through the IL, you only have a mamximum of 1 thing on the evaluation stack:

.locals init ([0] int32 first,
           [1] int32 second,
           [2] int32 third,
           [3] int32 forth,
           [4] int32 fifth)


声明局部变量.


Declares the local variables.

IL_0000:  nop


无操作?? -什么也没做,所以为什么要打扰?就像我说的那样,我不是这方面的专家.


No-operation?? - Does nothing, so why bother? Like I say, I''m not an expert in this stuff.

IL_0001:  ldc.i4.s   10


将常量10加载到评估堆栈中,堆栈大小=1.


Loads the constant 10 into the evaluation stack, stack size= 1.

IL_0003:  stloc.0


将值从评估堆栈弹出到locals [0]中.评估堆栈大小= 0.漂洗对其他变量重复上述操作,但是最大评估堆栈 size 保持为1,因为5个值中的每一个都被压入堆栈并分别弹出到变量中.

我想您可以假设这样重写(可能不会编译):


Pops the value off the evaluation stack into locals[0]. evaluation stack size=0. Rinse & repeat for the other variables, but the max evaluation stack size remains 1 because each of the 5 values is pushed into stack and popped into the variables individually.

I suppose you could hypothetically re-write like something this (probably won''t compile) :

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       18 (0x12)
  .maxstack  5
  .locals init ([0] int32 first,
           [1] int32 second,
           [2] int32 third,
           [3] int32 forth,
           [4] int32 fifth)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   10
  IL_0003:  ldc.i4.s   20
  IL_0004:  ldc.i4.s   30
  IL_0006:  ldc.i4.s   40
  IL_0007:  ldc.i4.s   50 //Stack size 5
  IL_0009:  stloc.4  //Stack size 4
  IL_000a:  stloc.3  //Stack size 3
  IL_000c:  stloc.2  //Stack size 2
  IL_000d:  stloc.1  //Stack size 1
  IL_000f:  stloc.0  //Stack size 0
  IL_0011:  ret
} 




这是对yuthub的回应,必须注意MSIL/CIL不在我的舒适范围内:

运行时有多个堆栈在运行.当const由ldc推送时,它将被推送到求值堆栈中(请参阅




This is in response to the comments by yuthub, it must be read with the caveat that MSIL/CIL is well outside my comfort zone:

There is more than one stack in operation when running. When the const is pushed by the ldc, it is pushed onto the evaluation stack (see here[^] for a good article). When the value is popped from the evaluation stack, in this case, being an int declare in a method, it is placed into a different stack, the stack is the one we''d normally discuss as "The stack" when discussing stack vs heap memory in c# for example. I''ve updated the earlier part of my question to help clarify.


这篇关于.maxstack在ildasm中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆