Align in assembly x86

Viewed 224

I'm having trouble understanding align.
I tried running the following:

section .data
align 4
xs: dw 0xA1A2
ys: db 0xB1, 0xB2, 0xB3, 0xB4

and see what each byte would be, I expected it to be a contiguous block in memory as follows:

           for instance:    4      5    6    7     8    9     A     B
    (addr divisible by 4):[0xA2, 0xA1, 0x0, 0x0, 0xB1, 0xB2, 0xB3, 0xB4]

In order to make the address of ys divisible by 4 Instead it was more like:

                            4      5    6      7     8    9     
    (addr divisible by 4):[0xA2, 0xA1, 0xB1, 0xB2, 0xB3, 0xB4]

Obviously I have a misconception of how align works. Could anyone please clear that up?

1 Answers

To get what you want you need to put an align directive directly in front of the ys data. Also, to get it to use zero bytes as alignment data you have to explicitly specify this. (The default is 90h nop.) Like this:

section .data
align 4, db 0
  xs: dw 0xA1A2
align 4, db 0
  ys: db 0xB1, 0xB2, 0xB3, 0xB4

This is because the directive does not mean "going forward, align all data to this boundary". What it does mean is, "at this particular point align the emitted data once to this boundary".

Related