home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / directsel.lha / direct_selection.S < prev    next >
Encoding:
Text File  |  1987-01-01  |  1.7 KB  |  57 lines

  1. ; from disc # 5
  2. ; ----------------------------------------------------------------
  3. ; --- implementation of the direct-selection sorting algorithm ---
  4. ; --- done by JPN/Level 4-ASD in August 1990 (assembly version)---
  5. ; --- This is the non-optimized version                        ---
  6. ; --- program sorts data sized as words from 'data_list'       ---
  7. ; --- program processes 16 bit signed data (-32768 -> +32767)  ---
  8. ; --- 'num_elements' features the number of elements to sort   ---
  9. ; --- routine length : 64 ($40) bytes                          ---
  10. ; --- any questions? Call Germany: (0)631/43752 (KRIS)         ---
  11. ; ----------------------------------------------------------------
  12.  
  13. org    $40000
  14. load    $40000
  15.  
  16. num_elements=100
  17.  
  18. a:
  19.         lea    data_list,a0        ; table for elements
  20.         moveq    #00,d0            ; outer loop counter (x)
  21. outer:    
  22.         move.w    d0,d1            ; inner loop counter (y)
  23.         move.w    d0,d2            ; index for smallest (j=x)
  24. inner:                    
  25.         move.w    (a0,d1.w),d3        ; a(y) -> d3        
  26.         cmp.w    (a0,d2.w),d3        ; a(j) < a(y) ???
  27.         bgt    smaller            
  28.         move.w    d1,d2            ; j=y
  29. smaller:                
  30.         addq.w    #02,d1            ; next y value
  31.         cmp.w    #num_elements*2,d1    ; end of inner loop ?
  32.         bne    inner            ; next y
  33.         move.w    (a0,d2.w),d3        ; a(j) -> d3
  34.         move.w    (a0,d0.w),d4        ; a(x) => d4
  35.         move.w    d3,(a0,d0.w)        ; exchange    
  36.         move.w    d4,(a0,d2.w)        ; the numbers (d3, d4)
  37.         addq.w    #02,d0            ; next x value
  38.         cmp.w    #num_elements*2,d0    ; end of outer loop
  39.         bne    outer            ; next x
  40.         rts            
  41. end_of_routine:
  42.  
  43. data_list:
  44.     dc.w 5,2,3,1,6,8,9,7,4,0
  45.     dc.w 5,2,3,-1,6,8,9,7,4,0
  46.     dc.w 5,2,3,-2,6,8,9,7,4,0
  47.     dc.w 5,2,3,1,6,8,9,7,4,0
  48.     dc.w 5,2,3,1,6,8,9,7,4,0
  49.     dc.w 5,2,3,1,6,8,9,7,4,0
  50.     dc.w 5,2,3,1,6,8,9,7,4,0
  51.     dc.w 5,2,3,1,6,8,9,7,4,0
  52.     dc.w 5,2,3,1,6,8,9,7,4,0
  53.     dc.w 5,2,3,1,6,8,9,7,4,0
  54.  
  55.  
  56.  
  57.