Author Topic: Need help on alignment  (Read 15770 times)

nobody

  • Guest
Need help on alignment
« on: December 21, 2007, 09:36:20 PM »
Hi could you help me on Align bytes in nasm code i went throught nasm manual i m sorry to say it does'nt satisfy me sorry for that please explain if you could thanks !!

mcamember

  • Guest
Re: Need help on alignment
« Reply #1 on: December 22, 2007, 02:24:30 AM »
Hi,
I can show you two examples of using the ALIGN directive for
data structures which are used in defining Dialog boxes. The
value after the ALIGN directive is expressed in BYTES.

The first example is the structure DLGTEMPLATE. The Windows API
says that this structure must be aligned on a WORD boundary so
IMMEDIATELY before declaring this structure in the data section
I put the directive ALIGN 2. (One WORD =2 BYTES).

align 2
    dlgTrans:
    istruc DLGTEMPLATE
        at DLGTEMPLATE.style,            dd NULL
        at DLGTEMPLATE.dwExtendedStyle,  dd NULL
        at DLGTEMPLATE.cdit,             dw NULL
        at DLGTEMPLATE.x,                dw NULL
        at DLGTEMPLATE.y,                dw NULL
        at DLGTEMPLATE.lx,               dw NULL
        at DLGTEMPLATE.ly,               dw NULL
    iend
dlgTransMenu  dw 0x0
dlgTransClass dw 0x0
TransCaption dw 'F','r','o','m',' ','G','e','r','m','a','n', 0x0
dlgTransFont dw 0bh,'A','R','I','A','L',0X0

The next example uses another Dialog structure, DLGITEMTEMPLATE which the Windows API says must be aligned on a DWORD boundary
so IMMEDIATELY before declaring this structure I put the directive
ALIGN 4. (One DWORD = 2 WORDS = 4 BYTES)


align 4
    dlgTransItemEdit:
    istruc DLGITEMTEMPLATE
    at DLGITEMTEMPLATE.style,           dd NULL
    at DLGITEMTEMPLATE.dwExtendedStyle, dd NULL
    at DLGITEMTEMPLATE.x,               dw NULL
    at DLGITEMTEMPLATE.y,               dw NULL
    at DLGITEMTEMPLATE.lx,              dw NULL
    at DLGITEMTEMPLATE.ly,              dw NULL
    at DLGITEMTEMPLATE.id,              dw NULL
    iend
dlgTransItemEditClName dw 0ffffh
                      dw 0081h
dlgTransItemEditTitle dw 0;
dlgTransItemEditCrDat dw 0

The ALIGN directive will cause the assembler to insert whatever
dummy values are necessary after the preceding data (or code) to
ensure that the first byte following the ALIGN directive is set
on the required boundary.

It's possible to get lucky and have the data or code item installed
exactly on the correct boundary but if you change anything ahead of
it in code or data the item probably won't be in alignment any
longer.

I can tell you from my experience with these Dialog Box structures that the ALIGN directive is crucial. If the alignment is off at all, the Dialog Box won't get created.

Hope this helps.
afk

nobody

  • Guest
Re: Need help on alignment
« Reply #2 on: December 22, 2007, 08:12:22 AM »
indeed it does help

cheers
unknown