Page 113 - ARM 64 Bit Assembly Language
P. 113
Data processing and other instructions 99
4.2.9.3 Examples
1 cmp w0, 35 // Compare w0 to 35 and set flags
2 cmn x2, w3, sxtw // Compare negative x2 to sxtw and set flags
3 tst w1, 0xaaaaaaaa // Test bits in w1 and Set PSTATE flags
Example 10. The following listings show how the compare, branch, and add instructions can
be used to create a for loop. There are basically three steps for creating a for loop: allocating
and initializing the loop variable, testing the loop variable, and modifying the loop variable.
In general, any of the registers x0-x15, except x8 can be used to hold the loop variable. These
are the scratch registers shown in Fig. 3.2. Section 5.4 in Chapter 5 introduces some consider-
ations for choosing an appropriate register. For now, it is assumed that x0 is available for use
as the loop variable in the example.
Suppose we want to implement a loop that is equivalent to the following C code:
.
1 . .
2 for(int i = 0; i < 10; i++)
3 {
4
5 /* insert loop body statements here */
6
7 }
.
8 . .
The loop can be written with the following AArch64 assembly code:
.
1 . .
2 mov w0, #0 // int i = 0;
3 loop: cmp w0, #10
4 bge endloop // Exit loop if r0 > 10
5
6 // Insert loop body instructions here
7
8 add w0, w0, #1 // i++
9 b loop
10 endloop:
.
11 . .
Example 11. The following C program waits for the character ‘A’ to be input on standard
input:
1 #include <stdio.h>
2
3 int main(void)
4 {