; STRSORT.SRC
;
; *******************************
; *         SORT ROUTINE        *
; *                             *
; *       A=USR(A,B,C,D,E)      *
; *                             *
; *      A=ADR OF ROUTINE       *
; *      B=ADR OF STRING        *
; *      C=RECORD LENGTH        *
; *      D=DEPTH OF SORT        *
; *      E=# OF RECORDS         *
; *                             *
; *******************************
;
	*= $5000
;
; EQUATES
; -------
;
SADR	= $CB 	; ADR OF RECORD
HSADR	= $0600 	; ADR OF DATA STRING
RLEN	= $0602 	; RECORD LENGTH
DEEP	= $0603 	; DEPTH OF SEARCH
TEMP	= $0607 	; TEMP DATA BUFFER
NREC	= $0605 	; # OF RECORDS
SSADR	= $CD 	; ADR OF 2ND RECORD
FLG	= $0606 	; SWITCHED FLAG
;
; INITIALIZE
; ----------
;
	PLA  	; # OF ARGUMENTS
	PLA  	; GET STRING ADR HI
	STA HSADR+1
	PLA  	; GET STRING ADR LO
	STA HSADR
	PLA  	; IGNORE HI BYTE
	PLA  	; GET RECORD LENGTH
	STA RLEN
	PLA  	; IGNORE HI BYTE
	PLA  	; GET DEPTH OF SORT
	STA DEEP
	PLA  	; IGNORE HI BYTE
	PLA  	; GET # RECORDS
	STA NREC
;
; MAIN PROGRAM
; ------------
;
START
	LDA HSADR+1 	; BEGINNING
	STA SADR+1 	; OF DATA STRING
	STA SSADR+1
	LDA HSADR
	STA SADR
	CLC 
	ADC RLEN 	; CALCULATE ADR OF
	STA SSADR 	; NEXT HIGHEST RECORD
	BCC START2
	INC SSADR+1
START2
	LDA #$00 	; INITIALIZE FLAG
	STA FLG
	LDX #$01 	; RECORD COUNT
NCMPR
	LDY #$00 	; POINT TO 1ST CHAR
CMPR
	LDA (SADR),Y 	; COMPARE CHARACTERS
	CMP (SSADR),Y 	; IN EACH RECORD
	BCC NEXT 	; IN RIGHT ORDER
	BNE SWITCH 	; IN WRONG ORDER
	INY  	; EQUAL, SO NEXT CHAR
	CPY DEEP 	; REACHED SORT !!EQUA	;!!!!!!!! INVERSE CHARS SPECIAL CHARS !!!!!!!!
	JSR 