24

My question has two parts.

First, as a newbie to this address space, I would like to know what is the meaning of memory alignment of an address. I Googled about it but wanted to ask this question here as well since I found answers here very useful.

The second part of my question is related to alignment and programming: how do I find if an address is 4 byte aligned or not ? Somewhere I read:

  if(address & 0x3) // for 32 bit register 

But I don't really know how this checks for a 4 byte alignment. Could anyone explain it in detail?

Edit: It would be great If someone can draw pictorial view on this subject.

Thanks

sansuiso
  • 9,259
  • 1
  • 40
  • 58
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199

1 Answers1

40

Sequential addresses refer to sequential bytes in memory.

An address that is "4-byte aligned" is a multiple of 4 bytes. In other words, the binary representation of the address ends in two zeros (00), since in binary, it's a multiple of the binary value of 4 (100b). The test for 4-byte aligned address is, therefore:

if ( (address & 0x3) == 0 )
{
    // The address is 4-byte aligned here
}

or simply

if ( !(address & 0x3) )
{
    // The address is 4-byte aligned here
}

The 0x3 is binary 11, or a mask of the lowest two bits of the address.

Alignment is important since some CPU operations are faster if the address of a data item is aligned. This is because CPUs are 32-bit or 64-bit word based. Small amounts of data (say 4 bytes, for example) fit nicely in a 32-bit word if it is 4-byte aligned. If it is not aligned, it can cross a 32-bit boundary and require additional memory fetches. Modern CPUs have other optimizations as well that improve performance for address aligned data.

Here's a sample article regarding the topic of alignment and speed.

Here are some some nice diagrams of alignment.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • 2
    And some designs (ARM for example) are not just inefficient at unaligned access, they don't even support it in hardware at all - the programmer must explicitly read the pieces and put them back together. Needless to say unaligned data would be avoided there, except when packing things for size or for interchanging in a format that requires it. – Chris Stratton Oct 04 '13 at 20:58
  • Thanks @mbratch for your response ,If all resgisters on a Harware device is 32 bit then is it necessary that they must be 4 byte aligned.Is it true?? – Amit Singh Tomar Oct 04 '13 at 21:04
  • @AmitSinghTomar it depends upon the microprocessor. In many cases, it just leads to less efficient processing if not aligned. – lurker Oct 04 '13 at 21:11
  • 1
    One more thing @mbratch for above case if address is not 4 byte aligned ,is there any chance of accessing undesired register?? – Amit Singh Tomar Oct 04 '13 at 21:11
  • @AmitSinghTomar Not sure what you mean by that. One some processor, if you try to access some data type that's not properly aligned, the hardware will fix it up, and your program will not notice anything except it takes a few more cycles to execute. On other processors, the processor will issue a [trap.](http://stackoverflow.com/questions/7871589/what-are-traps) – nos Oct 04 '13 at 21:26
  • See @nos My point is very simple if I have an FPGA device which have 3 registers and all 32 bit in size ,address of 1st register would be 0x0 ,for 2nd it would be 0x4 and 3rd would be 0x8 ,Now I have routine which will read these register and if I give an address which is not a 4 byte aligned address then read to desired register would fail.Do any one agree to what I think?? – Amit Singh Tomar Oct 04 '13 at 21:39
  • @AmitSinghTomar indeed, if you are accessing special hardware, not just memory, there are several things you have to consider. Much may depend upon how the hardware designer implemented the memory mapped access of the FPGA whether a non-byte aligned access will cause trouble or not. If you were in a situation where you weren't sure, then you would play it safe and assume that you'd need alignment. But in our FPGA address constants would be set properly and it shouldn't be an issue. – lurker Oct 05 '13 at 00:27
  • 1
    sometimes you may want to put it in macro form or even in a form more available across all your translation unit or sources, here in this link on StackOverflow you get some macros. https://stackoverflow.com/questions/1898153/how-to-determine-if-memory-is-aligned – Peter Mar 03 '20 at 22:01
  • 1
    You are amazing! and those links introduced are really great! I finally got to understand alignment. Thanks you so much. – Sami Apr 16 '21 at 14:10