Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding word alignment

I understand what it means to access memory such that it is aligned but I don’t understand why this is necessary. For instance, why can I access a single byte from an address 0x…1 but I cannot access a half word (two bytes) from the same address.

Again, I understand that if you have an address A and an object of size s that the access is aligned if A mod s = 0. But I just don’t understand why this is important at the hardware level.

like image 672
ChrisDiRulli Avatar asked Oct 18 '09 07:10

ChrisDiRulli


People also ask

What are the 4 types of aligning the text?

Alignment determines the appearance and orientation of the edges of the paragraph: left-aligned text, right-aligned text, centered text, or justified text, which is aligned evenly along the left and right margins.

What are the 3 types of alignment?

There are three main types of alignments available – front-end, thrust and four-wheel. The type of suspension that your vehicle has determines what kind of alignment your car will receive. Your mechanic will be able to recommend the right alignment type for your vehicle.

What does alignment in word mean?

Alignment refers to where and how the text lines up. Default settings in Microsoft Word will left- align your text, but there are many other ways to format a document's alignment. This tutorial outlines two ways to modify alignment: using keyboard commands and using the Ruler bar.


2 Answers

Hardware is complex; this is a simplified explanation.

A typical modern computer might have a 32-bit data bus. This means that any fetch that the CPU needs to do will fetch all 32 bits of a particular memory address. Since the data bus can't fetch anything smaller than 32 bits, the lowest two address bits aren't even used on the address bus, so it's as if RAM is organised into a sequence of 32-bit words instead of 8-bit bytes.

When the CPU does a fetch for a single byte, the read cycle on the bus will fetch 32 bits and then the CPU will discard 24 of those bits, loading the remaining 8 bits into whatever register. If the CPU wants to fetch a 32 bit value that is not aligned on a 32-bit boundary, it has several general choices:

  • execute two separate read cycles on the bus to load the appropriate parts of the data word and reassemble them
  • read the 32-bit word at the address determined by throwing away the low two bits of the address
  • read some unexpected combination of bytes assembled into a 32-bit word, probably not the one you wanted
  • throw an exception

Various CPUs I have worked with have taken all four of those paths. In general, for maximum compatibility it is safest to align all n-bit reads to an n-bit boundary. However, you can certainly take shortcuts if you are sure that your software will run on some particular CPU family with known unaligned read behaviour. And even if unaligned reads are possible (such as on x86 family CPUs), they will be slower.

like image 197
Greg Hewgill Avatar answered Oct 24 '22 18:10

Greg Hewgill


The computer always reads in some fixed size chunks which are aligned.

So, if you don't align your data in memory, you will have to probably read more than once.

Example

  • word size is 8 bytes
  • your structure is also 8 bytes
  • if you align it, you'll have to read one chunk
  • if you don't align it, you'll have to read two chunks

So, it's basically to speed up.

like image 35
Etan Avatar answered Oct 24 '22 18:10

Etan