memory alignment

博客介绍了汇编代码中.align指令的作用,它用于将数据位置对齐到内存字边界,目的是提升程序运行性能。通过对比数据对齐和未对齐时CPU获取操作数的情况,说明对齐可减少内存访问次数,虽有少量内存浪费,但能提高整体性能,且通常该指令用于数据而非指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If u are careful enough, u will notice, when reading assembly code, a sentence as below is met
quite often:
.align
It is used to align the position of data to boundary of memory word. Why do so is a matter of
program running performance. When CPU needs a data as its operand of current executing
instruction, it gets the data via bus between itself and main memory. Say, the bus we referred
is a 32-bit bus, which is not uncommon in today's 2-byte=1-word  PCs. Then let's simulate CPU
to fetch the operand, and do an intuitional comparison among aligned and unaligned conditions.
A very important issue to our comparison must be cited first, that is, the memory must only be
accessed in such a manner as follows:
1. 32-bit one time, namely, 4-byte a time, which is just the bandwidth of the 32-bit bus;
2. these bytes must start at an aligned address, namely, the address of the first byte is formally
like XXX...XX00;

We have fixed our bus above, 32-bit. And we now assume our operand and its allocation as table:

 

 Unaligned

 aligned

 operand

 4-byte

 4-byte

 Memory allocation
(colored: boundary)

xxx…xx010
xxx…xx011
xxx…xx100
xxx…xx101

xxx…xx000
  xxx…xx001
  xxx…xx010
 
xxx…xx011


We first consider unaligned allocation of the data, surely, by the means of the bus above,
we can not fetch the operand in one time, because it span across two aligned memory region.
One time, a sole and intact aligned memory region will be transmitted via the bus. And we need
two times at least.(also at most here, because this operand is 32-bit in all ). Exactly, to the table
above, the first time we fetch xxx…xx000 -- xxx…xx011; the second time, xxx…xx100 – xxx…xx111;
then it’s the CPU ‘s job to extract each part of the operand and assemble them to an entire data.
On the other hand, for aligned case, a time for xxx…xx000 – xxx…xx011 is enough for the same operand.

Since the memory access time is relatively long for computer system, this method evidently reduces
the memory access times and enhances the overall performance, although with a small wastage
of memory.

Also we may notice that usually this .align operator is found before data rather than normal instruction,
the reason is instructions are usually prefetched before needed, and in form of blocks rather than sole
instruction one time, so it is unnecessary for aligning the instructions.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值