第 76 / 187 篇

7.3. 算术指令

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C7-x86_64/arithmetic.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

7.3. Arithmetic Instructions

x86 ISA 实现了若干条与 ALU 执行的算术运算相对应的指令。表 1 列出了阅读汇编时可能遇到的几条算术指令。

The x86 ISA implements several instructions that correspond to arithmetic operations performed by the ALU. Table 1 lists several arithmetic instructions that one may encounter when reading assembly.

表 1. 常用算术指令

Table 1. Common Arithmetic Instructions.

指令 含义
add S, D S + D → D
sub S, D D - S → D
inc D D + 1 → D
dec D D - 1 → D
neg D -D → D
imul S, D S × D → D
idiv S %rax / S:商 → %rax,余数 → %rdx
Instruction Translation
add S, D S + D → D
sub S, D D - S → D
inc D D + 1 → D
dec D D - 1 → D
neg D -D → D
imul S, D S × D → D
idiv S %rax / S: quotient → %rax, remainder → %rdx

add 和 sub 指令对应于加法和减法,各自接收两个操作数。接下来的三项是单寄存器指令,分别对应 C 中的自增(x++)、自减(x--)和取负(-x)运算。乘法指令对两个操作数进行运算,并把乘积放到目标中。如果乘积需要超过 64 位来表示,结果会被截断为 64 位。

The add and sub instructions correspond to addition and subtraction and take two operands each. The next three entries show the single-register instructions for the increment (x++), decrement (x--), and negation (-x) operations in C. The multiplication instruction operates on two operands and places the product in the destination. If the product requires more than 64 bits to represent, the value is truncated to 64 bits.

除法指令的工作方式略有不同。在执行 idiv 指令之前,假定寄存器 %rax 中存放着被除数。对操作数 S 执行 idiv,会用 S 去除 %rax 的内容,把商放到寄存器 %rax 中,余数放到寄存器 %rdx 中。

The division instruction works a little differently. Prior to the execution of the idiv instruction, it is assumed that register %rax contains the dividend. Calling idiv on operand S divides the contents of %rax by S and places the quotient in register %rax and the remainder in register %rdx.

译注

更准确地说,64 位的 idiv 把 %rdx 和 %rax 拼成的 128 位数(%rdx 是高 64 位)当作被除数。所以在 idiv 之前,编译器通常会先用一条 cqto(Intel 语法叫 cqo)指令把 %rax 符号扩展到 %rdx。读汇编时看到紧挨着 idiv 的 cqto/cltd,就是这个原因。

7.3.1. 移位指令

7.3.1. Bit Shifting Instructions

移位指令让编译器能执行移位运算。乘法和除法指令通常需要很长时间才能执行完。对于是 2 的幂的乘数和除数,移位为编译器提供了一条捷径。例如,要计算 77 * 4,大多数编译器会把这个运算翻译成 77 << 2,以避免使用 imul 指令。同样,要计算 77 / 4,编译器通常会把这个运算翻译成 77 >> 2,以避免使用 idiv 指令。

Bit shifting instructions enable the compiler to perform bit shifting operations. Multiplication and division instructions typically take a long time to execute. Bit shifting offers the compiler a shortcut for multiplicands and divisors that are powers of 2. For example, to compute 77 * 4, most compilers will translate this operation to 77 << 2 to avoid the use of an imul instruction. Likewise, to compute 77 / 4, a compiler typically translates this operation to 77 >> 2 to avoid using the idiv instruction.

记住,左移和右移会根据目标是算术(有符号)移位还是逻辑(无符号)移位,被翻译成不同的指令。

Keep in mind that left and right bit shift translate to different instructions based on whether the goal is an arithmetic (signed) or logical (unsigned) shift.

表 2. 移位指令

Table 2. Bit Shift Instructions

指令 含义 算术还是逻辑?
sal v, D D << v → D 算术
shl v, D D << v → D 逻辑
sar v, D D >> v → D 算术
shr v, D D >> v → D 逻辑
Instruction Translation Arithmetic or Logical?
sal v, D D << v → D arithmetic
shl v, D D << v → D logical
sar v, D D >> v → D arithmetic
shr v, D D >> v → D logical

每条移位指令接收两个操作数,一个通常是寄存器(用 D 表示),另一个是移位量(v)。在 64 位系统上,移位量编码为单个字节(因为移位超过 63 位没有意义)。移位量 v 必须是一个常数,或者存放在寄存器 %cl 中。

Each shift instruction takes two operands, one which is usually a register (denoted by D) and the other which is a shift value (v). On 64-bit systems, the shift value is encoded as a single byte (since it doesn’t make sense to shift past 63). The shift value v must either be a constant or stored in register %cl.

原书注 · 不同版本的指令有助于在汇编层面区分类型(Different Versions of Instructions Help Distinguish Types at an Assembly Level)

在汇编层面,没有类型的概念。不过,回想一下,编译器会根据类型使用组成部分寄存器。同样,回想一下,右移的行为取决于值是有符号还是无符号的。在汇编层面,编译器用不同的指令来区分逻辑移位和算术移位!

At the assembly level, there is no notion of types. However, recall that the compiler will use component registers based on types. Similarly, recall that shift right works differently depending on whether or not the value is signed or unsigned. At the assembly level, the compiler uses separate instructions to distinguish between logical and arithmetic shifts!

7.3.2. 位运算指令

7.3.2. Bitwise Instructions

位运算指令让编译器能对数据执行位运算。编译器使用位运算的一种方式是进行某些优化。例如,编译器可能会选择用运算 77 & 3 来实现 77 mod 4,而不用代价更高的 idiv 指令。

Bitwise instructions enable the compiler to perform bitwise operations on data. One way the compiler uses bitwise operations is for certain optimizations. For example, a compiler may choose to implement 77 mod 4 with the operation 77 & 3 in lieu of the more expensive idiv instruction.

表 3 列出了常用的位运算指令。

Table 3 lists common bitwise instructions.

表 3. 位运算

Table 3. Bitwise Operations

指令 含义
and S, D S & D → D
or S, D S `
xor S, D S ^ D → D
not D ~D → D
Instruction Translation
and S, D S & D → D
or S, D S `
xor S, D S ^ D → D
not D ~D → D

记住,按位取反 not 与取负(neg)不同。not 指令只翻转各位,不加 1。小心不要混淆这两条指令。

Remember that bitwise not is distinct from negation (neg). The not instruction flips the bits but does not add 1. Be careful not to confuse these two instructions.

原书警示 · 只在 C 代码确实需要时才使用位运算!(Use bitwise operations only when needed in your C code!)

读完本节,你可能很想把 C 代码中常见的算术运算替换成移位和其他位运算。我们不建议这样做。大多数现代编译器都足够聪明,会在合适的时候用位运算替换简单的算术运算,程序员不必亲自去做。一般来说,程序员应当尽可能把代码的可读性放在首位,避免过早优化。

After reading this section, it may be tempting to replace common arithmetic operations in your C code with bitwise shifts and other operations. This is not recommended. Most modern compilers are smart enough to replace simple arithmetic operations with bitwise operations when it makes sense, making it unnecessary for the programmer to do so. As a general rule, programmers should prioritize code readability whenever possible and avoid premature optimization.

7.3.3. 加载有效地址指令

7.3.3. The Load Effective Address Instruction

lea 跟它有什么关系(有什么关系)?

What’s lea got to do (got to do) with it?

lea 不就是加载一个有效地址吗?

What’s lea, but an effective address loading?

~向 Tina Turner 致歉

~With apologies to Tina Turner

译注

这两句套用了 Tina Turner 的名曲《What’s Love Got to Do with It》(爱情与它何干),把 love 换成了读音相近的 lea。

最后我们来看加载有效地址(load effective address)指令,即 lea 指令,它可能是最让学生头疼的算术指令。传统上,它被用作快速计算某个内存位置地址的方法。lea 指令使用的操作数结构与我们迄今见过的相同,但它不进行内存查找。无论操作数中的数据是什么类型(无论是常数值还是地址),lea 都只是执行算术运算。

We finally come to the load effective address or lea instruction, which is probably the arithmetic instruction that causes students the most consternation. It is traditionally used as a fast way to compute the address of a location in memory. The lea instruction operates on the same operand structure that we’ve seen thus far but does not include a memory lookup. Regardless of the type of data contained in the operand (whether it be a constant value or an address), lea simply performs arithmetic.

例如,假设寄存器 %rax 中存放常数值 0x5,寄存器 %rdx 中存放常数值 0x4,寄存器 %rcx 中存放值 0x808(它恰好是一个地址)。表 4 描绘了一些 lea 操作示例、它们的含义以及对应的值。

For example, suppose register %rax contains the constant value 0x5, register %rdx contains the constant value 0x4, and register %rcx contains the value 0x808 (which happens to be an address). Table 4 depicts some example lea operations, their translations, and corresponding values.

表 4. lea 操作示例

Table 4. Example lea Operations

指令 含义 值
lea 8(%rax), %rax 8 + %rax → %rax 13 → %rax
lea (%rax, %rdx), %rax %rax + %rdx → %rax 9 → %rax
lea (,%rax,4), %rax %rax × 4 → %rax 20 → %rax
lea -0x8(%rcx), %rax %rcx - 8 → %rax 0x800 → %rax
lea -0x4(%rcx, %rdx, 2), %rax %rcx + %rdx × 2 - 4 → %rax 0x80c → %rax
Instruction Translation Value
lea 8(%rax), %rax 8 + %rax → %rax 13 → %rax
lea (%rax, %rdx), %rax %rax + %rdx → %rax 9 → %rax
lea (,%rax,4), %rax %rax × 4 → %rax 20 → %rax
lea -0x8(%rcx), %rax %rcx - 8 → %rax 0x800 → %rax
lea -0x4(%rcx, %rdx, 2), %rax %rcx + %rdx × 2 - 4 → %rax 0x80c → %rax

在所有情况下,lea 指令都是对源 S 所指定的操作数执行算术运算,并把结果放到目标操作数 D 中。mov 指令与 lea 指令完全相同,只是如果源操作数是内存形式,mov 指令必须把源操作数的内容当作内存位置来处理。与之不同,lea 执行同样(有时很复杂)的操作数算术运算,但不进行内存查找,这使得编译器能巧妙地用 lea 来替代某些类型的算术运算。

In all cases, the lea instruction performs arithmetic on the operand specified by the source S and places the result in the destination operand D. The mov instruction is identical to the lea instruction except that the mov instruction is required to treat the contents in the source operand as a memory location if it is in a memory form. In contrast, lea performs the same (sometimes complicated) operand arithmetic without the memory lookup, enabling the compiler to cleverly use lea as a substitution for some types of arithmetic.