Page 132 - ARM 64 Bit Assembly Language
P. 132
118 Chapter 5
Listing 5.7 Complex selection in AArch64 assembly.
1 .text
2 .type main, %function
3 .global main
4 main:
5 stp x29, x30, [sp, #-16]!
6
7 // calculate addresses
8 adr x0, a // x0 = &a
9 adr x1, b // x1 = &b
10 adr x2, c // x2 = &c
11
12 // load from memory
13 ldr x0, [x0] // x0 = a
14 ldr x1, [x1] // x1 = b
15 ldr x2, [x2] // x2 = c
16
17 if: // if ((a < b) && (a < c))
18 cmp x0, x1 // compare a and b
19 bge elseif // if (a >= b) goto elseif
20 cmp x0, x2 // compare a and c
21 bge elseif // if (a >= c) goto elseif
22 b endif // x0 = a
23 elseif:
24 cmp x1, x2 // compare b and c
25 csel x0, x1, x2, lt //x0=(b<c?b:c)
26 endif:
27 adr x9, y // x9 = &y
28 str x0, [x9] // *y = x0
29
30 // return 0
31 mov w0, #0
32 ldp x29, x30, [sp], #16
33 ret
34 .size main, (. - main)
35
36 .data
37 y: .skip 8
38 a: .quad 0x57
39 b: .quad 0x75
40 c: .quad 0x21
The if statement on line 9 of Listing 5.6 is implemented using conditional selection. The
comparison is performed on line 23 of Listing 5.7. Line 24 contains the instruction that condi-
tionally selects the result value.