Page 130 - ARM 64 Bit Assembly Language
P. 130

116 Chapter 5



                                               Listing 5.3 Selection in C.
                            .
                1           . .
                2  static int a,b,x;
                            .
                3           . .
                4         if(a<b)
                5           x=1;
                6         else
                7           x=0;
                            .
                8           . .



                           Listing 5.4 Selection in AArch64 assembly using branch instructions.
                           .
                1          . .
                2         adr     x0, a        // load pointer to a
                3         adr     x1, b        // load pointer to b
                4         ldr     x0, [x0]     // load a
                5         ldr     x1, [x1]     // load b
                6  if:
                7         cmp     x0, x1       // compare them
                8         bge     else         // if a >= b then skip forward
                9         mov     x0, #1       // THEN section - load 1 into x0
                10        b       endif        // skip the else section
                11  else:
                12        mov     x0, #0       // ELSE section - load 0 into x0
                13  endif:
                14        adr     x1, x        // load pointer to x
                15        str     x0, [x1]     // store x0 in x
                           .
                16         . .



                         Listing 5.5 Selection in AArch64 assembly using a conditional instruction.
                           .
                1          . .
                2         adr     x0, a        // load pointer to a
                3         adr     x1, b        // load pointer to b
                4         ldr     x0, [x0]     // load a
                5         ldr     x1, [x1]     // load b
                6         cmp     x0, x1       // compare them
                7         cset    x0, lt       // Conditionally set x0 to 1 if a
                8         adr     x1, x        // load pointer to x
                9         str     x0, [x1]     // store x0 in x
                           .
                10         . .
   125   126   127   128   129   130   131   132   133   134   135