NOP (code)_NOP指令作用及解析
摘自:维基百科NOP (code)前言在计算机科学中,NOP、no-op 或 NOOP(发音为“no op”;no operation 的缩写)是一种机器语言指令及其汇编语言助记符、编程语言语句或不执行任何操作的计算机协议命令。机器语言指令一些计算机指令集包括一条指令,其明确目的是不改变任何程序员可访问的寄存器、状态标志或内存的状态。 通常需要明确定义的时钟周期数来执行。 在其他指令集中,没有明确
摘自:维基百科 NOP (code)
前言
- 在计算机科学中,NOP、no-op 或 NOOP(发音为“no op”;no operation 的缩写)是一种机器语言指令及其汇编语言助记符、编程语言语句或不执行任何操作的计算机协议命令。
- 免费的C语言和linuxPDF文档:传送门
机器语言指令
- 一些计算机指令集包括一条指令,其明确目的是不改变任何程序员可访问的寄存器、状态标志或内存的状态。 通常需要明确定义的时钟周期数来执行。 在其他指令集中,没有明确的 NOP 指令,但汇编语言助记符 NOP 表示作为 NOP 的指令; 例如,在 SPARC 上,[sethi 0,%g0]。
- NOP 不得访问内存,因为这可能会导致内存错误或页面错误。
- NOP 最常用于计时目的,强制内存对齐,防止危险,占用分支延迟槽,使现有指令(例如跳转)无效,作为执行指令的目标,或作为位置 - 持有者在程序开发后期被活动指令替换(或在重组有问题或耗时时替换已删除的指令)。 在某些情况下,NOP 可能会产生轻微的副作用; 例如,在 Motorola 68000 系列处理器上,NOP 操作码会导致流水线同步。
nop指令的作用:[译者添加]
- 从硬件设计的角度来看,总线的未映射区域通常设计为返回零; 由于 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 语句。 许多实现是可能的:
- 使用 ; 空语句 或 {} 空块语句与 C 和派生示例中的方式相同;
- 当语法不允许使用前面的方法时,使用 undefined 或 null 表达式作为完整语句(表达式语句)。
在需要功能的情况下,替代方案是:
- 使用 Function.prototype() 内置函数,它接受任何参数并返回 undefined;[8]
- 使用第三方库中可用的 NOP 函数——见下文;
- 定义一个自定义的 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 architecture | Instruction mnemonic | Bytes | Opcode | Notes |
---|---|---|---|---|
Intel x86 CPU family | NOP | 1; 1–9 for i686 and x86-64 | 0x90[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 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 (little-endian architecture).[3] |
ARM A32 | NOP | 4 | 0x00000000 | 这代表andeq r0, r0, r0。 汇编指令 nop 很可能会扩展为 mov r0, r0,其编码为 0xE1A00000(小端架构)。 |
ARM T32 (16 bit) | NOP | 2 | 0xb000 | Opcode 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) | NOP | 4 | 0xF3AF 8000 | |
ARM A64 (64 bit) | NOP | 4 | 0xD503201F | |
AVR | NOP | 2 | 0x0000 | one clock cycle |
NOP | 4 | 0x47000000 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. | |
NOPR | 2 | 0x0700 or 0x070n or 0x07n0 where "n" is any 4-bit value. | ||
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 | (extended opcode for add r0,r0,r0 ) |
MIX | NOP | 1 word | ± * * * * 0 | The * 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. |
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.[1] |
Motorola 6809 | NOP | 1 | 0x12 | |
MOS Technology 65xx (e.g. 6502) | 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 | (extended opcode for ori r0,r0,0 ) |
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[5] |
Z80 | NOP | 1 | 0x00 | There are some other instructions without any effect (and the same timing): LD A, A , LD B, B etc. |
PDP-10 | JFCL 0, (conventional)JUMP, SETA, SETAI, CAI, TRN, TLN | 1 word | 25500******* (octal) | Jump never Jump never, set nothing, skip never |
PDP-11 | NOP | 16 bits | 000240 (octal) | Clear none of the condition codes |
VAX | NOP | 1 | 0x01 | Delay is dependent on processor type |
- 从硬件设计的角度来看,总线的未映射区域通常设计为返回零; 由于 NOP 滑动行为通常是可取的,因此它倾向于使用全零操作码对其进行编码。
引用:
更多推荐
所有评论(0)