第 95 / 187 篇

8.4.3. 汇编中的循环

说明

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

8.4.3. Loops in assembly

与 if 语句一样,汇编中的循环也是用跳转指令实现的。不过,循环让指令可以根据对条件求值的结果被重新执行。

Like if statements, loops in assembly are also implemented using jump instructions. However, loops enable instructions to be revisited based on the result of an evaluated condition.

下例中的 sumUp 函数把从 1 到用户给定整数之间的所有正整数加起来。这段代码故意写得不够优化,以便演示 C 中的 while 循环。

The sumUp function shown in the following example sums up all the positive integers from 1 to a user-defined integer. This code is intentionally written suboptimally to illustrate a while loop in C.

1
2
3
4
5
6
7
8
9
10
11
int sumUp(int n) {
//initialize total and i
int total = 0;
int i = 1;

while (i <= n) { //while i is less than or equal to n
total += i; //add i to total
i+=1; //increment i by 1
}
return total;
}

用 -m32 选项编译这段代码,再用 GDB 反汇编,得到下面的汇编代码:

Compiling this code with the -m32 option and disassembling it using GDB yields the following assembly code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(gdb) disas sumUp
Dump of assembler code for function sumUp:
0x804840b <+0>: push %ebp
0x804840c <+1>: mov %esp,%ebp
0x804840e <+3>: sub $0x10,%esp
0x8048411 <+6>: movl $0x0,-0x8(%ebp)
0x8048418 <+13>: movl $0x1,-0x4(%ebp)
0x804841f <+20>: jmp 0x804842b <sumUp+32>
0x8048421 <+22>: mov -0x4(%ebp),%eax
0x8048424 <+25>: add %eax,-0x8(%ebp)
0x8048427 <+28>: add $0x1,-0x4(%ebp)
0x804842b <+32>: mov -0x4(%ebp),%eax
0x804842e <+35>: cmp 0x8(%ebp),%eax
0x8048431 <+38>: jle 0x8048421 <sumUp+22>
0x8048433 <+40>: mov -0x8(%ebp),%eax
0x8048436 <+43>: leave
0x8048437 <+44>: ret

同样,在这个例子中我们不会明确地画出栈,但鼓励读者自己把栈画出来。我们分几部分来分析这段汇编:

Again, we will not draw out the stack explicitly in this example. However, we encourage readers to draw the stack out themselves. Let’s analyze this assembly segment in parts:

前五条指令

The First Five Instructions

这个函数的前五条指令为函数的执行设置好栈:

The first five instructions of this function set the stack up for function execution:

1
2
3
4
5
0x804840b <+0>:   push   %ebp                 # save ebp on stack
0x804840c <+1>: mov %esp,%ebp # update ebp (new stack frame)
0x804840e <+3>: sub $0x10,%esp # add 16 bytes to stack frame
0x8048411 <+6>: movl $0x0,-0x8(%ebp) # place 0 at ebp-0x8 (total)
0x8048418 <+13>: movl $0x1,-0x4(%ebp) # place 1 at ebp-0x4 (i)

回想一下,栈上的位置存放的是函数中的临时变量。为简单起见,我们把 %ebp - 0x8 标记的位置称为 total,把 %ebp - 0x4 称为 i。sumUp 的输入参数位于 %ebp + 0x8。

Recall that stack locations store temporary variables in a function. For simplicity we will refer to the location marked by %ebp - 0x8 as total and %ebp - 0x4 as i. The input parameter to sumUp is located at %ebp
0x8.

译注

原书网页此处的 %ebp + 0x8 排版出错,显示成了 %ebp、一个反斜杠和换行后的 0x8;按照前文的约定,第一个参数位于 %ebp+8。

循环的核心

The Heart of the Loop

sumUp 函数中接下来的七条指令构成了循环的核心:

The next seven instructions in the sumUp function represent the heart of the loop:

1
2
3
4
5
6
7
0x804841f <+20>:  jmp    0x804842b <sumUp+32>  # goto <sumUp+32>
0x8048421 <+22>: mov -0x4(%ebp),%eax # copy i to eax
0x8048424 <+25>: add %eax,-0x8(%ebp) # add i to total (total+=i)
0x8048427 <+28>: add $0x1,-0x4(%ebp) # add 1 to i (i+=1)
0x804842b <+32>: mov -0x4(%ebp),%eax # copy i to eax
0x804842e <+35>: cmp 0x8(%ebp),%eax # compare i with n
0x8048431 <+38>: jle 0x8048421 <sumUp+22> # if (i <= n) goto <sumUp+22>

第一条指令是直接跳转到 <sumUp+32>,它把指令指针(%eip)设为地址 0x804842b。

The first instruction is a direct jump to <sumUp+32>, which sets the instruction pointer (%eip) to address 0x804842b.

接下来执行的指令(<sumUp+32> 和 <sumUp+35>)把 i 的值复制到寄存器 %eax,并把 i 与 sumUp 函数的第一个参数(即 n)进行比较。cmp 指令设置相应的条件码,为 <sumUp+38> 处的 jle 指令做准备。

The next instructions that execute (<sumUp+32> and <sumUp+35>) copy the value of i to register %eax and compare i with the first parameter to the sumUp function (or n). The cmp instruction sets the appropriate condition codes in preparation for the jle instruction at <sumUp+38>.

接着执行 <sumUp+38> 处的 jle 指令。如果 i 小于或等于 n,就走分支,程序跳转到 <sumUp+22> 执行,%eip 被设为 0x8048421。然后依次执行下面的指令:

The jle instruction at <sumUp+38> then executes. If i is less than or equal to n, the branch is taken and program execution jumps to <sumUp+22>, and %eip is set to 0x8048421. The following instructions then execute in sequence:

  • mov -0x4(%ebp),%eax 把 i 复制到寄存器 %eax。
  • mov -0x4(%ebp),%eax copies i to register %eax.
  • add %eax,-0x8(%ebp) 把 i 加到 total 上(total+=i)。
  • add %eax,-0x8(%ebp) adds i to total (total+=i).
  • add $0x1,-0x4(%ebp) 让 i 加 1(i+=1)。
  • add $0x1,-0x4(%ebp) increments i by 1 (i+=1).
  • mov -0x4(%ebp),%eax 把 i 复制到寄存器 %eax。
  • mov -0x4(%ebp),%eax copies i to register %eax.
  • cmp 0x8(%ebp),%eax 比较 i 和 n。
  • cmp 0x8(%ebp),%eax compares i to n.
  • 如果 i 小于或等于 n,jle 0x8048421 <sumUp+22> 就跳回这串指令的开头。
  • jle 0x8048421 <sumUp+22> jumps back to the beginning of this instruction sequence if i is less than or equal to n.

如果在 <sumUp+38> 处没有走分支(即 i 不小于或等于 n),total 被放到返回寄存器中,函数退出。

If the branch is not taken at <sumUp+38> (i.e., i is not less than or equal to n), total is placed in the return register, and the function exits.

表 1 展示了 sumUp 函数的汇编形式和 C 的 goto 形式:

Table 1 shows the assembly and C goto forms of the sumUp function:

表 1. 把 sumUp 翻译成 C 的 goto 形式

Table 1. Translating sumUp into goto C form.

汇编

Assembly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<sumUp>:
<+0>: push %ebp
<+1>: mov %esp,%ebp
<+3>: sub $0x10,%esp
<+6>: movl $0x0,-0x8(%ebp)
<+13>: movl $0x1,-0x4(%ebp)
<+20>: jmp <sumUp+32>
<+22>: mov -0x4(%ebp),%eax
<+25>: add %eax,-0x8(%ebp)
<+28>: addl $0x1,-0x4(%ebp)
<+32>: mov -0x4(%ebp),%eax
<+35>: cmp 0x8(%ebp),%eax
<+38>: jle <sumUp+22>
<+40>: mov -0x8(%ebp),%eax
<+43>: leave
<+44>: ret

翻译出的 goto 形式

Translated goto Form

1
2
3
4
5
6
7
8
9
10
11
12
13
int sumUp(int n) {
int total = 0;
int i = 1;
goto start;
body:
total += i;
i += 1;
start:
if (i <= n) {
goto body;
}
return total;
}

上面的代码也等价于下面这段不含 goto 语句的 C 代码:

The preceding code is also equivalent to the following C code without goto statements:

1
2
3
4
5
6
7
8
9
int sumUp(int n) {
int total = 0;
int i = 1;
while (i <= n) {
total += i;
i += 1;
}
return total;
}

汇编中的 for 循环

for Loops in Assembly

sumUp 函数中的主循环也可以写成 for 循环:

The primary loop in the sumUp function can also be written as a for loop:

1
2
3
4
5
6
7
8
int sumUp2(int n) {
int total = 0; //initialize total to 0
int i;
for (i = 1; i <= n; i++) { //initialize i to 1, increment by 1 while i<=n
total += i; //updates total by i
}
return total;
}

这个版本生成的汇编代码与我们的 while 循环示例完全相同。下面再列出这段汇编代码,并为每一行加上英文注解:

This version yields assembly code identical to our while loop example. We repeat the assembly code below and annotate each line with its English translation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0x8048438 <+0>:  push   %ebp                  # save ebp
0x8048439 <+1>: mov %esp,%ebp # update ebp (new stack frame)
0x804843b <+3>: sub $0x10,%esp # add 16 bytes to stack frame
0x804843e <+6>: movl $0x0,-0x8(%ebp) # place 0 at ebp-0x8 (total)
0x8048445 <+13>: movl $0x1,-0x4(%ebp) # place 1 at ebp-0x4 (i)
0x804844c <+20>: jmp 0x8048458 <sumUp2+32> # goto <sumUp2+32>
0x804844e <+22>: mov -0x4(%ebp),%eax # copy i to %eax
0x8048451 <+25>: add %eax,-0x8(%ebp) # add %eax to total (total+=i)
0x8048454 <+28>: addl $0x1,-0x4(%ebp) # add 1 to i (i+=1)
0x8048458 <+32>: mov -0x4(%ebp),%eax # copy i to %eax
0x804845b <+35>: cmp 0x8(%ebp),%eax # compare i with n
0x804845e <+38>: jle 0x804844e <sumUp2+22> # if (i <= n) goto <sumUp2+22>
0x8048460 <+40>: mov -0x8(%ebp),%eax # copy total to %eax
0x8048463 <+43>: leave # prepare to leave the function
0x8048464 <+44>: ret # return total

要理解为什么这段代码的 for 循环版本与 while 循环版本生成的汇编完全相同,回想一下,for 循环的表示形式是:

To understand why the for loop version of this code results in identical assembly to the while loop version of the code, recall that the for loop has the following representation:

1
2
3
for ( <initialization>; <boolean expression>; <step> ){
<body>
}

它等价于下面的 while 循环表示形式:

and is equivalent to the following while loop representation:

1
2
3
4
5
<initialization>
while (<boolean expression>) {
<body>
<step>
}

由于每个 for 循环都可以用 while 循环表示,下面两个 C 程序都是前面那段汇编的等价表示:

Since every for loop can be represented by a while loop, the following two C programs are equivalent representations for the previous assembly:

表 2. 编写 sumUp 函数的两种等价方式

Table 2. Equivalent ways to write the sumUp function.

for 循环

For loop

1
2
3
4
5
6
7
8
int sumUp2(int n) {
int total = 0;
int i = 1;
for (i; i <= n; i++) {
total += i;
}
return total;
}

while 循环

While loop

1
2
3
4
5
6
7
8
9
int sumUp(int n){
int total = 0;
int i = 1;
while (i <= n) {
total += i;
i += 1;
}
return total;
}