Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;-----------------------------------------------------------
- ;
- ; Sorting....
- ;
- ;-----------------------------------------------------------
- ; NOTES:
- ;-----------------------------------------------------------
- ORG $8000
- ;-----------------------------------------------------------
- Start:
- ; CLS
- call 3435
- ld a, 2
- call 5633
- ; Sort using the version we want
- ld c, 8
- ld hl, SortedIndex
- call SortPoints
- ; Display the data for the hell of it
- ld b, 8
- ld hl, SortedIndex
- DispLoop:
- ld a, (hl)
- inc hl
- push hl
- push bc
- call DispTeamNameByIdx
- pop bc
- pop hl
- djnz DispLoop
- ret
- ;-----------------------------------------------------------
- DispTeamNameByIdx:
- ; Each entry is 16 bytes so shift up (need 16 bit shift if the data gets bigger than 256 bytes)
- sla a
- sla a
- sla a
- sla a
- ; add the offset
- ld e, a
- ld d, 0
- ld hl, TableData
- add hl, de
- ; Get the name
- inc hl
- inc hl
- inc hl
- ; Write all the names
- ld b, 13
- PrintNameLoop:
- push bc
- ld a, (hl)
- inc hl
- push hl
- rst 16
- pop hl
- pop bc
- djnz PrintNameLoop
- ld a, 13
- rst 16
- ret
- ;-----------------------------------------------------------
- SortPoints:
- ld a, 0
- jp Sort
- SortGoalDiff:
- ld a, 1
- jp Sort
- SortScored:
- ld a, 2
- ; Fall through to sort
- Sort:
- ; Self modify the sort to the write byte
- ld (ByteOffset + 1), a
- ld (Step + 2), hl
- ; The sort routine
- dec c ; Note that the first step involves N-1 checks
- ld hl, 1 ; Setting H=0 and L=1, for optimising speed
- Step:
- ld ix, 0
- ld e, h ; Bit 0 of E will indicate if there was need to swap
- ld b, c ; C holds the number of elements in the current step
- Loop:
- ld a, (ix+0)
- ld d, (ix+1)
- call CompareIndex
- jr c, Continue
- ; Switch them - regrab as overwritten
- ld a, (ix+0)
- ld d, (ix+1)
- ld (ix), d ; Swapping order is actually performed by simply writing
- ld (ix+1), a ; the values back in a reversed order
- ld e, l ; Swapping is indicated here (L=1)
- Continue:
- inc ix
- djnz Loop
- dec e
- ret nz
- dec c
- jr nz, Step
- ret
- ; Used to compare the correct index
- CompareIndex:
- ; Get the values
- ex af, af'
- ld a, d
- call GetIndex
- ld d, a
- ex af, af'
- call GetIndex
- ; Compare them
- cp d
- ret
- ; Gets the sort index in A
- GetIndex:
- push hl
- push de
- sla a
- sla a
- sla a
- sla a
- ByteOffset:
- add a, 0
- ; add the offset
- ld e, a
- ld d, 0
- ld hl, TableData
- add hl, de
- ld a, (hl)
- pop de
- pop hl
- ret
- ;-----------------------------------------------------------
- ; Points, Goal Diff, Scored, Name
- TableData:
- DB 10, 3, 10
- DB "TEAM 1 "
- DB 11, 5, 9
- DB "TEAM 2 "
- DB 17, 22, 21
- DB "TEAM 3 "
- DB 18, 15, 6
- DB "TEAM 4 "
- DB 21, 10, 19
- DB "TEAM 5 "
- DB 9, 3, 19
- DB "TEAM 6 "
- DB 9, 8, 7
- DB "TEAM 7 "
- DB 1, 3, 2
- DB "TEAM 8 "
- ;-----------------------------------------------------------
- SortedIndex:
- DB 0, 1, 2, 3, 4, 5, 6, 7
- ;-----------------------------------------------------------
- END Start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement