说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C8-IA32/loops.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
8.4.3. Loops in assembly
与 if 语句一样,汇编中的循环也是用跳转指令实现的。不过,循环让指令可以根据对条件求值的结果被重新执行。
Like
ifstatements, 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
sumUpfunction 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 awhileloop in C.
1 | int sumUp(int n) { |
用 -m32 选项编译这段代码,再用 GDB 反汇编,得到下面的汇编代码:
Compiling this code with the
-m32option and disassembling it using GDB yields the following assembly code:
1 | (gdb) disas sumUp |
同样,在这个例子中我们不会明确地画出栈,但鼓励读者自己把栈画出来。我们分几部分来分析这段汇编:
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 | 0x804840b <+0>: push %ebp # save ebp on stack |
回想一下,栈上的位置存放的是函数中的临时变量。为简单起见,我们把 %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 - 0x8astotaland%ebp - 0x4asi. The input parameter tosumUpis located at%ebp0x8.
译注
原书网页此处的 %ebp + 0x8 排版出错,显示成了 %ebp、一个反斜杠和换行后的 0x8;按照前文的约定,第一个参数位于 %ebp+8。
循环的核心
The Heart of the Loop
sumUp 函数中接下来的七条指令构成了循环的核心:
The next seven instructions in the
sumUpfunction represent the heart of the loop:
1 | 0x804841f <+20>: jmp 0x804842b <sumUp+32> # goto <sumUp+32> |
第一条指令是直接跳转到 <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 ofito register%eaxand compareiwith the first parameter to thesumUpfunction (orn). Thecmpinstruction sets the appropriate condition codes in preparation for thejleinstruction at<sumUp+38>.
接着执行 <sumUp+38> 处的 jle 指令。如果 i 小于或等于 n,就走分支,程序跳转到 <sumUp+22> 执行,%eip 被设为 0x8048421。然后依次执行下面的指令:
The
jleinstruction at<sumUp+38>then executes. Ifiis less than or equal ton, the branch is taken and program execution jumps to<sumUp+22>, and%eipis set to 0x8048421. The following instructions then execute in sequence:
mov -0x4(%ebp),%eax把i复制到寄存器%eax。
mov -0x4(%ebp),%eaxcopiesito register%eax.
add %eax,-0x8(%ebp)把i加到total上(total+=i)。
add %eax,-0x8(%ebp)addsitototal(total+=i).
add $0x1,-0x4(%ebp)让i加 1(i+=1)。
add $0x1,-0x4(%ebp)incrementsiby 1 (i+=1).
mov -0x4(%ebp),%eax把i复制到寄存器%eax。
mov -0x4(%ebp),%eaxcopiesito register%eax.
cmp 0x8(%ebp),%eax比较i和n。
cmp 0x8(%ebp),%eaxcomparesiton.
- 如果
i小于或等于n,jle 0x8048421 <sumUp+22>就跳回这串指令的开头。
jle 0x8048421 <sumUp+22>jumps back to the beginning of this instruction sequence ifiis less than or equal ton.
如果在 <sumUp+38> 处没有走分支(即 i 不小于或等于 n),total 被放到返回寄存器中,函数退出。
If the branch is not taken at
<sumUp+38>(i.e.,iis not less than or equal ton),totalis placed in the return register, and the function exits.
表 1 展示了 sumUp 函数的汇编形式和 C 的 goto 形式:
Table 1 shows the assembly and C
gotoforms of thesumUpfunction:
表 1. 把 sumUp 翻译成 C 的 goto 形式
Table 1. Translating sumUp into goto C form.
汇编
Assembly
1 | <sumUp>: |
翻译出的 goto 形式
Translated goto Form
1 | int sumUp(int n) { |
上面的代码也等价于下面这段不含 goto 语句的 C 代码:
The preceding code is also equivalent to the following C code without
gotostatements:
1 | int sumUp(int n) { |
汇编中的 for 循环
for Loops in Assembly
sumUp 函数中的主循环也可以写成 for 循环:
The primary loop in the
sumUpfunction can also be written as aforloop:
1 | int sumUp2(int n) { |
这个版本生成的汇编代码与我们的 while 循环示例完全相同。下面再列出这段汇编代码,并为每一行加上英文注解:
This version yields assembly code identical to our
whileloop example. We repeat the assembly code below and annotate each line with its English translation:
1 | 0x8048438 <+0>: push %ebp # save ebp |
要理解为什么这段代码的 for 循环版本与 while 循环版本生成的汇编完全相同,回想一下,for 循环的表示形式是:
To understand why the
forloop version of this code results in identical assembly to thewhileloop version of the code, recall that theforloop has the following representation:
1 | for ( <initialization>; <boolean expression>; <step> ){ |
它等价于下面的 while 循环表示形式:
and is equivalent to the following
whileloop representation:
1 | <initialization> |
由于每个 for 循环都可以用 while 循环表示,下面两个 C 程序都是前面那段汇编的等价表示:
Since every
forloop can be represented by awhileloop, 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 | int sumUp2(int n) { |
while 循环
While loop
1 | int sumUp(int n){ |