What does the ".align" x86 Assembler directive do exactly?

Sinister Clock picture Sinister Clock · Jun 25, 2013 · Viewed 13.8k times · Source

I will list exactly what I do not understand, and show you the parts I can not understand as well.

First off,

The .Align Directive

  1. .align integer, pad. The .align directive causes the next data generated to be aligned modulo integer bytes

1.~ ? : What is implied with "causes the next data generated to be aligned modulo integer bytes?" I can surmise that the next data generated is a memory-to-register transfer, no? Modulo would imply the remainder of a division. I do not understand "to be aligned modulo integer bytes".......

What would be a remainder of a simple data declaration, and how would the next data generated being aligned by a remainder be useful? If the next data is aligned modulo, that is saying the next generated data, whatever that means exactly, is the remainder of an integer? That makes absolutely no sense.

What specifically would the .align, say, .align 8 directive issued in x86 for a data byte compiled from a C char, i.e., char CHARACTER = 0; be for? Or specifically coded directly with that directive, not preliminary Assembly code after compiling C? I have debugged in Assembly and noticed that any C/C++ data declarations, like chars, ints, floats, etc. will insert the directive .align 8 to each of them, and add other directives like .bss, .zero, .globl, .text, .Letext0, .Ltext0.

What are all of these directives for, or at least my main asking? I have learned a lot of the main x86 Assembly instructions, but never was introduced or pointed at all of these strange directives. How do they affect the opcodes, and are all of them necessary?

Answer

faffaffaff picture faffaffaff · Jun 25, 2013

As mentioned in the comments, it means the compiler will add enough padding bytes so the next data lands on an "even" position (divisible by the alignment value). This is important because aligned memory access is much faster than unaligned memory access. (Loading a doubleword from 0x10000 is better than loading a doubleword from 0x10001). It might also be useful in case you are interfacing with other components and need to send/receive structs of data with a given padding/alignment.