Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Heap sort for the armlet
- mov $1, >test_len
- loa $1, $1
- mov $0, >test_data
- sub $0, $0, 1 # index origin is 1
- mov $7, 1 # start heapifying at the origin
- @heapify:
- cmp $7, $1 # done with heapify?
- bab >getmax # ... yes
- mov $6, $7 # current index
- add $3, $0, $6 # address of current
- loa $2, $3 # load current value
- @upheap_loop:
- cmp $6, 1 # at heap top?
- bbe >heapify_next # ... yes
- lsr $3, $6, 1 # index of parent
- add $4, $0, $3 # address of parent
- loa $4, $4 # load parent
- cmp $4, $2 # compare parent and current
- bae >heapify_next # ... heap property holds
- add $5, $0, $6 # address of current
- sto $5, $4 # store parent to current
- mov $6, $3 # move to parent
- jmp >upheap_loop
- @heapify_next:
- add $3, $0, $6 # address of current
- sto $3, $2 # store current value here
- add $7, $7, 1
- jmp >heapify
- @getmax:
- cmp $1, 0 # done with max extraction?
- beq >sort_done # yes
- add $7, $0, 1 # address of heap top
- loa $3, $7 # load heap top
- add $2, $0, $1 # address of last element
- loa $6, $2 # load last element
- sto $2, $3 # heap top goes to last element
- sub $1, $1, 1 # decrease heap size
- mov $7, 1 # start downheap, $6 is current
- @downheap_loop:
- lsl $2, $7, 1 # left child index
- cmp $2, $1 # is there a left child?
- bab >downheap_done # ... no
- add $4, $2, $0 # left child address
- loa $4, $4 # load left child
- add $3, $2, 1 # right child index
- cmp $3, $1 # is there a right child?
- bab >downheap_left # ... no
- add $5, $3, $0 # right child address
- loa $5, $5 # load right child
- cmp $4, $5 # compare children
- bae >downheap_left # ... go left
- @downheap_right:
- cmp $6, $5 # compare current and right
- bae >downheap_done # ... ok to insert at current
- add $2, $0, $7 # current address
- sto $2, $5 # insert right to current
- mov $7, $3 # move to right
- jmp >downheap_loop
- @downheap_left:
- cmp $6, $4 # compare current and left
- bae >downheap_done # ... ok to insert at current
- add $3, $0, $7 # current address
- sto $3, $4 # insert left to current
- mov $7, $2 # move to left
- jmp >downheap_loop
- @downheap_done:
- add $2, $0, $7 # current address
- sto $2, $6 # store current here
- jmp >getmax
- @sort_done:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement