Advertisement
Guest User

Sort example - (quickly - badly written)

a guest
Feb 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;-----------------------------------------------------------
  2. ;
  3. ; Sorting....
  4. ;
  5. ;-----------------------------------------------------------
  6. ; NOTES:
  7.  
  8. ;-----------------------------------------------------------
  9.  
  10.                 ORG     $8000
  11.  
  12. ;-----------------------------------------------------------
  13.  
  14. Start:
  15.                 ; CLS
  16.                 call    3435
  17.                 ld      a, 2
  18.                 call    5633
  19.  
  20.                 ; Sort using the version we want
  21.                 ld      c, 8
  22.                 ld      hl, SortedIndex
  23.                 call    SortPoints
  24.  
  25.                 ; Display the data for the hell of it
  26.                 ld      b, 8
  27.                 ld      hl, SortedIndex
  28. DispLoop:
  29.                 ld      a, (hl)
  30.                 inc     hl
  31.                 push    hl
  32.                 push    bc
  33.  
  34.                 call    DispTeamNameByIdx
  35.  
  36.                 pop     bc
  37.                 pop     hl
  38.                    
  39.                 djnz    DispLoop
  40.  
  41.                 ret
  42.  
  43. ;-----------------------------------------------------------
  44.  
  45. DispTeamNameByIdx:
  46.                 ; Each entry is 16 bytes so shift up (need 16 bit shift if the data gets bigger than 256 bytes)
  47.                 sla     a
  48.                 sla     a
  49.                 sla     a
  50.                 sla     a
  51.  
  52.                 ; add the offset
  53.                 ld      e, a
  54.                 ld      d, 0
  55.                 ld      hl, TableData
  56.                 add     hl, de
  57.  
  58.                 ; Get the name
  59.                 inc     hl
  60.                 inc     hl
  61.                 inc     hl
  62.  
  63.                 ; Write all the names
  64.                 ld      b, 13
  65. PrintNameLoop:
  66.                 push    bc
  67.  
  68.                 ld      a, (hl)
  69.                 inc     hl
  70.                 push    hl
  71.  
  72.                 rst     16
  73.  
  74.                 pop     hl
  75.  
  76.                 pop     bc
  77.  
  78.                 djnz    PrintNameLoop
  79.  
  80.                 ld      a, 13
  81.                 rst     16
  82.                 ret
  83.  
  84. ;-----------------------------------------------------------
  85.  
  86. SortPoints:
  87.                 ld      a, 0
  88.                 jp      Sort
  89. SortGoalDiff:
  90.                 ld      a, 1
  91.                 jp      Sort
  92. SortScored:
  93.                 ld      a, 2
  94.  
  95.                 ; Fall through to sort
  96. Sort:
  97.                 ; Self modify the sort to the write byte
  98.                 ld      (ByteOffset + 1), a
  99.                 ld      (Step + 2), hl
  100.  
  101.                 ; The sort routine
  102.                 dec     c ; Note that the first step involves N-1 checks
  103.                 ld      hl, 1 ; Setting H=0 and L=1, for optimising speed
  104. Step:
  105.                 ld      ix, 0
  106.                 ld      e, h ; Bit 0 of E will indicate if there was need to swap
  107.                 ld      b, c ; C holds the number of elements in the current step
  108. Loop:
  109.                 ld      a, (ix+0)
  110.                 ld      d, (ix+1)
  111.                 call    CompareIndex
  112.                 jr      c, Continue
  113.  
  114.                 ; Switch them - regrab as overwritten
  115.                 ld      a, (ix+0)
  116.                 ld      d, (ix+1)
  117.                 ld      (ix), d ; Swapping order is actually performed by simply writing
  118.                 ld      (ix+1), a ; the values back in a reversed order
  119.                 ld      e, l ; Swapping is indicated here (L=1)
  120. Continue:
  121.                 inc     ix
  122.                 djnz    Loop
  123.                 dec     e
  124.                 ret     nz
  125.                 dec c
  126.                 jr      nz, Step
  127.                 ret
  128.  
  129. ; Used to compare the correct index
  130. CompareIndex:
  131.                 ; Get the values
  132.                 ex      af, af'
  133.                 ld      a, d
  134.                 call    GetIndex
  135.                 ld      d, a
  136.                 ex      af, af'
  137.                 call    GetIndex
  138.                
  139.                 ; Compare them
  140.                 cp      d
  141.                 ret
  142.  
  143. ; Gets the sort index in A
  144. GetIndex:      
  145.                 push    hl
  146.                 push    de
  147.                 sla     a
  148.                 sla     a
  149.                 sla     a
  150.                 sla     a
  151.  
  152. ByteOffset:
  153.                 add     a, 0
  154.  
  155.                 ; add the offset
  156.                 ld      e, a
  157.                 ld      d, 0
  158.                 ld      hl, TableData
  159.                 add     hl, de
  160.  
  161.                 ld      a, (hl)
  162.                 pop     de
  163.                 pop     hl
  164.                 ret
  165.  
  166. ;-----------------------------------------------------------
  167. ; Points, Goal Diff, Scored, Name
  168. TableData: 
  169.                 DB      10, 3, 10
  170.                 DB      "TEAM 1       "
  171.  
  172.                 DB      11, 5, 9
  173.                 DB      "TEAM 2       "
  174.  
  175.                 DB      17, 22, 21
  176.                 DB      "TEAM 3       "
  177.  
  178.                 DB      18, 15, 6
  179.                 DB      "TEAM 4       "
  180.  
  181.                 DB      21, 10, 19
  182.                 DB      "TEAM 5       "
  183.  
  184.                 DB      9, 3, 19
  185.                 DB      "TEAM 6       "
  186.  
  187.                 DB      9, 8, 7
  188.                 DB      "TEAM 7       "
  189.  
  190.                 DB      1, 3, 2
  191.                 DB      "TEAM 8       "
  192.  
  193. ;-----------------------------------------------------------
  194.  
  195. SortedIndex:
  196.                 DB      0, 1, 2, 3, 4, 5, 6, 7
  197.  
  198. ;-----------------------------------------------------------
  199.                
  200.                 END Start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement