IA-32 x86 数据对齐

IA-32 x86 data alignment

我能找到的一切都告诉我我应该对齐,因为它可以更有效率,但我找不到我应该在 IA-32 上这样做的硬件问题。这是因为地址总线需要被 4 整除的地址吗?或者因为 RAM 只能提供对齐的数据而不会损失性能?或者?

http://www-inst.eecs.berkeley.edu/~cs164/sp05/ia32-refs/ia32-chapter-two.pdf

On the ia32, there is no requirement for data alignment. A program will work correctly with four-byte integers regardless of where they are located. For example, a four byte integer could be located at addresses 1,2,3,4. However, the machine executes much more efficiently if, for example, four byte integers are on a four byte boundary, so a better choice of starting address for a four byte integer is an address that is a multiple of 4.

这是一个与硬件相关的问题。通常RAM模块连接如下:

如果发生对齐访问,则模块 1、2、3 和 4 同时 selected,因此可以在一个内存读取周期内读取 32 位。

如果发生未对齐的访问,比如说少了 2 个字节,那么模块 1、2、3 和 4 在第一个读取周期 selected,其中只有高 2 个字节(低 2 个字节我们正在获取的数据)被读取(模块 3 和 4);在下一个读取周期模块 5、6、7 和 8 被 selected,其中仅读取低 2 个字节(我们正在获取的数据的高 2 个字节)。

RAM 模块的更详细视图(针对容量为 4 个字的 4 位机器进行了简化)。

在此图中,很明显您只能 select 一排触发器,因为解码器只会激活其输出线之一,其他 3 条将保持为 0。

请注意,如果访问未对齐的地址(即 MIPS),某些体系结构会通过抛出总线错误来强制对齐所有内存访问,而其他体系结构将执行尽可能多的读取周期以获取所需的数据(即 x86) ,当然这需要额外的硬件(通常是 MMU)。