The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ecube on February 18, 2008, 12:51:29 PM

Title: about align
Post by: ecube on February 18, 2008, 12:51:29 PM
can someone please explain about align? I see align 16, align 8 etc... the questions I have are

What does align do?
Is align ever a requirement?
How do you know the best number to align by?
Title: Re: about align
Post by: hutch-- on February 18, 2008, 01:41:00 PM
Cuibe,

Depending on the processor, data is fetched on one grab if the data is aligned by at least 4 bytes. This means it evenly divides by 4. With code its less important but can still play a part at times, usually labels are aligned in code if you test some advantage from doing so.
Title: Re: about align
Post by: Tedd on February 18, 2008, 07:58:04 PM
It inserts 'space' (NOP for code, 0 for data) so that the current offset becomes evenly divisible by the given number - so the next instruction/data lies on an 'aligned' address.
It's rarely a requirement, though some instructions (mainly SSE) and certain structures do need it (and they generally cause an exception if you don't play along.)
The appropriate alignment depends on what you're aligning and why. A simple rule is to align by the size of the data you're aligning - so words at align-2, dwords at align-4, qwords at 8, etc - this simply helps the processor access the data in a single operation. There can be certain performance benefits by aligning particular data/functions/jump-destinations, which again just helps the processor and its caches. It's sometimes good to align functions to 16, but in general it won't make noticable difference (unless you call the function a lot) and you shouldn't worry too mcuh about it.