Search code examples
assemblyx86memory-alignmentintel-syntax

What is the syntax of "align" keyword/instruction in x86 assembly?


As far as I understand, some objects in the "data" section sometimes need alignment in x86 assembly.

An example I've come across is when using movaps in x86 SSE: I need to load a special constant for later xors into an XMM register.

The XMM register is 128 bits wide and I need to load a 128-bit long memory location into it, that would also be aligned at 128 bits.

With trial and error, I've deduced that the code I'm looking for is:

section .text
_foo:
  movaps xmm0, [xor_const]  ; this is the loading part
  xor eax, eax
  ret

section .data
  align 16  ; align to 16 bytes = 128 bits
  xor_const: dd 80000000h, 80000000h, 80000000h, 80000000h  ; the 128-bit constant

My questions are:

  • In which assembly flavors do I use .align instead of align?
  • Do I need to write this keyword/instruction before every data object or is there a way to write it just once?
  • What is a relevant documentation I can read about this?
  • Is "align" a keyword or an instruction (or maybe something else)?

Solution

  • In which assembly flavors do I use .align instead of align?

    Most notably the GNU assembler (GAS) uses .align, but every assembler can have its own syntax. You should check the manual of whatever assembler you are actually using.

    Do I need to write this keyword/instruction before every data object or is there a way to write it just once?

    You don't need to write it before each object if you can keep track of the alignment as you go. For instance, in your example, you wrote align 16 and then assembled 4 dwords of data, which is 16 bytes. So following that data, the current address is again aligned to 16 and another align 16 would be unnecessary (though of course harmless). You could write something like

      align 16  ; align to 16 bytes = 128 bits
      xor_const: dd 80000000h, 80000000h, 80000000h, 80000000h  ; the 128-bit constant
      another_const: dd 0deadbeef, 0deadbeefh, 0deadbeefh, 0deadbeefh
    

    and be assured that another_const is also 16-byte aligned. Of course, if you make a typo and one of the constants ends up with more or fewer bytes than you meant, then everything else will be wrong and your program may break horribly - but maybe that's good in that you will find the bug faster.

    What is a relevant documentation I can read about this?

    The manual for the assembler you are using. For instance GAS manual, NASM manual, etc.

    Is "align" a keyword or an instruction (or maybe something else)?

    It would usually be called a directive - a command that affects the behavior of the assembler but isn't itself a machine instruction.