; Last revision 8/25/91, Howard Goldstein ; Previous revision 6/4/91, Bruce Morgen ; SHELL-METZNER SORT ; Call with the following information: ; ; BC = Number of records to be sorted ; DE = Record length ; HL = Buffer address ; ; Comment out the following line if subroutine is ; to be yanked into a file instead of using RMAC/LINK ; public sort ; false equ 0 true equ not false ; ascend equ true ; Set FALSE for descending sort striphi equ true ; True, if high bits not significant ; sort: ld (sstadr),hl push hl ld (sreclen),de push de ld (snumrect),bc ld (snumrecw),bc ; ; NOW DIVIDE # OF FIELDS BY 2 ; divide: ld hl,(snumrecw) ; GET VALUE srl h rr l ld (snumrecw),hl ; SAVE RESULT ld a,l ; IF SNUMRECW<>0 or h ; THEN jr nz,notdone ; NOT DONE ; ; ALL FIELDS SORTED ; pop bc ; CLEAN UP STACK pop de ret ; notdone: ex de,hl ld hl,(snumrect) sbc hl,de ld (sreclen),hl ld hl,1 ld (ssortv1),hl ld (sstadr),hl dec l pop bc push bc ndone1: add hl,de dec bc ld a,b or c jr nz,ndone1 ld (ssortv2),hl ex de,hl pop bc pop hl push hl push bc ndone2: ld (ssortv4),hl ld (ssortv3),hl ex de,hl add hl,de ex de,hl compare: pop bc push bc compar1: ld a,(de) if striphi and 7fh push bc push af ld a,(hl) and 7fh ld b,a pop af sub b pop bc else sub (hl) endif jr nz,notequ inc hl inc de dec bc ld a,b or c jr nz,compar1 ; fall through noswitch: ld hl,(sstadr) inc hl ld (sstadr),hl ld (ssortv1),hl ex de,hl ld hl,(sreclen) or a sbc hl,de jr c,divide ld hl,(ssortv4) pop de push de add hl,de ld de,(ssortv2) jr ndone2 ; notequ: if ascend jr nc,noswitch else jr c,noswitch endif ; switch: push bc ld b,(hl) ld a,(de) ld (hl),a ld a,b ld (de),a inc hl inc de pop bc dec bc ld a,b or c jr nz,switch ld de,(snumrecw) ld hl,(ssortv1) scf sbc hl,de jr c,noswitch inc hl ld (ssortv1),hl ld de,(ssortv3) ld hl,(ssortv2) ld a,e sub l ld l,a ld a,d sbc a,h ld h,a ld (ssortv3),hl jr compare ; ;..... dseg ; sreclen: ds 2 sstadr: ds 2 ssortv1: ds 2 ssortv2: ds 2 ssortv3: ds 2 ssortv4: ds 2 snumrect: ds 2 snumrecw: ds 2 end