2

I am attempting to learn about memory alignment, without much success admittedly. I am using this article from IBM.

Can someone please explain to me what this excerpt means from the double byte memory access granularity section:

However, notice what happens when reading from address 1. Because the address doesn't fall evenly on the processor's memory access boundary, the processor has extra work to do. Such an address is known as an unaligned address. Because address 1 is unaligned, a processor with two-byte granularity must perform an extra memory access, slowing down the operation.

Why is another memory access in order? What does it mean by memory access boundary and it being even on the memory access boundary?

I have a VERY limited knowledge on the CPU, as I have only delt with upper level programming (Objective-C and C++). Any help is greatly appreciated!

Thanks!

foobar5512
  • 2,470
  • 5
  • 36
  • 52
  • Did you read the earlier part of the article, where it described "memory access granularity", with a diagram showing how the processor accesses memory? – Barmar May 18 '13 at 03:02
  • Yes, but I don't understand why that would take another call. – foobar5512 May 18 '13 at 03:05

1 Answers1

6

The example is describing what happens when you try to read a block of 4 consecutive bytes on a CPU with double-byte access granuality. On this type of CPU, memory is accessed as pairs of bytes, always starting with an even-numbered byte.

If you try to read the block starting with byte 0, it has to perform 2 reads: bytes 0-1 and bytes 2-3.

If you try to read the block starting with byte 1, it has to perform 3 reads: bytes 0-1 (to get byte 1), bytes 2-3, and bytes 4-5 (to get byte 4).

Memory access granularity is the number of bytes it accesses at a time, and a memory access boundary is where each of these groups of bytes begins. The groups of bytes are always addressed at even multiples of the granularity -- if it's double-byte granularity they start on even addresses, if it's quad-byte granularity they're at multiples of 4.

As an analogy, consider an apartment building with 4 units on each floor. Units 0-3 are on floor 0, units 4-7 are on floor 1, etc. If you want to slip a flyer under the doors of units 0-3, you only have to go to one floor. But if you want to slip a flyer under 1-4, you have to go to 2 floors: floor 0 for 1-3, floor 2 for unit 4.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Is there a reason why it can't just start at 1. For example, 1-2, 2-3. Wouldn't that also be 4 bytes? Thanks! – foobar5512 May 18 '13 at 03:20
  • 2
    Because that's not how aligned memory access works. The hardware is designed to access memory in specific chunks. – Barmar May 18 '13 at 03:28
  • 1
    See the analogy I just added to the answer, it should help understand. – Barmar May 18 '13 at 03:31