AArch64/ARM64 Tutorial

Chapter 20: Address Relative Loading

Even though the Program Counter (PC) is a read-only register, its value can be utilized for setting a GPR to a Memory Address.


Compute Address:
adr xD, label

Instruction can only accept Extended Registers. The instruction will set xD to the memory address designated by label. For example...

adr x9, location_of_text
.. // Dots represent instructions in between adr and text
location_of_text:
.asciz "Hi there"

Whatever memory address the text is residing at, that address will be placed in x9.

Another example:
adr XD, .

The "." represents 'here'. Use this instruction to get the address (value) of the PC at any time.


Compute Page-Aligned Address:
adrp xD, label

This will do the same as above except the address loaded into xD will have the lower 12 bits cleared (zeroed), Thus, any address value loaded via adrp will end in 0x000. The reason this instruction exists is because the adr instruction can only compute an address that is no further than +/-1MB away from the PC. The adrp instruction increases this range to +/-4GB.

Since adrp generates address's that are missing their lower 12 bits, you will need to complete the lower 12-bits of the address via an add instruction. Like so...

adrp xD, label
add xD, xD, :lo12: label

The :lo12: is an ARM64 GNU compliant Assembler Directive that will add just the lower 12-bits of label's Address.

Example: (load x5 with address using label name of "test")
adrp x5, test
add x5, x5, :lo12: test


Next Chapter

Tutorial Index