2

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)?
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
1ncend1ary
  • 23
  • 6
  • 1
    Syntax always depends on the assembler. Just like how in GAS you'd need to use `.section .data`, you'd use `.p2align 4` there. This looks like NASM syntax, but you haven't specified. Most assemblers other than GAS use `align n`, as documented in their respective manuals, e.g. https://nasm.us/doc/nasmdoc5.html#section-5.10 – Peter Cordes Nov 18 '21 at 10:47
  • 2
    `align` is a directive instructing the assembler to advance until the address is aligned to (i.e. dividable by) a certain number. It is not an instruction. – fuz Nov 18 '21 at 11:41
  • For this special constant, there might be some more efficient instruction to broadcast the immediate `0x80000000` across four elements, instead of needing to load from memory. I don't know SSE/AVX well enough to say offhand, though. – Nate Eldredge Nov 18 '21 at 18:05
  • @NateEldredge: AVX1 can efficiently `vbroadcastss` broadcast-load a single dword. Before that, you'd have to load and shuffle, unfortunately, like `movss` / `shufps`. (Or construct on the fly with `pcmpeqd xmm0, xmm0` / `pslld xmm0, 31` - [What are the best instruction sequences to generate vector constants on the fly?](https://stackoverflow.com/q/35085059)). Not until AVX-512 can we actually use a broadcast memory operand as a source operand for other instructions like `vxorps xmm1, xmm0, [RIP + signbit_mask]{1to4}`, but if you're hoisting this load out of a loop that doesn't matter. – Peter Cordes Nov 19 '21 at 04:10

1 Answers1

2

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.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82