ArticleslgStudy

computer science

NOP (code)

NOP (code) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand NOP (code) rather than just read about it. In short: In computer science, a NOP, no-op, or NOOP (pronounced "no op"; short for no operation) is a machine language instruction and its assembly language mnemonic, programming language statement, or computer protocol command that does nothing. Machine language instructions Some computer instruction sets include an instruction whose purpose is to not change the state of any of the programmer-accessible registers, status fl…

Key takeaways

  • NOP (code) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect NOP (code) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of NOP (code) from memory before moving on to harder problems.

Reference excerpt

In computer science, a NOP, no-op, or NOOP (pronounced "no op"; short for no operation) is a machine language instruction and its assembly language mnemonic, programming language statement, or computer protocol command that does nothing.

Machine language instructions Some computer instruction sets include an instruction whose purpose is to not change the state of any of the programmer-accessible registers, status flags, or memory. It often takes a well-defined number of clock cycles to execute. In some instruction sets, there is no explicit NOP instruction; the assembly language mnemonic NOP represents a regular instruction with parameters that make it do nothing such as the SPARC's sethi 0, %g0. A NOP must not access memory, as that could cause a memory fault or page fault. A NOP is most commonly used for timing purposes, to force memory alignment, to prevent hazards, to occupy a branch delay slot, to render void an existing instruction such as a jump, as a target of an execute instruction, or as a place-holder to be replaced by active instructions later on in program development (or to replace removed instructions when reorganizing would be problematic or time-consuming). In some cases, a NOP can have minor side effects; for example, on the Motorola 68000 series of processors, the NOP opcode causes a synchronization of the pipeline. Listed below are the NOP instruction for some CPU architectures:

From a hardware design point of view, unmapped areas of a bus are often designed to return zeroes; since the NOP slide behavior is often desirable, it gives a bias to coding it with the all-zeroes opcode.

Code A function or a sequence of programming language statements is a NOP or null statement if it has no effect. Null statements may be required by the syntax of some languages in certain contexts.

Ada In Ada, the null statement serves as a NOP. As the syntax forbids that control statements or functions be empty, the null statement must be used to specify that no action is required. (Thus, if the programmer forgets to write a sequence of statements, the program will fail to compile.)

C and derivatives The simplest NOP statement in C is the null statement, which is just a semi-colon in a context requiring a statement. Most C compilers generate no code for null statements, which has historical and performance reasons.

;

An empty block (compound statement) is also a NOP, and may be more legible, but will still have no code generated for it by the compiler.

{}

In some cases, such as the body of a function, a block must be used, but this can be empty. In C, statements cannot be empty—simple statements must end with a ; (semicolon) while compound statements are enclosed in {} (braces), which does not itself need a following semicolon. Thus in contexts where a statement is grammatically required, some such null statement can be used. The null statement is useless by itself, but it can have a syntactic use in a wider context, e.g., within the context of a loop:

alternatively,

or more tersely:

The last form might generate a warning with some compilers or compiler options, as a semicolon placed after a parenthesis at the end of a line usually indicates the end of a function call expression. The above code continues calling the function getchar() until it returns a \n (newline) character, essentially fast-forwarding the current reading location of standard input to the beginning of next line. As well as a NOP statement, C programs will also sometimes use a NOP expression; for instance, the C standard requires that, should the NDEBUG macro be defined before the <assert.h> header, the assert() macro defined by it be defined as the expression ((void)0). Similar techniques can be used by user code; for instance, consider the following code that defines a macro over the prefetch builtin function provided by GCC which is defined as a NOP expression if it's not available:

Fortran In Fortran, the CONTINUE statement is used in some contexts such as the last statement in a DO loop, although it can be used anywhere, and does not have any functionality.

JavaScript The JavaScript language does not have a built-in NOP statement. Many implementations are possible:

Use the ; empty statement or the {} empty block statement the same way as in the C and derivatives examples; Use the undefined or the null expression as a complete statement (an expression statement) when the previous methods are not allowed by the syntax. Alternatives, in situations where a function is required, are:

Use the Function.prototype() built-in function, that accepts any arguments and returns undefined; Use a NOP function available in a third-party library —see below; Define a custom NOP function, as in the following example (using the ES6 arrow function syntax):

AngularJS The AngularJS framework provides angular.noop function that performs no operations.

jQuery The jQuery library provides a function jQuery.noop(), which does nothing.

Lodash The Lodash library provides a function _.noop(), which returns undefined and does nothing.

Pascal As with C, the ; used by itself can be used as a null statement in Pascal. In fact, due to the specification of the language, in a BEGIN / END block, the semicolon is optional before the END statement, thus a semicolon used there is superfluous. Also, a block consisting of BEGIN END; may be used as a placeholder to indicate no action, even if placed inside another BEGIN / END block.

Python The Python programming language has a pass statement which has no effect when executed and thus serves as a NOP. It is primarily used to ensure correct syntax due to Python's indentation-sensitive syntax; for example the syntax for definition of a class requires an indented block with the class logic, which has to be expressed as pass when it should be empty.

Shell scripting (bash, zsh, etc.) The ':' [colon] command is a shell builtin that has similar effect to a "NOP" (a do-nothing operation). It is not technically an NOP, as it changes the special parameter $? (exit status of last command) to 0. It may be considered a synonym for the shell builtin 'true', and its exit status is true (0).

TeX macro language (ConTeXt, LaTeX, etc.) The TeX typographical system's macro language has the \relax command. It does nothing by itself, but may be used to prevent the immediately preceding command from parsing any subsequent tokens.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with NOP (code)

Start with the simplest possible case. Write down what NOP (code) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to NOP (code) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about NOP (code) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of NOP (code)

In research
NOP (code) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses NOP (code) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
NOP (code) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computing acronyms, Machine code, X86 instructions, so understanding it makes those chapters shorter.
In everyday life
Look for NOP (code) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “NOP (code)” →

How to study NOP (code) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what NOP (code) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain NOP (code) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is NOP (code) in simple terms?

In computer science, a NOP, no-op, or NOOP (pronounced "no op"; short for no operation) is a machine language instruction and its assembly language mnemonic, programming language statement, or computer protocol command that does nothing. Machine language instructions Some computer instruction sets…

Why does NOP (code) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study NOP (code)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on NOP (code).

Tags

  • Computing acronyms
  • Machine code
  • X86 instructions

Keep exploring