Page 161 - ARM 64 Bit Assembly Language
P. 161

Structured programming 147





                                 Listing 5.39 Initializing an array of structured data 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, #(100*100*3)       // precompute w*h*size
                   14         bl     malloc                 // allocate storage.
                   15         cmp    x0, xzr                // if (image != NULL)
                   16         bne    endif                  //   goto endif
                   17         mov    w0, #1                 // else return 1
                   18         b      return
                   19  endif:  mov   w3, #100               // w3 = width
                   20         mov    w4, #100               // w4 = height
                   21         mov    w5, #0                 // w5 = 0
                   22         mov    w1, #0                 // intj=0
                   23  loop1:  mov   w2, #0                 // inti=0
                   24  loop2:  madd  x9, x1, x3, x2         // x9 = j * width + i
                   25         add    x9, x9, x0             // x9 += image address
                   26         strb   w5, [x9, #i_red]       // image[j*w+i].red = 0
                   27         strb   w5, [x9, #i_green]     // image[j*w+i].green = 0
                   28         strb   w5, [x9, #i_blue]      // image[j*w+i].blue = 0
                   29         add    w2, w2, #1             // i++
                   30         cmp    w2, w3                 // if (i < width) goto loop2
                   31         ble    loop2                  // end of loop2
                   32
                   33         add    w1, w1, #1             // j++
                   34         cmp    w1, w4                 //if (j < height) goto loop1
                   35         ble    loop1                  // end of loop1
                   36
                   37         bl     free                   // free(image)
                   38         mov    w0, #0                 // return 0
                   39  return: ldp   x29, x30, [sp], #16
                   40         ret
                   41         .size  main,(. - main)
   156   157   158   159   160   161   162   163   164   165   166