Page 94 - ARM 64 Bit Assembly Language
P. 94
Load/store and branch instructions 79
syntax, because the assembler and linker cannot predict the location of the label. The adr in-
struction provides a way to get the address of the label.
1 .section .rodata
2 format: .string "hello, world\n"
3
4 .text
5 .type main, %function
6 .global main
7 main:
8 stp x29, x30, [sp, #-16]!
9
10 // printf("hello, world\n")
11 adr x0, format // Load address of format string
12 bl printf
13
14 // return 0
15 mov w0, 0
16
17 ldp x29, x30, [sp], #16
18 ret
19 .size main, (. - main)
The range of adr is just as limited as an unconditional b or a bl. To address a label that is a
greater distance away, yet within 4 GB in either direction, the adrp instruction can be used.
Note that the following code allocates 4 GB of data. If that allocation were done in the .data
section, then the executable file would contain the 4 GB of data. By allocating the data in the
.bss section, the program size is reduced by 4 GB. As an added benefit, it takes much less
time to assemble the program, and less time to load and run it.
1 .bss
2 closeBy:
3 .word 0x00000000
4 .skip 4000000000
5 farFarAway:
6 .word 0xffffffff
7 .text
8 .type main, %function
9 .global main
10 main:
11 adrp x0, farFarAway
12 add x0, x0, #:lo12:farFarAway
13
14 ret
15 .size main, (. - main)
A special notation is used to add only the lowest 12 bits of the label to the adrp address. This
fully calculates the label address because the 4 KB page is addressable with 12 bits.