The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: indiocolifa on September 04, 2007, 11:44:07 PM

Title: Help with alignment
Post by: indiocolifa on September 04, 2007, 11:44:07 PM
I'm developing a very small fractal drawing application.

This is the data I'm using:


; structure for complex numbers,
; fractal parameters and data defs
;
COMPLEX STRUCT
x REAL8 ?
y REAL8 ?
COMPLEX ENDS

COMPLEXRECT STRUCT
x1y1 COMPLEX <?>
x2y2 COMPLEX <?>
COMPLEXRECT EndS

FRACTALPARAMS STRUCT
       extent COMPLEXRECT << -2.5, 2.5>,<0.5,0.5>>
       bailout DWORD 10
       iter DWORD 256
FRACTALPARAMS EndS

.data
wndrect RECT <?>
params FRACTALPARAMS <>
_TWO REAL8 2.0


; colormap array

colormap COLORREF 00080808h, 00100830h, 00101080h, 002020FFh,
004040FFh, 008040E0h, 00C040C0h, 00FF00C0h,
00FF0880h, 00FF1040h, 00FF2010h, 00FF4000h,
00FF8000h, 00FFC000h, 00FFFF00h, 00000000h


I know what alignment is and what are the penalities for accesing unaligned data. But I want a very quick explanation on how to align the data above (specially the Structs) to a proper value. Should I align all to DWORD boundaries?

Thank you very much!
Title: Re: Help with alignment
Post by: hutch-- on September 05, 2007, 12:18:00 AM
Properly you should align data on the data SIZE or a larger multiple of that data size. As long as you use .486 or higher you can align up to 16 bytes which will do most things but may miss some of the later exotic data sizes.
Title: Re: Help with alignment
Post by: raymond on September 06, 2007, 12:31:44 AM
The very first variable you declare in your .data section will automatically be aligned to a page boundary. Since your structs are all multiples of 8 bytes, positioning all your variables related to those structs (and those declared as REAL8) at the top of your .data section will guarantee that they will ALL be properly aligned without any need to instruct the assembler about alignement.

Then declaring all your DWORD variables will guarantee that those will all be aligned on a DWORD boundary. I always declare byte variables as the very last ones in the .data section.

If you also declare variables in the .data? section, you may only be certain that the first variable will be aligned on a DWORD boundary, but not necessarily on a paragraph or page boundary.

Raymond
Title: Re: Help with alignment
Post by: jdoe on September 06, 2007, 01:39:45 AM
Quote from: raymond on September 06, 2007, 12:31:44 AM

If you also declare variables in the .data? section, you may only be certain that the first variable will be aligned on a DWORD boundary, but not necessarily on a paragraph or page boundary.

Raymond



What is the difference between a page and a paragraph boundary. I'm not really good when it comes to the internal details of a computer as you see by my question.

:red

Title: Re: Help with alignment
Post by: Rockoon on September 06, 2007, 08:58:59 AM
Paragraph boundary is 16 bytes
Page boundary is 4096 bytes under Win32 / NT / Vista

You can achieve paragraph alignment with no muss and no fuss ..

If you need better than that then you cannot take advantage of the windows PE loader to do the grunt work for you via the masm align directive. It is not technically possible under Win32 / NT / Vista because the PE loader simply doesnt offer better.

Also of note is cache line boundaries .. which are 64 bytes on most modern CPU's