You copied the Doc URL to your clipboard.

21.5 ALIGN

The ALIGN directive aligns the current location to a specified boundary by padding with zeros or NOP instructions.


ALIGN {expr{,offset{,pad{,padsize}}}}



is a numeric expression evaluating to any powerof 2 from 20 to 231


can be any numeric expression


can be any numeric expression


can be 1, 2 or 4.


The current location is aligned to the next lowest addressof the form:

offset + n * expr

n is any integer which theassembler selects to minimise padding.

If expr is not specified, ALIGN setsthe current location to the next word (four byte) boundary. The unusedspace between the previous and the new current location are filledwith:

  • Copies of pad, if pad is specified.

  • NOP instructions, ifall the following conditions are satisfied:

    • pad is not specified.

    • The ALIGN directive follows ARM or Thumb instructions.

    • The current section has the CODEALIGN attribute set on the AREA directive.

  • Zeros otherwise.

pad is treated as a byte,halfword, or word, according to the value of padsize.If padsize is not specified, pad defaultsto bytes in data sections, halfwords in Thumb code, or words inARM code.


Use ALIGN to ensure that your dataand code is aligned to appropriate boundaries. This is typicallyrequired in the following circumstances:

  • The ADR Thumbpseudo-instruction can only load addresses that are word aligned,but a label within Thumb code might not be word aligned. Use ALIGN4 to ensure four-byte alignment of an address withinThumb code.

  • Use ALIGN to take advantageof caches on some ARM processors. For example, the ARM940T has acache with 16-byte lines. Use ALIGN 16 toalign function entries on 16-byte boundaries and maximize the efficiencyof the cache.

  • In ARMv5TE, or in ARMv6 when SCTLR.U is 0, LDRD and STRD doubleword data transfers must be eight-byte aligned. Use ALIGN 8 before memory allocation directives such as DCQ if the data is to be accessed using LDRD or STRD. This is not required in ARMv6 when SCTLR.U is 1, or in ARMv7, because in these versions, doubleword data transfers can be word-aligned.

  • A label on a line by itself can be arbitrarily aligned.Following ARM code is word-aligned (Thumb code is halfword aligned).The label therefore does not address the code correctly. Use ALIGN4 (or ALIGN 2 for Thumb) beforethe label.

Alignment is relative to the start of the ELF section wherethe routine is located. The section must be aligned to the same,or coarser, boundaries. The ALIGN attribute on the AREA directiveis specified differently.


        AREA    cacheable, CODE, ALIGN=3rout1   ; code         ; aligned on 8-byte boundary        ; code        MOV     pc,lr  ; aligned only on 4-byte boundary        ALIGN   8      ; now aligned on 8-byte boundaryrout2   ; code

In the following example, the ALIGN directivetells the assembler that the next instruction is word aligned andoffset by 3 bytes. The 3 byte offset is counted from the previousword aligned address, resulting in the second DCB placedin the last byte of the same word and 2 bytes of padding are tobe added.

        AREA    OffsetExample, CODE        DCB     1      ; This example places the two bytes in the first        ALIGN   4,3    ; and fourth bytes of the same word.        DCB     1      ; The second DCB is offset by 3 bytes from the                        ; first DCB.

In the following example, the ALIGN directivetells the assembler that the next instruction is word aligned andoffset by 2 bytes. Here, the 2 byte offset is counted from the nextword aligned address, so the value n isset to 1 (n=0 clashes with the third DCB).This time three bytes of padding are to be added.

        AREA    OffsetExample1, CODE        DCB     1      ; In this example, n cannot be 0 because it        DCB     1      ; clashes with the 3rd DCB. The assembler         DCB     1      ; sets n to 1.        ALIGN   4,2    ; The next instruction is word aligned and         DCB     2      ; offset by 2.

In the following example, the DCB directivemakes the PC misaligned. The ALIGN directive ensuresthat the label subroutine1 and the followinginstruction are word aligned.

        AREA    Example, CODE, READONLYstart   LDR     r6,=label1        ; code        MOV     pc,lrlabel1  DCB     1      ; PC now misaligned        ALIGN          ; ensures that subroutine1 addressessubroutine1            ; the following instruction.        MOV r5,#0x5

Related reference

Was this page helpful? Yes No