What's the purpose of the CIL nop opcode?

前端 未结 19 1278
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 10:42

I\'m going through MSIL and noticing there are a lot of nop instructions in the MSIL.

The MSDN article says they take no action and are used to fill space if the opc

相关标签:
19条回答
  • 2020-12-07 10:49

    It may also make code run faster, when optimizing for specific processors or architectures:

    Processors for a long time employ multiple pipelines that work roughly in parallel, so two independent instruction can be exceuted at the same time. On a simple processor with two pipelines, the first may support all instructions, whereas the second supports only a subset. Also, there are some stalls between the pipelines when one has to wait for the result of a previous instruction that isn't finished yet.

    Under these circumstances, a dedicated nop may force the next instruction into a specific pipeline (the first, or not the first), and improve the pairing of following instructions so that the cost of the nop is more than amortized.

    0 讨论(0)
  • 2020-12-07 10:50

    It provides an opportunity for line-based markers (e.g. breakpoints) in the code where a release build would emit none.

    0 讨论(0)
  • 2020-12-07 10:54

    Dude! No-op is awesome! It is an instruction that does nothing but consume time. In the dim dark ages you would use it to do microadjustments in timing in critical loops or more importantly as a filler in self-modifying code.

    0 讨论(0)
  • 2020-12-07 10:54

    This is not an answer to your specific question, but back in the old days you could use a NOP to fill a branch delay slot, if you couldn't manage to fill it with an otherwise-useful instruction.

    0 讨论(0)
  • 2020-12-07 10:57

    They allow the linker to replace a longer instruction (typically long jump) with a shorter one (short jump). The NOP takes the extra space - the code could not be moved around as it would stop other jumps from working. This happens at link-time, so the compiler can't know whether a long or short jump would be appropriate.

    At least, that's one of their traditional uses.

    0 讨论(0)
  • 2020-12-07 11:02

    Here's how MSIL / CIL nops (not x86 machine code nop) are used by debugging:

    Nops are used by language compilers (C#, VB, etc.) to define implicit sequence points. These tell the JIT compiler where to ensure machine instructions can be mapped back to IL instructions.

    Rick Byer's blog entry on DebuggingModes.IgnoreSymbolStoreSequencePoints, explains a few of the details.

    C# also places Nops after call instructions so that the return site location in source is the call out rather than the line after the call.

    0 讨论(0)
提交回复
热议问题