NOP (code)
In computer science, a NOP, no-op, or NOOP is an assembly language instruction, programming language statement, or computer protocol command that does nothing.
Machine set of directions
Some computer instruction sets include an instruction whose explicit 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 other instruction sets, a NOP can be simulated by executing an instruction having operands that cause the same effect; e.g., on the SPARC processor, the instructionsethi 0, %g0
is the recommended solution.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, or as a place-holder to be replaced by active instructions later on in program development. 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:
CPU architecture | Instruction mnemonic | Bytes | Opcode | Notes |
Intel x86 CPU family | NOP | 1; 1–9 for i686 and x86-64 | 0x90 | 0x90 decodes to xchg eax, eax in all modes except long mode, where the opcode 0x90 still has no effect. The longer encodings are described in Intel's manual. |
Intel 8051 / MCS-51 family | NOP | 1 | 0x00 | |
ARM A32 | NOP | 4 | 0x00000000 | This stands for andeq r0, r0, r0. The assembly instruction nop will most likely expand to mov r0, r0 which is encoded 0xE1A00000. |
ARM T32 | NOP | 2 | 0xb000 | Opcode for ADD SP, #0 - Add zero to the stack pointer. The assembly instruction nop will most likely expand to mov r8, r8 which is encoded 0x46C0. |
ARM T32 | NOP | 4 | 0xf3af 8000 | |
ARM A64 | NOP | 4 | 0xd503201f | |
AVR | NOP | 2 | 0x0000 | one clock cycle |
IBM System/360, IBM System/370, IBM System/390, z/Architecture, UNIVAC Series 90 | NOP | 4 | 0x47000000 or 0x470nnnnn or 0x47n0nnnn where "n" is any 4-bit value. | The NOP and NOPR are a subset of the "Branch on Condition" or "Branch on Condition Register" instructions, respectively; both versions have two options for generating a NO-OP. In the case of both the NOP and NOPR instructions, the first 0 in the second byte is the "mask" value, the condition to test such as equal, not equal, high, low, etc. If the mask is 0, no branch occurs. In the case of the NOPR instruction, the second value in the second byte is the register to branch on. If register 0 is chosen, no branch occurs regardless of the mask value. Thus, if either of the two values in the second byte is 0, the branch will not happen. In the case of the NOP instruction, the second value in the second byte is the "base" register of a combined base register, displacement register and offset address. If the base register is also 0, the branch is not taken regardless of the value of the displacement register or displacement address. |
IBM System/360, IBM System/370, IBM System/390, z/Architecture, UNIVAC Series 90 | NOPR | 2 | 0x0700 or 0x070n or 0x07n0 where "n" is any 4-bit value. | The NOP and NOPR are a subset of the "Branch on Condition" or "Branch on Condition Register" instructions, respectively; both versions have two options for generating a NO-OP. In the case of both the NOP and NOPR instructions, the first 0 in the second byte is the "mask" value, the condition to test such as equal, not equal, high, low, etc. If the mask is 0, no branch occurs. In the case of the NOPR instruction, the second value in the second byte is the register to branch on. If register 0 is chosen, no branch occurs regardless of the mask value. Thus, if either of the two values in the second byte is 0, the branch will not happen. In the case of the NOP instruction, the second value in the second byte is the "base" register of a combined base register, displacement register and offset address. If the base register is also 0, the branch is not taken regardless of the value of the displacement register or displacement address. |
SuperH | NOP | 2 | 0x0009 | |
MIPS | NOP | 4 | 0x00000000 | Stands for sll r0,r0,0 , meaning: Logically shift register 0 zero bits to the left and store the result in register 0 |
MIPS-X | NOP | 4 | 0x60000019 | |
MIX | NOP | 1 word | ± * * * * 0 | The * bytes are arbitrary, and can be anything from 0 to the maximum byte. MIX uses sign-magnitude representation. |
MMIX | SWYM | 4 | 0xfd****** | SWYM stands for "Sympathize with your machinery". The * digits can be chosen arbitrarily. |
Motorola 68000 family | NOP | 2 | 0x4e71 | This synchronizes the pipeline and prevents instruction overlap. |
Motorola 6809 | NOP | 1 | 0x12 | |
MOS Technology 65xx | NOP | 1 | 0xea | NOP consumes two clock cycles. Undefined opcodes in the NMOS versions of the 65xx family were converted to be NOPs of varying instruction lengths and cycle times in the 65C02. |
PowerPC | NOP | 4 | 0x60000000 | |
PIC microcontroller | NOP | 12 bits | 0b000000000000 | MOVW 0,W |
RISC-V | NOP | 4 | 0x00000013 | ADDI x0, x0, 0 |
SPARC | NOP | 4 | 0x01000000 | Stands for sethi 0, %g0 which zeroes the hardwired-to-zero %g0 register |
Z80 | NOP | 1 | 0x00 | There are some other instructions without any effect : LD A, A , LD B, B etc. |
PDP-11 | NOP | 16 bits | 000240 | Clear none of the condition codes |
VAX | NOP | 1 | 0x01 | Delay is dependent on processor type |
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
NOP is sometimes used as a description for the action performed by a function or a sequence of programming language statements if the function or code has no effect. A common compiler optimization is the detection and removal of this kind of code. Such code may be required by the grammar of the programming language, which does not allow a blank.Ada
In Ada, thenull
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.C and derivatives
The second line below is an example of a single C statement that behaves like a NOP. In practice, most compilers will not generate code for this statement:int i = 0;
i+1;
This statement performs an addition and discards the result. Indeed, any statement without side effects can be removed, as the result of the computation is discarded.
The simplest possible statement in C that behaves like a NOP is the so-called null statement, which is just a semi-colon in a context requiring a statement.
;
Alternatively, an empty block may be used, and may be more legible:
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 ; while compound statements are enclosed in , 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:
while
alternatively,
while
;
or more tersely:
while ;
.
The above code continues calling the function getchar until it returns a \n character, essentially fast-forwarding the current reading location of standard input to the beginning of next line.
Fortran
In Fortran, theCONTINUE
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 thenull
expression as a complete statement when the previous methods are not allowed by the syntax.
- Use the
Function.prototype
built-in function, that accepts any arguments and returnsundefined
; - Use a NOP function available in a third-party library —see below;
- Define a custom NOP function, as in the following example :
AngularJS
The AngularJS framework provides function that performs no operations.jQuery
The jQuery library provides a functionjQuery.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 apass
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 ':
' command is a shell builtin that has similar effect to a "NOP". It is not technically an NOP, as it changes the special parameter $? to 0. It may be considered a synonym for the shell builtin 'true', and its exit status is true.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.NOP protocol commands
Many computer protocols, such as telnet, include a NOP command that a client can issue to request a response from the server without requesting any other actions. Such a command can be used to ensure the connection is still alive or that the server is responsive. A NOOP command is part of the following protocols :- telnet
- FTP
- SMTP
- X11
- POP3
- NNTP
- finger
- IMAP4
- BitTorrent
While most telnet or FTP servers respond to a NOOP command with "OK" or "+OK", some programmers have added quirky responses to the client. For example, the
ftpd
daemon of MINIX responds to NOOP with the message:200 NOOP to you too!