第 93 / 187 篇

8.4.1. 预备知识

说明

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

8.4.1. Preliminaries

条件比较指令

Conditional Comparison Instructions

比较指令执行一次算术运算,目的是引导程序的条件执行。表 1 列出了与条件控制相关的基本指令。

Comparison instructions perform an arithmetic operation for the purpose of guiding the conditional execution of a program. Table 1 lists the basic instructions associated with conditional control.

表 1. 条件控制指令

Table 1. Conditional Control Instructions

指令 含义
cmp R1, R2 比较 R2 与 R1(即计算 R2 - R1)
test R1, R2 计算 R1 & R2
Instruction Translation
cmp R1, R2 Compares R2 with R1 (i.e., evaluates R2 - R1)
test R1, R2 Computes R1 & R2

cmp 指令比较两个寄存器 R2 和 R1 的值,具体来说,就是从 R2 中减去 R1。test 指令执行按位与。常常会看到这样一条指令:

The cmp instruction compares the values of two registers, R2 and R1. Specifically, it subtracts R1 from R2. The test instruction performs bitwise AND. It is common to see an instruction like:

1
test %eax, %eax

在这个例子中,%eax 与自身按位与的结果,只有在 %eax 为零时才为零。换句话说,这是检测值是否为零,等价于:

In this example, the bitwise AND of %eax with itself is zero only when %eax contains zero. In other words, this is a test for a zero value and is equivalent to the following:

1
cmp $0, %eax

与迄今介绍的算术指令不同,cmp 和 test 不修改目标寄存器,而是修改一系列称为条件码标志(condition code flag)的单位值。例如,cmp 会根据 R2 - R1 的结果是正(大于)、负(小于)还是零(等于)来修改条件码标志。回想一下,条件码的值编码了 ALU 中一次运算的相关信息。在 x86 系统上,条件码标志是 FLAGS 寄存器的一部分。

Unlike the arithmetic instructions covered thus far, cmp and test do not modify the destination register. Instead, both instructions modify a series of single-bit values known as condition code flags. For example, cmp will modify condition code flags based on whether the value R2 - R1 results in a positive (greater), negative (less), or zero (equal) value. Recall that condition code values encode information about an operation in the ALU. The condition code flags are part of the FLAGS register on x86 systems.

表 2. 常用的条件码标志

Table 2. Common Condition Code Flags.

标志 含义
ZF 是否等于零(1:是;0:否)
SF 是否为负(1:是;0:否)
OF 是否发生了溢出(1:是;0:否)
CF 是否发生了算术进位(1:是;0:否)
Flag Translation
ZF Is equal to zero (1: yes; 0: no)
SF Is negative (1: yes; 0: no)
OF Overflow has occurred (1:yes; 0: no)
CF Arithmetic carry has occurred (1: yes; 0:no)

表 2 描绘了用于条件码运算的常用标志。回到 cmp R1, R2 指令:

Table 2 depicts the common flags used for condition code operations. Revisiting the cmp R1, R2 instruction:

  • 如果 R1 和 R2 相等,ZF 标志被置为 1。
  • The ZF flag is set to 1 if R1 and R2 are equal.
  • 如果 R2 小于 R1(即 R2 - R1 的结果为负),SF 标志被置为 1。
  • The SF flag is set to 1 if R2 is less than R1 (i.e., R2 - R1 results in a negative value).
  • 如果运算 R2 - R1 导致整数溢出,OF 标志被置为 1(用于有符号比较)。
  • The OF flag is set to 1 if the operation R2 - R1 results in an integer overflow (useful for signed comparisons).
  • 如果运算 R2 - R1 导致进位(借位),CF 标志被置为 1(用于无符号比较)。
  • The CF flag is set to 1 if the operation R2 - R1 results in a carry operation (useful for unsigned comparisons).

SF 和 OF 标志用于有符号整数的比较运算,而 CF 标志用于无符号整数的比较。尽管深入讨论条件码标志超出了本书的范围,但正是 cmp 和 test 对这些标志的设置,使我们接下来要介绍的那组指令(跳转指令)能够正确运作。

The SF and OF flags are used for comparison operations on signed integers, whereas the CF flag is used for comparisons on unsigned integers. Although an in-depth discussion of condition code flags is beyond the scope of this book, the setting of these registers by cmp and test enables the next set of instructions we cover (the jump instructions) to operate correctly.

跳转指令

The Jump Instructions

跳转指令让程序的执行能「跳」到代码中的一个新位置。在我们迄今跟踪过的汇编程序中,%eip 总是指向程序内存中的下一条指令。跳转指令使 %eip 能被设置为一条还没见过的新指令(例如 if 语句的情形),或者一条之前执行过的指令(例如循环的情形)。

A jump instruction enables a program’s execution to “jump” to a new position in the code. In the assembly programs we have traced through thus far, %eip always points to the next instruction in program memory. The jump instructions enable %eip to be set to either a new instruction not yet seen (as in the case of an if statement) or to a previously executed instruction (as in the case of a loop).

直接跳转指令

Direct jump instructions

表 3. 直接跳转指令

Table 3. Direct Jump Instructions

指令 说明
jmp L 跳转到 L 所指定的位置
jmp *addr 跳转到指定的地址
Instruction Description
jmp L Jump to location specified by L
jmp *addr Jump to specified address

表 3 列出了直接跳转指令的集合;L 指一个符号标签(symbolic label),它在程序的目标文件中充当标识符。所有标签都由一些字母和数字后跟一个冒号组成。标签相对于目标文件的作用域可以是局部的或全局的。函数标签通常是全局的,一般由函数名加冒号组成。例如,main:(或 <main>:)用来标记用户定义的 main 函数。按照惯例,作用域为局部的标签以句点开头。在 if 语句或循环中,可能会遇到名为 .L1 这样的局部标签。

Table 3 lists the set of direct jump instructions; L refers to a symbolic label, which serves as an identifier in the program’s object file. All labels consist of some letters and digits followed by a colon. Labels can be local or global to an object file’s scope. Function labels tend to be global and usually consist of the function name and a colon. For example, main: (or <main>:) is used to label a user-defined main function. By convention, labels whose scope are local are preceded by a period. One may encounter a local label with a name like .L1 in the context of an if statement or loop.

所有标签都有一个对应的地址。当 CPU 执行 jmp 指令时,它修改 %eip,使其反映标签 L 所指定的程序地址。编写汇编的程序员也可以用 jmp * 指令指定一个要跳转到的具体地址。有时,局部标签显示为相对于函数起始处的偏移量。因此,地址距离 main 起始处 28 字节的指令可能用标签 <main+28> 表示。

All labels have an associated address. When the CPU executes a jmp instruction, it modifies %eip to reflect the program address specified by label L. A programmer writing assembly can also specify a particular address to jump to using the jmp * instruction. Sometimes, local labels are shown as an offset from the start of a function. Therefore, an instruction whose address is 28 bytes away from the start of main may be represented with the label <main+28>.

例如,指令 jmp 0x8048427 <main+28> 表示跳转到地址 0x8048427,与之关联的标签是 <main+28>,表示它距离 main 函数的起始地址 28 字节。执行这条指令会把 %eip 设为 0x8048427。

For example, the instruction jmp 0x8048427 <main+28> indicates a jump to address 0x8048427, which has the associated label <main+28>, representing that it is 28 bytes away from the starting address of the main function. Executing this instruction sets %eip to 0x8048427.

条件跳转指令

Conditional jump instructions

条件跳转指令的行为取决于 cmp 指令设置的条件码寄存器。表 4 列出了常用的条件跳转指令。每条指令都以字母 j 开头,表示它是一条跳转指令。每条指令的后缀表示跳转的条件。跳转指令的后缀还决定了把数值比较解释为有符号还是无符号。

The behavior of conditional jump instructions depends on the condition code registers set by the cmp instruction. Table 4 lists the set of common conditional jump instructions. Each instruction starts with the letter j denoting that it is a jump instruction. The suffix of each instruction indicates the condition for the jump. The jump instruction suffixes also determine whether to interpret numerical comparisons as signed or unsigned.

表 4. 条件跳转指令;括号中为同义指令

Table 4. Conditional Jump Instructions; Synonyms Shown in Parentheses

有符号比较 无符号比较 说明
je (jz) 相等(==)或为零时跳转
jne (jnz) 不相等(!=)时跳转
js 为负时跳转
jns 非负时跳转
jg (jnle) ja (jnbe) 大于(>)时跳转
jge (jnl) jae (jnb) 大于等于(>=)时跳转
jl (jnge) jb (jnae) 小于(<)时跳转
jle (jng) jbe (jna) 小于等于(<=)时跳转
Signed Comparison Unsigned Comparison Description
je (jz) jump if equal (==) or jump if zero
jne (jnz) jump if not equal (!=)
js jump if negative
jns jump if non-negative
jg (jnle) ja (jnbe) jump if greater (>)
jge (jnl) jae (jnb) jump if greater than or equal (>=)
jl (jnge) jb (jnae) jump if less (<)
jle (jng) jbe (jna) jump if less than or equal (<=)

与其死记这些不同的条件跳转指令,不如把指令后缀「读出来」更有帮助。表 5 列出了跳转指令中常见的字母及其对应的单词。

Instead of memorizing these different conditional jump instructions, it is more helpful to sound out the instruction suffixes. Table 5 lists the letters commonly found in jump instructions and their word correspondence.

表 5. 跳转指令后缀

Table 5. Jump Instruction Suffixes.

字母 单词
j jump(跳转)
n not(不)
e equal(等于)
s signed(有符号,指符号位为负)
g greater(大于,有符号解释)
l less(小于,有符号解释)
a above(高于,无符号解释)
b below(低于,无符号解释)
Letter Word
j jump
n not
e equal
s signed
g greater (signed interpretation)
l less (signed interpretation)
a above (unsigned interpretation)
b below (unsigned interpretation)

这样读出来,就能看出 jg 对应于 jump greater(大于则跳转),它的有符号同义指令 jnl 代表 jump not less(不小于则跳转)。同样,无符号版本 ja 代表 jump above(高于则跳转),它的同义指令 jnbe 代表 jump not below or equal(不低于也不等于则跳转)。

Sounding it out, we can see that jg corresponds to jump greater and that its signed synonym jnl stands for jump not less. Likewise, the unsigned version ja stands for jump above, while its synonym jnbe stands for jump not below or equal.

译注

与第 7 章相同的笔误:按上面的表 4,jg 的同义指令是 jnle(jump not less or equal,不小于等于则跳转);jnl(不小于则跳转)是 jge 的同义指令。

如果把指令读出来,就有助于理解为什么某些同义指令对应于特定的指令。另一件要记住的事是:术语 greater(大于)和 less(小于)指示 CPU 把数值比较解释为有符号值,而 above(高于)和 below(低于)表示数值比较是无符号的。

If you sound out the instructions, it helps to explain why certain synonyms correspond to particular instructions. The other thing to remember is that the terms greater and less instruct the CPU to interpret the numerical comparison as a signed value, whereas above and below indicate that the numerical comparison is unsigned.

goto 语句

The goto Statement

在接下来的几个小节中,我们会考察汇编中的条件语句和循环,并把它们逆向还原成 C。把条件语句和循环的汇编代码翻译回 C 时,理解对应的 C 语言 goto 形式会很有帮助。goto 语句是 C 的一种原语,它强制程序的执行切换到代码中的另一行。与 goto 语句对应的汇编指令是 jmp。

In the following subsections, we look at conditionals and loops in assembly and reverse engineer them back to C. When translating assembly code of conditionals and loops back into C, it is useful to understand the corresponding C language goto forms. The goto statement is a C primitive that forces program execution to switch to another line in the code. The assembly instruction associated with the goto statement is jmp.

goto 语句由 goto 关键字后跟一个 goto 标签(goto label)组成,goto 标签是一种指明执行应当在哪里继续的程序标签。所以,goto done 表示程序的执行应当跳到标记为 done 的那一行。C 中程序标签的其他例子包括第 2 章介绍过的 switch 语句标签。

The goto statement consists of the goto keyword followed by a goto label, a type of program label that indicates where execution should continue. So, goto done means that the program execution should jump to the line marked by label done. Other examples of program labels in C include the switch statement labels previously covered in Chapter 2.

表 6. 一个 C 函数与其对应的 goto 形式的比较

Table 6. Comparison of a C function and its associated goto form.

普通的 C 版本

Regular C version

1
2
3
4
5
6
7
8
9
10
int getSmallest(int x, int y) {
int smallest;
if ( x > y ) { //if (conditional)
smallest = y; //then statement
}
else {
smallest = x; //else statement
}
return smallest;
}

goto 版本

Goto version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getSmallest(int x, int y) {
int smallest;

if (x <= y ) { //if (!conditional)
goto else_statement;
}
smallest = y; //then statement
goto done;

else_statement:
smallest = x; //else statement

done:
return smallest;
}

表 6 描绘了用普通 C 代码编写的函数 getSmallest(),以及它在 C 中对应的 goto 形式。getSmallest() 函数比较两个整数(x 和 y)的值,并把较小的值赋给变量 smallest。

Table 6 depicts a function getSmallest() written in regular C code and its associated goto form in C. The getSmallest() function compares the value of two integers (x and y), and assigns the smaller value to variable smallest.

这个函数的 goto 形式可能显得有违直觉,我们来讨论一下到底发生了什么。条件检查变量 x 是否小于或等于 y。

The goto form of this function may seem counterintuitive, but let’s discuss what exactly is going on. The conditional checks to see whether variable x is less than or equal to y.

  • 如果 x 小于或等于 y,程序把控制转移到标记为 else_statement 的标签处,那里只有一条语句 smallest = x。由于程序是线性执行的,程序接着执行标签 done 下面的代码,返回 smallest 的值(x)。
  • If x is less than or equal to y, the program transfers control to the label marked by else_statement, which contains the single statement smallest = x. Since the program executes linearly, the program continues on to execute the code under the label done, which returns the value of smallest (x).
  • 如果 x 大于 y,smallest 被赋值为 y。接着程序执行语句 goto done,把控制转移到 done 标签处,返回 smallest 的值(y)。
  • If x is greater than y, then smallest is assigned the value y. The program then executes the statement goto done, which transfers control to the done label, which returns the value of smallest (y).

虽然在编程的早期 goto 语句很常用,但在现代代码中使用 goto 语句被认为是不好的做法,因为它降低了代码整体的可读性。事实上,计算机科学家艾兹格·迪杰斯特拉(Edsger Dijkstra)写过一篇抨击使用 goto 语句的著名文章,题为《Go To 语句有害论》(Go To Statement Considered Harmful)1。

Although goto statements were commonly used in the early days of programming, their use in modern code is considered bad practice, because it reduces the overall readability of code. In fact, computer scientist Edsger Dijkstra wrote a famous paper lambasting the use of goto statements called Go To Statement Considered Harmful1.

一般来说,设计良好的 C 程序不使用 goto 语句,程序员也不被鼓励使用它,以免写出难以阅读、调试和维护的代码。不过,理解 C 的 goto 语句很重要,因为 GCC 在把含有条件语句的 C 代码(包括含有 if 语句和循环的代码)翻译成汇编之前,通常会先把它改写成 goto 形式。

In general, well-designed C programs do not use goto statements, and programmers are discouraged from using them to avoid writing code that is difficult to read, debug, and maintain. However, the C goto statement is important to understand, as GCC typically changes C code with conditionals into a goto form prior to translating it to assembly, including code that contains if statements and loops.

接下来的几个小节更详细地介绍 if 语句和循环的汇编表示。

The following subsections cover the assembly representation of if statements and loops in greater detail.

参考文献

References

  1. Edsger Dijkstra. “Go To Statement Considered Harmful”. Communications of the ACM 11(3) pp. 147—​148. 1968.