views:

73

answers:

1

I understand why data need to be aligned (and all the efforts made to accomplish it like padding) so we can reduce the number of memory accesses but this assumes that processor just can fetch addresses multiples of 4(supposing we are using a 32-bit architecture). And because of that assumption we need to align memory and my question is why we can just access addresses multiple of 4(efficiency, hardware restriction, another one)? Which is the advantages of doing this? Why cannot we access all the addresses available?

hugs

+3  A: 

Memory is constructed from hardware (RAM) that is attached to memory busses. The wider the bus, the fewer cycles are required to fetch data. If memory was one byte wide, you'd need four cycles to read one 32-bit value. Over time memory architectures have evolved, and depending on the class of processor (embedded, low power, high performance, etc.), and the cache design, memory may be quite wide (say, 256 bits).

Given a very wide internal bus (between RAM or cache) and registers, say twice the width of the register, you could fetch a value in one cycle regardless of alignment if you have a barrel shifter in the data path. Barrel shifters are expensive, so not all processors have them; without one in the path, multiple cycles would be needed to align the value.

Doug Currie
much clearer and better answer +1
flownt
hi doug, thks for the reply.But I still don't understand why I just can fetch data at addresses multiple of 4, WHICH IS THE ADVANTAGE OF DOING THIS?
utxeeeee
When the address is a multiple of 4 then the data don't have to be shifted and merged from multiple physical memory locations. On some processors this costs in time; on other processors there is no hardware to do the shifting and merging, so it is not permitted.
Doug Currie
Suppose that I can access all addresses in memory and not that are multiple of 4, in this schema the memory alignment wasn't necessary, because I could start at the address that my variable was stored in memory and the fetch the data, here I will just do one fetch (my variable was an int). My difficulty is understand why I can't access all addressable memory values?
utxeeeee
On some processors, you can "access all addressable memory values" because there is a barrel shifter in the (at least double-)wide data path between the data cache and CPU, or there is microcode in the CPU to do multiple memory reads and shift/mask the data for you. On other processors you can't because there is no barrel shifter and no such microcode; this is a cost/benefit decision of the CPU designer.
Doug Currie