为什么在这种情况下使用“ br.s” IL操作码? [英] Why is the 'br.s' IL opcode used in this case?

查看:314
本文介绍了为什么在这种情况下使用“ br.s” IL操作码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于教育目的,我正在学习一些IL(主要是因为我很好奇引擎盖下的'%'发生了什么(原来是rem)并开始偏离题名...)。

For educational purposes I'm learning a bit of IL (mainly because I was curious what happens to '%' under the hood (which turns out to be rem) and started digressing...).

我写了一个方法,只是返回true来使事情分解,并想知道'br.s'操作码:

I wrote a method, just returning true to break things down a bit and was wondering about the 'br.s' opcode:

.method public hidebysig static bool  ReturnTrue() cil managed
{
  // Code size       7 (0x7)
  .maxstack  1
  .locals init ([0] bool CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.1
  IL_0002:  stloc.0
  IL_0003:  br.s       IL_0005
  IL_0005:  ldloc.0
  IL_0006:  ret
} // End of method Primes::ReturnTrue

在ldc.i4.1将1压入堆栈并stloc.0将其放置在第0个本地地址后,br.s基本上(据我所知)在行IL_0005。

After ldc.i4.1 pushes 1 on the stack and stloc.0 places this in the 0th local, br.s basically (as far as I know) does a 'goto' to ldloc.0 at line IL_0005.

为什么?为什么根本没有IL_0004行,因此可以省略?

Why is this? Why is there simply no IL_0004 line so this could be omitted?

推荐答案

该分支用于调试,返回值是计算并存储起来,现在可以调用调试器。与方法条目中的 NOP 相同。

That branch is for debugging purposes, the return value has been calculated and stored away and now the debugger can be "called". It's the same with the NOP in the method entry.

关于 IL_0004 ,如@hvd所述, br.s 有一个地址,不适合一行,此处为一个字节(我没有知道您对寻址的熟悉程度,但是一条指令通常是一个字节(即8位),以及地址或偏移量(通常是8位,16位或32位),在这种情况下,我们有8位具有8位偏移量的位操作码。维基百科在CIL-OP代码方面有不错的文章

With regards to IL_0004, as @hvd states, br.s has an address and doesn't fit in "one row", one byte here (I don't know how familiar you are with addressing, but one instruction usually is one byte, that is, 8-bit, as well as the address or offset, typically 8-, 16- or 32-bit. In this case we have an 8-bit opcode with an 8-bit offset. Wikipedia has a good article on CIL-OP-codes).

另外,假设您的方法有多个返回值,例如,通过 if 分支,它们都跳转最后,根据您的情况, IL_0005 ,因此在函数返回时只需要一个断点。

Additionally, let's say your method has multiple returns and via for example if-branches, all of them jump to the end, IL_0005 in your case, so only one breakpoint is needed at function return.

这篇关于为什么在这种情况下使用“ br.s” IL操作码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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