Being a C convert, I have always been confused when I here about boundaries and alignment in asm. For instance, if a buffer is supposed to begin on the next 16 bit boundary after another buffer, what does that mean in lay-terms? Another example is "dword aligned". What does this mean? I have an idea but it might be all wrong. Thanks.
Posted on 2002-01-16 15:25:12 by rdaneel
Memory alignment simply means the lowset bits of the address meet some condition. For example, for DWORD alignment, the address must be even (as an odd address is right in the middle of a dword).

How do you know that? Well, concider address zero is ALWAYS aligned with everything, so address boundaries of type X are always X apart and thus are always evenly divisible by X.

For dwords, X=2, thus they are on even addresses.

MASM supports alignment with the ALIGN keyword. Example:




.data

ALIGN DWORD
MyAlignedSpace BYTE 256 DUP(0)



This places the start of MyAlignedSpace at an even address, even if a byte is wasted.



WHY does it need be aligned? That doesn't matter, it's a hardware issue. ;-)
Posted on 2002-01-16 18:07:03 by Ernie
DWORD alignment means the address is divisible by 4!
Posted on 2002-01-16 18:11:05 by bitRAKE
The x86 can handle data on any byte boundary. But for best performance, data should generally be aligned on a boundary that is a multiple of it's size, DD items should start on a 4 byte boundary for example. If an item "crosses" a boundary, extra memory accesses may be needed to fetch the data, slowing down the program.

Agner Fog's optimization guide covers some data alignment issues pretty well. :)
Posted on 2002-01-16 21:18:12 by S/390
I think I understand now. So then is it correct that you couldn't cross a BYTE boundary with a BYTE value, but you can cross a WORD, etc. boundary with any other value.
Posted on 2002-01-17 08:30:35 by rdaneel
<quickly silently edits his previous post so he can make bittRake seem silly>


OK, OK, you got me again.
Posted on 2002-01-17 23:33:45 by Ernie
how do you align by more than a QWORD (like a page or something)...? once again, i RT*M and i don't know... i remember seeing some message about that once, but it seems to have disappeared :( Thanks :)
Posted on 2002-07-29 22:09:35 by jademtech
Look at the sample code here http://www.asmcommunity.net/board/showthread.php?threadid=6395&highlight=strtok and check the alignments used and the segment declarations(segdecl.inc).
Posted on 2002-07-29 22:29:49 by stryker