Page 178 - ARM 64 Bit Assembly Language
P. 178

Abstract data types  165

                     There are many other possibilities for re-ordering the list. Regardless of which method is
                     chosen, the main program and the interface (header file) need not be changed. Different im-
                     plementations of the sorting function can be substituted without affecting any other code,
                     because the implementation of the ADT is hidden from the code that uses it. This separation
                     of inteface and implementation provides many benefits.

                     The wl_print_numerical function can be implemented in assembly as shown in List-
                     ing 6.8. The function operates by re-ordering the linked list using an insertion sort as de-
                     scribed above. Listing 6.9 shows the change that must be made to the makefile. Now, when
                     make is run, it compiles the two C files and the assembly file into object files, then links them
                     all together. The C implementation of wl_print_numerical in list.c must be deleted
                     or commented out or the linker will emit an error indicating that it found two versions of
                     wl_print_numerical.

                             Listing 6.8 AArch64 assembly implementation of wl_print_numerical.

                    1  ### Definitions for the wordlistnode type
                    2         .equ   wln_word,  0   // word pointer field
                    3         .equ   wln_count, 8   // count field and padding
                    4         .equ   wln_next, 16   // next pointer field
                    5         .equ   wln_size, 24   // sizeof(wordlistnode)
                    6  ### Definitions for the wordlist type
                    7         .equ   wl_nwords, 0   // number of words in list and padding
                    8         .equ   wl_head,   8   // head of linked list
                    9         .equ   wl_size,  16   // sizeof(wordlist)
                   10  ### Define NULL
                   11         .equ   NULL,0
                   12
                   13  ### ----------------------------------------------------------
                   14  ### The sort_numerical function sorts the list of words in
                   15  ### reverse by number of occurrences, and returns a
                   16  ### pointer to the head of the re-ordered list.
                   17  ### Records with identical counts will maintain their
                   18  ### original ordering with respect to each other.
                   19  ### x0 holds head of source list (head)
                   20  ### x1 holds destination list (dest)
                   21  ### x2 holds pointer to node currently being moved (node)
                   22  ### x3 holds pointer to current node in destination list (curr)
                   23  ### x4 holds pointer to previous node in destination list (prev)
                   24  ### w5 holds count of current node in destination list
                   25  ### w6 holds count of node currently being moved
                   26         .type  sort_numerical, %function
                   27  sort_numerical:
                   28         stp    x29, x30, [sp, #-16]!
                   29         mov    x1, #NULL              // initialize new list to NULL
                   30         ## loop until source list is empty
                   31  loopa:  cmp   x0, #NULL
   173   174   175   176   177   178   179   180   181   182   183