KillianMills

selectionSort_1457cycles_0stalls.s

Oct 24th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. ;
  2. ; Sort values a[0] through a[N-1] (inclusive) using selection sort. Run the
  3. ; program and observe that the array (beginning in memory at a0) is sorted.
  4. ;
  5. ; Initially:
  6. ; - 2249 cycles, with 231 stalls.
  7. ;
  8. ; Known to be achievable:
  9. ; - 1248 cycles, with 0 stalls.
  10. ;
  11. ; You should not fundamentally change the selection sort algorithm being used.
  12. ; You may, however, remove any obvious inefficiencies in the implementation.
  13. ; In fact, this may be a good place to start.
  14. ;
  15. ; *** Ensure forwarding and the branch delay slot are both enabled. ***
  16. ;
  17.  
  18. .data
  19. N: .word 20
  20.  
  21. a0: .word 23 ; a[0]
  22. .word 12 ; a[1]
  23. .word 19 ; a[2]
  24. .word 9 ; .
  25. .word 98 ; .
  26. .word 4 ; .
  27. .word 7
  28. .word 9
  29. .word 30405
  30. .word 21
  31. .word 16288
  32. .word 26483
  33. .word 9982
  34. .word 261
  35. .word 5025
  36. .word 18825
  37. .word 30405
  38. .word 9575
  39. .word 9575
  40. .word 25247 ; a[19]
  41.  
  42. ;
  43. ; Rough approach:
  44. ;
  45. ; for (i=0; i<N; i+=1)
  46. ; {
  47. ; int mi = i; // mi = minimum index.
  48. ; int j;
  49. ;
  50. ; for (j=i+1; j<N; j+=1)
  51. ; if ( a[j] < a[mi] )
  52. ; mi = j;
  53. ;
  54. ; // swap a[i] <-> a[mi]
  55. ; int t = a[mi];
  56. ; a[mi] = a[i];
  57. ; a[i] = t;
  58. ; }
  59. ;
  60.  
  61. .text
  62. ; r1 is the address of the i-th element (like main loop counter, address of a[i])
  63. ; r2 is the address after the last element in array (address of a[N], after end of array)
  64. ; r3 is the address of a candidate minimum value during the inner loop (address of a[mi])
  65. ; r4 is the address of the j-th element (like inner loop counter, address of a[j])
  66.  
  67. start: ; set up r1 (like i = 0)
  68. daddi r1,r0,a0 ; r1 = address of a[0]
  69. ld r8,N(r0) ; r8 = N
  70. ; set up r2
  71. daddi r2,r0,a0 ; r2 = address of a[0]
  72.  
  73. dsll r8,r8,3 ; r8 = r8 * 8 (because there are 8 bytes per 64-bit word)
  74. dadd r2,r2,r8 ; r2 = r2 + r8, address of a[N]
  75. daddi r9, r2, -8
  76.  
  77. main: ; start of the outer loop
  78. daddi r4,r1,8 ; r4 = r1, like j = i+1
  79. daddi r3,r1,0 ; r3 = r1 (position of minimum element), like mi = i
  80.  
  81. ld r11,0(r3) ; r11 = a[mi]
  82. ld r10,0(r4) ; r10 = a[j] ;; ;;
  83.  
  84. loop: ; start of the inner loop ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;
  85. beq r4,r2,done_loop ; done if j == N ;;
  86. ;;
  87. daddi r13,r4,0 ; save (in r13) the current value of j ;;
  88. ;;
  89. slt r12,r10,r11 ; set r12 if a[j] < a[mi]; set r12 if found new minimum ;;
  90. daddi r4,r4,8 ; like j = j + 1
  91. beqz r12,loop ; next iteration of inner loop unless found new minimum ;;
  92. ld r10,0(r4) ; r10 = a[j] ;;;nop ; branch delay slot ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;
  93. ;;
  94. daddi r3,r13,0 ; r3 = r13, like mi = j ;;
  95. j loop ; next iteration of inner loop ;;
  96. ld r11,0(r3) ; r11 = a[mi] ;;nop ; branch delay slot ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;
  97.  
  98. done_loop: ; done inner loop, swap elements, swap a[i] with a[mi]
  99. ld r10,0(r1) ; r10 = a[i]
  100. sd r11,0(r1) ; a[i] = r11, which is old a[mi]
  101. daddi r1,r1,8 ; r1 = r1 + 8, like i = i + 1
  102. ld r11,0(r3) ; r11 = a[mi]
  103.  
  104.  
  105.  
  106. next_main: ; move on to the next iteration of the outer loop
  107. bne r1,r9,main ; loop back to the main loop unless i == N
  108. sd r10,0(r3) ; a[mi] = r10, which is old a[i] ;nop ; branch delay slot
  109.  
  110. done: ; done
  111. halt ; halt
Advertisement
Add Comment
Please, Sign In to add comment