; 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 ; TRUE equ 255 FALSE equ 0 ; ascend equ TRUE ; Set FALSE for descending sort striphi equ FALSE ; True, if high bits not significant ; sort: ld (sstadr),hl push hl ld (sreclen),de push de ld (snumrt),bc ld (snumrw),bc ; ; Now divide # of fields by 2 ; divide: ld hl,(snumrw) ; Get value srl h rr l ld (snumrw),hl ; Save result ld a,l ; If snumrw<>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,(snumrt) or a sbc hl,de ld (sreclen),hl ld hl,1 ld (sortv1),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 (sortv2),hl ex de,hl pop bc pop hl push hl push bc ndone2: ld (sortv4),hl ld (sortv3),hl ex de,hl add hl,de ex de,hl compre: pop bc push bc compr1: 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,compr1 jr noswitch ; 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 hl,(snumrw) ld a,h cpl ld d,a ld a,l cpl ld e,a ld hl,(sortv1) add hl,de jr nc,noswitch inc hl ld (sortv1),hl ld de,(sortv3) ld hl,(sortv2) ld a,e sub l ld l,a ld a,d sbc a,h ld h,a ld (sortv3),hl jr compre ; noswitch: ld hl,(sstadr) inc hl ld (sstadr),hl ld (sortv1),hl ex de,hl ld hl,(sreclen) or a sbc hl,de jr c,divide ld hl,(sortv4) pop de push de add hl,de ld de,(sortv2) jr ndone2 ; sreclen: dw 0 sstadr: dw 0 sortv1: dw 0 sortv2: dw 0 sortv3: dw 0 sortv4: dw 0 snumrt: dw 0 snumrw: dw 0 end