memory alignment

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值