What's the purpose of the CIL nop opcode?

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 opcode is patched. They're used a lot more in debug builds than release builds.

I know that these kinds of statements are used in assembly languages to align later instructions, but why are MSIL nops needed in MSIL?

(Editor's note: the accepted answer is about machine-code NOPs, not MSIL/CIL NOPs which the question originally asked about.)


NOPs serve several purposes:

  • They allow the debugger to place a breakpoint on a line even if it is combined with others in the generated code.
  • It allows the loader to patch a jump with a different-sized target offset.
  • It allows a block of code to be aligned at a particular boundary, which can be good for caching.
  • It allows for incremental linking to overwrite chunks of code with a call to a new section without having to worry about the overall function changing size.

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.


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