I notice that there's some directives used with the .align prefix followed by numerical values.
I'm not sure on what this is, nor if it's even necessary to use, but I've wrote x86 Assembly and never used ".align" before.
What is the basic purpose of it, and why or why not is it mandatory?
41 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 struct placement in memory, since all new x86 architectures have a word length over 8 bits (usually 32 or 64 bits). This means that in a single memory location, you can store 4 or 8 bytes of data.
If you were to use 4 or 8 as your alignment byte size, it would ensure that the next byte to be assembled will be placed at a byte location of multiple 4 or 8, corresponding to the machine word length in this example (other alignment values are often useful depending on your application).
For example, assembling the following file (made-up syntax):
org $0x00
db $0x01
db $0x02
.align 4
db $0x03It would ensure that the 0x03 is placed on an address that is any integer multiple of 4 (includes zero!). The assembler would spit out the following bytes starting from $0x00:
Address | Data
--------------- 0x00 | 01 0x01 | 02 0x02 | XX/00 0x03 | XX/00 0x04 | 03 .... | .... 1