摘自:维基百科 NOP (code)

前言

  • 在计算机科学中,NOP、no-op 或 NOOP(发音为“no op”;no operation 的缩写)是一种机器语言指令及其汇编语言助记符、编程语言语句或不执行任何操作的计算机协议命令。
  •  免费的C语言和linuxPDF文档:传送门

机器语言指令

  • 从硬件设计的角度来看,总线的未映射区域通常设计为返回零; 由于 NOP 滑动行为通常是可取的,因此它倾向于使用全零操作码对其进行编码。

代码

  • 一个函数或一系列编程语言语句是一个 NOP 或 null 语句,如果它没有效果。 但在某些上下文中,某些语言的语法可能需要空语句。

在 Ada 中,null 语句用作 NOP。[6] 由于语法禁止控制语句或函数为空,因此必须使用空语句来指定不需要任何操作。 (因此,如果程序员忘记编写语句序列,程序将无法编译。)

C和衍生物
C 中最简单的 NOP 语句是 null 语句,它只是需要语句的上下文中的分号。[译者添加:空语句可能会被优化]:

;

一个空块(复合语句)也是一个 NOP,并且可能更清晰:

{}

 在某些情况下,例如函数体,必须使用块,但块可以是空的。 在 C 中,语句不能为空——简单的语句必须以 ; 结尾。 (分号)而复合语句包含在 {}(大括号)中,它本身不需要后面的分号。 因此,在语法上需要语句的上下文中,可以使用一些这样的空语句。

null 语句本身是无用的,但它可以在更广泛的上下文中使用语法,例如,在循环的上下文中:

while (getchar() != '\n') {}

while (getchar() != '\n')
      ;
while (getchar() != '\n');

[请注意,最后一种形式可能会令人困惑,因此会在某些编译器或编译器选项中生成警告,因为分号通常在行尾括号后表示函数调用指令的结束]。

上面的代码继续调用函数 getchar() 直到它返回一个 \n (换行符)字符,实质上是将标准输入的当前读取位置快进到下一行的开头。

Fortran

在 Fortran 中,CONTINUE 语句用于某些上下文,例如 DO 循环中的最后一个语句,尽管它可以在任何地方使用,并且没有任何功能。 

 JavaScript
JavaScript 语言没有内置的 NOP 语句。 许多实现是可能的:

  1. 使用 ; 空语句 或 {} 空块语句与 C 和派生示例中的方式相同;
  2. 当语法不允许使用前面的方法时,使用 undefined 或 null 表达式作为完整语句(表达式语句)。

在需要功能的情况下,替代方案是:

  1. 使用 Function.prototype() 内置函数,它接受任何参数并返回 undefined;[8]
  2. 使用第三方库中可用的 NOP 函数——见下文;
  3. 定义一个自定义的 NOP 函数,如下例所示(使用 ES6 箭头函数语法):
const noop = () => {};

Python
Python 编程语言有一个 pass 语句,它在执行时没有任何效果,因此用作 NOP。 由于 Python 的缩进敏感语法,它主要用于确保正确的语法; 例如,定义类的语法需要一个带有类逻辑的缩进块,当它应该为空时必须表示为 pass。

NOP 协议命令

  • 许多计算机协议,例如 telnet,都包含一个 NOP 命令,客户端可以发出该命令来请求来自服务器的响应,而无需请求任何其他操作。 这样的命令可用于确保连接仍然有效或服务器有响应。 NOOP 命令是以下协议的一部分(这是部分列表): 

    telnet/FTP/SMTP/X11/POP3/NNTP/finger/IMAP4/BitTorrent

    请注意,与列出的其他协议不同,IMAP4 NOOP 命令具有特定用途——它允许服务器向客户端发送任何挂起的通知。

    虽然大多数 telnet 或 FTP 服务器以“OK”或“+OK”响应 NOOP 命令,但一些程序员已向客户端添加了古怪的响应。 例如,MINIX 的 ftpd 守护进程响应 NOOP 消息:

    200 NOOP to you too!

破解
在破解检查序列号、特定硬件或软件要求、硬件加密狗的存在与否等的软件时,NOP 通常会参与其中。 正在检查的预期值。 因为安全检查例程中的大多数指令都不会被使用,这些指令将被替换为 NOP,从而删除软件的安全功能,而不会改变二进制文件中所有内容的位置。
 

安全漏洞 NOP

操作码可用于形成 NOP 幻灯片,它允许代码在指令指针的确切值不确定时执行(例如,当缓冲区溢出导致堆栈上的函数返回地址被覆盖时)。

下面列出了一些 CPU 架构的 NOP 指令:

CPU architectureInstruction mnemonicBytesOpcodeNotes
Intel x86 CPU familyNOP1; 1–9 for i686 and x86-640x90[2]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 familyNOP10x00
ARM A32NOP40x00000000This stands for andeq r0, r0, r0. The assembly instruction nop will most likely expand to mov r0, r0 which is encoded 0xE1A00000 (little-endian architecture).[3]
ARM A32NOP40x00000000这代表andeq r0, r0, r0。 汇编指令 nop 很可能会扩展为 mov r0, r0,其编码为 0xE1A00000(小端架构)。
ARM T32 (16 bit)NOP20xb000Opcode for ADD SP, #0 - Add zero to the stack pointer (No operation). The assembly instruction nop will most likely expand to mov r8, r8 which is encoded 0x46C0.[4]
ARM T32 (32 bit)NOP40xF3AF 8000
ARM A64 (64 bit)NOP40xD503201F
AVRNOP20x0000one clock cycle

IBM System/360

IBM System/370,

 IBM System/390,

 z/Architecture

UNIVAC Series 90

NOP40x47000000 or 0x470nnnnn or 0x47n0nnnn where "n" is any 4-bit value.The NOP ("No-Op") and NOPR ("No-Op Register") 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.

NOPR20x0700 or 0x070n or 0x07n0 where "n" is any 4-bit value.
SuperHNOP20x0009
MIPSNOP40x00000000Stands for sll r0,r0,0, meaning: Logically shift register 0 zero bits to the left and store the result in register 0
MIPS-XNOP40x60000019(extended opcode for add r0,r0,r0)
MIXNOP1 word± * * * * 0The * bytes are arbitrary, and can be anything from 0 to the maximum byte (required to be in the range 63-99). MIX uses sign-magnitude representation.
MMIXSWYM40xFD******SWYM stands for "Sympathize with your machinery". The * digits can be chosen arbitrarily.
Motorola 68000 familyNOP20x4E71This synchronizes the pipeline and prevents instruction overlap.[1]
Motorola 6809NOP10x12
MOS Technology 65xx (e.g. 6502)NOP10xEANOP 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.
PowerPCNOP40x60000000(extended opcode for ori r0,r0,0)
PIC microcontrollerNOP12 bits0b000000000000MOVW 0,W
RISC-VNOP40x00000013ADDI x0, x0, 0
SPARCNOP40x01000000Stands for sethi 0, %g0 which zeroes the hardwired-to-zero %g0 register[5]
Z80NOP10x00There are some other instructions without any effect (and the same timing): LD A, ALD B, B etc.
PDP-10JFCL 0, (conventional)
JUMP, SETA, SETAI, CAI, TRN, TLN
1 word25500******* (octal)Jump never
Jump never, set nothing, skip never
PDP-11NOP16 bits000240 (octal)Clear none of the condition codes
VAXNOP10x01Delay is dependent on processor type
  •  从硬件设计的角度来看,总线的未映射区域通常设计为返回零; 由于 NOP 滑动行为通常是可取的,因此它倾向于使用全零操作码对其进行编码。

引用:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐