GNU Info

Info Node: (nasm.info)Section 4.8.7

(nasm.info)Section 4.8.7


Next: Section 4.9 Prev: Section 4.8.6 Up: Section 4.8
Enter node , (file) or (file)node

4.8.7. `ALIGN' and `ALIGNB': Data Alignment
-------------------------------------------

   The `ALIGN' and `ALIGNB' macros provides a convenient way to align
code or data on a word, longword, paragraph or other boundary. (Some
assemblers call this directive `EVEN'.) The syntax of the `ALIGN' and
`ALIGNB' macros is

             align   4               ; align on 4-byte boundary
             align   16              ; align on 16-byte boundary
             align   8,db 0          ; pad with 0s rather than NOPs
             align   4,resb 1        ; align to 4 in the BSS
             alignb  4               ; equivalent to previous line

   Both macros require their first argument to be a power of two; they
both compute the number of additional bytes required to bring the
length of the current section up to a multiple of that power of two,
and then apply the `TIMES' prefix to their second argument to perform
the alignment.

   If the second argument is not specified, the default for `ALIGN' is
`NOP', and the default for `ALIGNB' is `RESB 1'. So if the second
argument is specified, the two macros are equivalent. Normally, you can
just use `ALIGN' in code and data sections and `ALIGNB' in BSS
sections, and never need the second argument except for special
purposes.

   `ALIGN' and `ALIGNB', being simple macros, perform no error
checking: they cannot warn you if their first argument fails to be a
power of two, or if their second argument generates more than one byte
of code.  In each of these cases they will silently do the wrong thing.

   `ALIGNB' (or `ALIGN' with a second argument of `RESB 1') can be used
within structure definitions:

     struc mytype2
     
       mt_byte:
             resb 1
             alignb 2
       mt_word:
             resw 1
             alignb 4
       mt_long:
             resd 1
       mt_str:
             resb 32
     
     endstruc

   This will ensure that the structure members are sensibly aligned
relative to the base of the structure.

   A final caveat: `ALIGN' and `ALIGNB' work relative to the beginning
of the _section_, not the beginning of the address space in the final
executable. Aligning to a 16-byte boundary when the section you're in
is only guaranteed to be aligned to a 4-byte boundary, for example, is
a waste of effort. Again, NASM does not check that the section's
alignment characteristics are sensible for the use of `ALIGN' or
`ALIGNB'.


automatically generated by info2www version 1.2.2.9