What does the designation ".align" mean in x86-64 Assembly?

I notice that there are some directives used with the .align prefix followed by numerical values.

I'm not sure what it is, and even if it is even necessary to use it, but I wrote the x86 assembly and have never used ".align" before.

What is the main purpose of this, and why or why is it not mandatory?

+4
source share
1 answer

From the MSDN entry for the ALIGN directive (using MASM):

ALIGN [[number]] Aligns the next variable or instruction on a byte that is a multiple of number. 

This is often used to optimize the placement of the structure in memory, since all new x86 architectures have a word length of more than 8 bits (usually 32 or 64 bits). This means that you can store 4 or 8 bytes of data in one memory location.

If you used 4 or 8 as the size of the alignment byte, it would ensure that the next byte to be assembled would be placed in a byte position with several 4 or 8, which corresponds to the length of the machine word in this example (other alignment values ​​are often useful depending on your application).


For example, building the following file (compiled syntax):

 org $0x00 db $0x01 db $0x02 .align 4 db $0x03 

This ensures that 0x03 is placed on an address that is an integer multiple of 4 (including zero!). The assembler spit out the following bytes, starting at $0x00 :

 Address | Data --------------- 0x00 | 01 0x01 | 02 0x02 | XX/00 0x03 | XX/00 0x04 | 03 .... | .... 
+10
source

Source: https://habr.com/ru/post/1488162/


All Articles