Page 155 - ARM 64 Bit Assembly Language
P. 155
Structured programming 141
18 char str[] = "\nThis is the string to reverse\n";
19 printf(str);
20 reverse(str, str + strlen(str)-1);
21 printf(str);
22 return 0;
23 }
Listing 5.33 String reversing in assembly using pointers.
1 .data // Storing in the data section instead of on the stack
2 str: .string "\nThis is the string to reverse\n"
3
4 .text
5 .type reverse, %function
6 .global reverse
7 reverse:stp x29, x30, [sp, #-16]! // push FP and LR
8 // if( left >= right) goto endif
9 cmp x0, x1
10 bge endif
11 ldrb w2, [x0] // w2 = *left
12 ldrb w3, [x1] // w3 = *right
13 strb w3, [x0] // a[left] = w3
14 strb w2, [x1] // a[right] = w2
15 // reverse(a, left+1, right-1)
16 add w0, w0, #1 // left += 1
17 sub w1, w1, #1 // right -= 1
18 bl reverse
19 endif: ldp x29, x30, [sp], #16 // pop FP and LR
20 ret
21 .size reverse, (. - reverse)
22
23 .type main, %function
24 .global main
25 main: stp x29, x30, [sp, #-16]!
26 // printf(str)
27 adr x0, str
28 bl printf
29 // reverse(str, str + strlen(str)-1)
30 adr x0, str
31 bl strlen
32 add x1, x0, #-1
33 adr x0, str
34 add x1, x1, x0
35 bl reverse
36 // printf(str)
37 adr x0, str
38 bl printf
39 // return 0
40 mov w0, #0