Page 179 - ARM 64 Bit Assembly Language
P. 179
166 Chapter 6
32 beq endloopa
33 ## detatch first node from source list
34 mov x2, x0 // node <- head
35 ldr w5, [x2, #wln_count] // load count for node
36 ldr x0, [x0, #wln_next] // head <- head->next
37 ## find location to insert into destination list
38 mov x3, x1 // curr <- dest
39 mov x4, #NULL // prev <- NULL
40 loopb: cmp x3, #NULL // Reached end of list?
41 beq found
42 ldr w6, [x3, #wln_count] // load count for curr
43 cmp w5, w6 // compare with count for node
44 bgt found
45 mov x4, x3 // previous <- current
46 ldr x3, [x3, #wln_next] // current <- current->next
47 b loopb
48 ## insert into destination list at current location
49 found: str x3, [x2, #wln_next] // node-> next <- current
50 cmp x4, #NULL // if prev == NULL
51 ## repeat with next list item
52 csel x1, x2, x1, eq // dest <- node
53 beq loopa
54 str x2, [x4, #wln_next] // else prev->next <- node
55 b loopa
56 endloopa:
57 mov x0, x1 // return dest (sorted list)
58 ldp x29, x30, [sp], #16
59 ret
60 .size sort_numerical, (. - sort_numerical)
61 ### ----------------------------------------------------------
62 ### wl_print_numerical prints a table showing the number
63 ### of occurrences for each word, followed by the word,
64 ### sorted in reverse order of occurence.
65 .type wl_print_numerical, %function
66 .global wl_print_numerical
67 wl_print_numerical:
68 stp x29, x30, [sp, #-32]!
69 str x0, [sp, #16] // save pointer to wordlist
70 ldr x0, [x0, #wl_head] // load pointer to the linked list
71 bl sort_numerical // re-sort the list
72 ldr x1, [sp, #16] // load pointer to wordlist
73 str x0, [x1, #wl_head] // update list pointer
74 mov x0, x1 // prepare to print
75 bl wl_print // print the sorted list
76 ldp x29, x30, [sp], #32 // restore FP & LR
77 ret // return
78 .size wl_print_numerical, (. - wl_print_numerical)