The align
directive works for data as well as code.
In the assembler's output file (an object file in the format the MSVC's linker can understand), it signals the required alignment of each section using metadata.
For example, if you use
section .data
align 1024*1024*2
foo: dd 1234
align 8 ; will assemble to 4 bytes of padding to reach the next multiple of 8
bar: dd 4567
The object file will have its required-alignment for that section set to 2MiB. At least that works on Linux, where I think the section in the object file inherits the highest alignment requirement seen in the source file, so very high alignments are possible.
For win32 object files, NASM even has special syntax for section alignment:
section .data data align=16
like in the example in the manual. The default is align=4
for .data
in win32/win64, apparently. The maximum is 64. I don't know if align
directives inside a section can increase its alignment if none is specified for -f win32
/ -f win64
. If not, as the manual cautions, you might be aligning wrt. the start of the section but not in an absolute sense.
ELF object files (Linux) work the same way, with each section having a required-alignment.
Your object file (hopefully) doesn't end up filled with up-to-2MiB of padding, but it might after linking if it links after something else that has a few bytes in a section that goes into the same segment as .data
in the executable.
But still, knowing (or setting) the minimum alignment of the start of a section, the assembler can support align
directives of any power of 2 at any point in the middle of any section. The align
directive doesn't have to be at the start of a section.
section
directive: nasm.us/doc/nasmdoc7.html#section-7.5.1 – Persuade