August 17th, 2007

What are these spurious nop instructions doing in my C# code?

Prerequisites: Basic understanding of assembly language. When you debug through some managed code at the assembly level, you may find that there are an awful lot of nop instructions scattered throughout your method. What are they doing there; isn’t the JIT smart enough to remove them? Isn’t this going to slow down execution of my program? It is my understanding that¹ this nop instructions are inserted by the JIT because you’re running the program under the debugger. They are emitted specifically so that the debugger can set breakpoints in locations that you normally wouldn’t be able to. (For example, they might represent a line of code that got optimized out or merged with another line of code.) Don’t worry. If there’s no debugger, the JIT won’t generate the dummy nops. Nitpicker’s Corner

¹As with all statements of alleged fact, this statement is an interpretation of events based on observation and thought and does not establish a statement of the official position of the CLR JIT compiler team or Microsoft Corporation, and that interpretation may ultimately prove incorrect.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.