Page 162 - ARM 64 Bit Assembly Language
P. 162

148 Chapter 5


                                     Listing 5.40 Improved 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     w0, #1                 // else return 1
                18        b       return
                19  endif:  mov   w2, #(width*height*i_size) // w2=w*h* size
                20        mov     w3, #0                 // w3 = 0
                21        mov     w1, #0                 // inti=0
                22  loop:  add    x9, x1, x0             // x9 = image + i
                23        strb    w3, [x9, #i_red]       // image[i].red = 0
                24        strb    w3, [x9, #i_green]     // image[i].green = 0
                25        strb    w3, [x9, #i_blue]      // image[i].blue = 0
                26        add     w1, w1, #3             // i += 3
                27        cmp     w1, w2                 // if(i<width*height*3)
                28        blt     loop                   //  repeat loop
                29
                30        bl      free                   // free(image)
                31        mov     w0, #0                 // return 0
                32  return: ldp   x29, x30, [sp], #16
                33        ret
                34        .size   main, (. - main)



                  results of these additional improvements. This third implementation will run much faster than
                  the previous implementations on a very large array, or image.



                  5.6 Chapter summary

                  Spaghetti code is the bane of assembly programming, but it can easily be avoided. Although
                  assembly language does not enforce structured programming, it does provide the low-level
   157   158   159   160   161   162   163   164   165   166   167