Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the .align directive do?

Tags:

x86

I'm learning x86, and I'm trying to figure out what the .align directive does and how it can be useful.

The Oracle Reference Manual says:

The .align directive causes the next data generated to be aligned modulo integer bytes. Integer must be a positive integer expression and must be a power of 2. If specified, pad is an integer byte value used for padding. The default value of pad for the text section is 0x90 (nop); for other sections, the default value of pad is zero (0).

But I'm not sure what it means. Would you refer to where I can read more about it or explain it briefly with an example?

like image 264
UXkQEZ7 Avatar asked Jan 15 '23 09:01

UXkQEZ7


1 Answers

The key to understanding what it does is to understand why it's there.

All computers have a natural boundary known as the wordsize. This boundary is typically 4-bytes or 8-bytes.

The CPU can load and store from memory faster and without wasting cache space if 4 and 8 byte values are located on these boundaries. Some types of CPU's cannot fetch misaligned values at all.

So, there must be a mechanism in the assembler that skips to the next boundary so that a label and a storage allocation directive can begin on a more efficient address.

For instructions, odd boundaries work on most computers, but they still have performance implications and waste cache space.

   .string "ab\0"
; this next address is 3

vs

    .string "ab\0"
    .align 4 # sometimes interpreted as 2**n, so, .align 2
; this next address is 4, and would still be 4 if the string was just "a"
like image 162
DigitalRoss Avatar answered Feb 16 '23 00:02

DigitalRoss