说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C9-ARM64/structs.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
9.9. structs in Assembly
结构体是在 C 中创建数据类型集合的另一种方式。与数组不同,结构体可以把不同的数据类型组合在一起。C 像存放一维数组那样存放 struct,其数据元素(字段)连续存放。
A
structis another way to create a collection of data types in C. Unlike arrays, they enable different data types to be grouped together. C stores astructlike a single-dimension array, where the data elements (fields) are stored contiguously.
我们再来看第 1 章中的 struct studentT:
Let’s revisit
struct studentTfrom Chapter 1:
1 | struct studentT { |
图 1 展示了 student 在内存中的布局。每个 ai 表示内存中的一个偏移。
Figure 1 shows how the
studentis laid out in memory. Each ai denotes an offset in memory.

图 1. struct studentT 的内存布局
Figure 1. The memory layout of a struct studentT.
各字段按照声明的顺序在内存中一个挨一个地连续存放。在图 1 中,age 字段被分配在紧跟 name 字段之后的内存位置(字节偏移 a64),后面依次是 grad_yr(字节偏移 a68)和 gpa(字节偏移 a72)字段。这种组织方式使访问字段既节省内存又高效。
Each field is stored contiguously next to one another in memory in the order in which they are declared. In Figure 1, the
agefield is allocated at the memory location directly after thenamefield (at byte offset a64) and is followed by thegrad_yr(byte offset a68) andgpa(byte offset a72) fields. This organization enables memory-efficient access to the fields.
为了理解编译器如何生成处理 struct 的汇编代码,考虑函数 initStudent:
To understand how the compiler generates assembly code to work with a
struct, consider the functioninitStudent:
1 | void initStudent(struct studentT *s, char *nm, int ag, int gr, float g) { |
initStudent 函数以一个 struct studentT 的基地址作为第一个参数,以各字段想要设置的值作为其余参数。下面的清单给出了这个函数的汇编:
The
initStudentfunction uses the base address of astruct studentTas its first parameter, and the desired values for each field as its remaining parameters. The following listing depicts this function in assembly:
1 | Dump of assembler code for function initStudent: |
留意每个字段的字节偏移,是理解这段代码的关键。下面是几点需要记住的事情。
Being mindful of the byte offsets of each field is key to understanding this code. Here are a few things to keep in mind.
strncpy调用以s的name字段的基地址、数组nm的地址和一个长度说明作为它的三个实参。回想一下,由于name是struct studentT中的第一个字段,s的地址与s→name的地址是同一个。
- The
strncpycall takes the base address of thenamefield ofs, the address of arraynm, and a length specifier as its three arguments. Recall that sincenameis the first field instruct studentT, the address ofsis synonymous with the address ofs→name.
译注 · 清单中的几处笔误
- 与第 7、8 章相同,原书网页把
->排版成了箭头「→」,代码中要写作s->name。 - 上面完整清单第一行的注释「store x29, x30 at sp, sp+4」应为
sp和sp+8:x29、x30都是 8 字节寄存器(最后一条ldp的注释同理)。 - 清单从
<+36>起地址与偏移对不上:initStudent从 0x7f4 开始,<+36>应在 0x818,清单却写成了重复的 0x814,此后各行的地址都差 4;末尾几行的偏移(<+80>、<+84>、<+88>)也跳了号。这应是手工编辑清单时造成的,不影响理解各条指令的作用。
1 | 0x7fc <+8>: str x0, [x29, #40] // store s at x29 + 40 |
- 上面的代码片段中有一个之前没有讨论过的寄存器(
s0)。s0寄存器是专门用于浮点值的寄存器的一个例子。
- The above code snippet contains an undiscussed register (
s0). Thes0register is an example of a register reserved for floating point values.
译注
AArch64 有 32 个 128 位的浮点/SIMD 寄存器 v0–v31,按访问宽度写作 s0(32 位单精度)、d0(64 位双精度)等。浮点参数依次通过 s0/d0–s7/d7 传递,与整数参数的 x0–x7 各自独立编号,所以第五个参数 g(float)用的是第一个浮点寄存器 s0。
- 下一部分(指令
<initStudent+44>到<initStudent+52>)把参数gr的值放到距s起始处偏移 68 的位置。回顾一下图 1 中结构体的内存布局可以看出,这个地址对应于s→grad_yr。
- The next part (instructions
<initStudent+44>thru<initStudent+52>) places the value of thegrparameter at an offset of 68 from the start ofs. Revisiting the memory layout of the struct in Figure 1 shows that this address corresponds tos→grad_yr.
1 | 0x81c <+44>: ldr x0, [x29, #40] // x0 = s |
- 下一部分(指令
<initStudent+56>到<initStudent+64>)把参数ag复制到s→age字段,它位于距s的地址偏移 64 字节处。
- The next section (instructions
<initStudent+56>thru<initStudent+64>) copies theagparameter to thes→agefield, which is located at an offset of 64 bytes from the address ofs.
1 | 0x828 <+56>: ldr x0, [x29, #40] // x0 = s |
最后,参数 g 的值被复制到 s→gpa 字段(字节偏移 72)。注意这里使用了 s0 寄存器,因为位置 x29 + 20 处存放的数据是单精度浮点数:
Lastly, the
gparameter value is copied to thes→gpafield (byte offset 72). Notice the use of thes0register since the data contained at locationx29 + 20is single-precision floating point:
1 | 0x834 <+68>: ldr x0, [x29, #40] // x0 = s |
9.9.1. 数据对齐与结构体
9.9.1. Data Alignment and structs
考虑下面这个修改过的 studentT 声明:
Consider the following modified declaration of
studentT:
1 | struct studentTM { |
name 字段的大小被改成了 63 字节,而不是原来的 64 字节。想想这会如何影响 struct 在内存中的布局。你可能会想当然地把它想象成图 2 那样。
The size of the
namefield is modified to be 63 bytes, instead of the original 64 bytes. Consider how this affects the way thestructis laid out in memory. It may be tempting to visualize it as in Figure 2.

图 2. 更新后的 struct studentTM 的一种错误内存布局。注意结构体的「name」字段从 64 字节减少到了 63 字节。
Figure 2. An incorrect memory layout for the updated struct studentTM. Note that the “name” field is reduced from 64 to 63 bytes.
在这种描绘中,age 字段出现在紧跟 name 字段之后的那个字节。但这是错误的。图 3 描绘了实际的内存布局。
In this depiction, the
agefield occurs in the byte immediately following thenamefield. But this is incorrect. Figure 3 depicts the actual layout in memory.

图 3. 更新后的 struct studentTM 的正确内存布局。字节 a63 是编译器为满足内存对齐约束而添加的,它不对应任何字段。
Figure 3. The correct memory layout for the updated struct studentTM. Byte a63 is added by the compiler to satisfy memory alignment constraints, but it doesn’t correspond to any of the fields.
A64 的对齐策略要求:四字节的数据类型(例如 int)位于四的倍数的地址上,而 64 位的数据类型(long、double 和指针数据)位于八的倍数的地址上。对于 struct,编译器会在字段之间添加空字节作为「填充」(padding),以确保每个字段都满足其对齐要求。例如,在上面代码片段声明的 struct 中,编译器在字节 a63 处添加了一个字节的填充,确保 age 字段从一个四的倍数的地址开始。在内存中正确对齐的值可以在单次操作中被读取或写入,从而提高效率。
A64’s alignment policy requires that four-byte data types (e.g.,
int) reside at addresses that are a multiple of four, whereas 64-bit data types (long,double, and pointer data) reside at addresses that are a multiple of eight. For astruct, the compiler adds empty bytes as “padding” between fields to ensure that each field satisfies its alignment requirements. For example, in thestructdeclared in the previous code snippet, the compiler adds a byte of padding at byte a63 to ensure that theagefield starts at an address that is at a multiple of four. Values aligned properly in memory can be read or written in a single operation, enabling greater efficiency.
考虑把 struct 定义成下面这样时会发生什么:
Consider what happens when the
structis defined as the following:
1 | struct studentTM { |
把 name 数组移到最后,就把那个填充字节移到了结构体的末尾,确保 age、grad_yr 和 gpa 都是四字节对齐的。
Moving the
namearray to the end moves the byte of padding to the end of the struct, ensuring thatage,grad_yr, andgpaare four-byte aligned.