Page 163 - ARM 64 Bit Assembly Language
P. 163
Structured programming 149
Listing 5.41 Very efficient initialization in assembly.
1 .equ i_red, 0
2 .equ i_green, 1
3 .equ i_blue, 2
4 .equ i_size, 3
5 .equ width, 100
6 .equ height, 100
7
8 .text
9 .type main, %function
10 .global main
11 main: stp x29, x30, [sp, #-16]!
12 // Call malloc to allocate the array of pixels
13 mov x0, #(width*height*i_size) // calculate space needed
14 bl malloc // allocate storage.
15 cmp x0, #0 // if (image != NULL)
16 bne endif // goto endif
17 mov x0, #1 // else return 1
18 b return
19 endif: mov x3, #0
20 mov x4, #0
21 mov x2, #(width*height*i_size) // w2=w*h*size
22 add x2, x2, x0 // get pointer to end
23 // move end addr back to quad boundary
24 orr x1, x2, #0xF // get # of leftover bytes
25 sub x2, x2, x1
26 mov x9, x0 // x9 = i = image
27 loop: stp x3, x4, [x9], #16 // clear quadword and i+=16
28 cmp x9, x2 // while (i < end-1)
29 blt loop // goto loop
30
31 // clear leftover bytes
32 loop2: strb x3, [x9], #1 // clear byte; i += 1
33 subs x1,x1,#1
34 blt loop2
35
36 done: bl free // free(image)
37 mov x0, #0 // return 0
38 return: ldp x29, x30, [sp], #16
39 ret
40 .size main, (. - main)
mechanisms required to write structured programs. The assembly programmer must be aware
of, and assiduously practice, proper structured programming techniques. The burden of writ-
ing properly structured code blocks, with selection structures and iteration structures, lies with