Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. @R0 contains the low address
  2. @R1 contains the high address
  3. @We assume partition based on last element as pivot
  4. @A better implementation would do a median-of-3 pivot
  5. @to prevent quicksort degenerating from O(n*log(n)) to O(n^2)
  6. @Median-of-3 would also allow us to optimize away 2 bounds checks
  7. @Return position of pivot in R0
  8.  
  9. _hoare_partition:
  10.  
  11. @ save array bounds
  12. MOV R5, R0
  13. MOV R6, R1
  14.  
  15. @ save pivot value
  16. MOV R2, R1
  17. LDR R2, [R2]
  18.  
  19. @ i = lo - 1
  20. SUB R0, R0, #4
  21.  
  22. @ loop forever (until partitioning is done)
  23. _partition_loop_forever:
  24.  
  25. @ do {i = i + 1 } while A[i] < pivot
  26. _first_partition_loop:
  27.  
  28. @ bounds check
  29. @ if (i == right) break
  30. CMP R0, R6
  31. BEQ _second_partition_loop
  32.  
  33. ADD R0, R0, #4
  34. LDR R3, [R0]
  35. CMP R3, R2
  36. BLT _first_partition_loop
  37.  
  38. @ do {j = j - 1} while A[j] > pivot
  39.  
  40. _second_partition_loop:
  41.  
  42. @ bounds check
  43. @ if (j == left) break
  44. CMP R1, R5
  45. BEQ _end_partition
  46.  
  47. SUB R1, R1, #4
  48. LDR R4, [R1]
  49. CMP R4, R2
  50. BGT _second_partition_loop
  51.  
  52. @ if i >= j then return j
  53.  
  54. CMP R0, R1
  55. BGE _end_partition
  56.  
  57. @ swap A[i] with A[j]
  58. STR R3, [R1]
  59. STR R4, [R0]
  60.  
  61. BAL _partition_loop_forever
  62.  
  63. _end_partition:
  64.  
  65. @ swap last element (pivot) into correct place
  66. ADD R1, R1, #4
  67. LDR R7, [R6]
  68. LDR R8, [R1]
  69. STR R7, [R1]
  70. STR R8, [R6]
  71.  
  72. @ return position of pivot in R0 (Linux ABI-compatible)
  73. MOV R0, R1
  74. @ return from function
  75. MOV PC, LR
  76. 22,11 16
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement