Page 134 - ARM 64 Bit Assembly Language
P. 134

120 Chapter 5


                                             Listing 5.9 Pre-test loop in C.
                           .
                1          . .
                2   while (x >= y)               // perform loop test
                3   {
                           .
                4          . .                   // body of loop
                5   }                            // done
                           .
                6          . .


                                     Listing 5.10 Pre-test loop in AArch64 assembly.
                           .
                1          . .
                2  loop:  cmp     x0, x1         // perform loop test
                3         blt     done           // exit loop if x0 < x1
                           .
                4          . .                   // body of loop
                5         b       loop           // repeat loop
                6  done:
                           .
                7          . .



                                            Listing 5.11 Post-test loop in C.
                           .
                1          . .
                2   do {
                           .
                3          . .                   // body of loop
                4   } while (x >= y);            // perform loop test
                           .
                5          . .


                  to false, then execution branches to the first instruction following the loop body. All structured
                  programming languages have a pre-test loop construct. For example, in C, the pre-test loop is
                  called a while loop. In assembly, a pre-test loop is constructed very similarly to an if-then
                  statement. The only difference is that it includes an additional branch instruction at the end of
                  the sequence of instructions that form the body. Listing 5.10 shows a pre-test loop in AArch64
                  assembly.


                  5.3.2 Post-test loop

                  In a post-test loop, the test is performed after the loop body is executed. If the test evaluates
                  to true, then execution branches to the first instruction in the loop body. Otherwise, execution
                  continues sequentially. Most structured programming languages have a post-test loop con-
                  struct. For example, in C, the post-test loop is called a do-while loop. Listing 5.11 shows a
   129   130   131   132   133   134   135   136   137   138   139