Purpose of memory alignment

前端 未结 8 1327
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:20

Admittedly I don\'t get it. Say you have a memory with a memory word of length of 1 byte. Why can\'t you access a 4 byte long variable in a single memory access on an unalig

8条回答
  •  粉色の甜心
    2020-11-22 05:56

    If you have a 32bit data bus, the address bus address lines connected to the memory will start from A2, so only 32bit aligned addresses can be accessed in a single bus cycle.

    So if a word spans an address alignment boundary - i.e. A0 for 16/32 bit data or A1 for 32 bit data are not zero, two bus cycles are required to obtain the data.

    Some architectures/instruction sets do not support unaligned access and will generate an exception on such attempts, so compiler generated unaligned access code requires not just additional bus cycles, but additional instructions, making it even less efficient.

提交回复
热议问题