****************************************************************************
* THIS IS NOT A STANDALONE SOURCE!
* You can compile and assemble it, but you can't run it!!!
*
* The procedures below execute the sorting on a vector of 32bit signed inte-
* gers using the MergeSort algorithm.
*
* Note that these are meant to be examples only, so optimization has been
* sacrificed to enhance the readability (btw: ESA isn't indicated at all
* for this kinda things...).
*
* To use them you need an integers vector of any length and an auxiliary
* vector of the same length (size in bytes = number of integers * 4).
****************************************************************************

****************************************************************************
* MergeSort v1.0.1
****************************************************************************
* INFO	sorts in ascending order a vector of signed long integers
* SYN	MergeSort[SouVec,AuxVec,FEI,LEI]
*	          a0     a1     d0  d1
* IN	SouVec	ptr to vector to sort
*	AuxVec	adr of auxiliary vector
*	FEI	First Element Index (tipically 0)
*	LEI	Last Element Index (tipically # of integers-1)
* NOTE	vector elems are .l
****************************************************************************

	procedure MergeSort[a0-a1/d0-d1],d2-d3
	when.s d0«d1

	 move.l	d0,d3
	 add.l	d1,d3
	 lsr.l	#1,d3	;(FEI+LEI)/2
	 MergeSort[sav:a0,a1,d0,d3]

	 addq.l	#1,d3	;(FEI+LEI)/2+1
	 MergeSort[sav:a0,a1,d3,d1]

	 move.l	d1,d2
	 Merge[sav:a0,a1,d0,d3,d2]

	ewhen
	eproc

****************************************************************************
* Merge v1.0.1
****************************************************************************
* INFO	core of MergeSort algo
* SYN	Merge[SouVec,AuxVec,FHI,SHI,LEI]
*	      a0     a1     d0  d1  d2
* IN	SouVec	ptr to vector to sort
*	AuxVec	ptr to auxiliary vector
*	FHI	First Half Index
*	SHI	Second Half Index
*	LEI	Last Elem Index
****************************************************************************

	procedure	Merge[a0-a1/d0-d2],a0/d3-d7
	move.l	d0,d3	;i=FHI
	move.l	d0,d4	;FHI
	move.l	d1,d5	;SHI

	while.s d3«=d2	;while i<=LEI

	 when.s d4=d1	;if FH elems over
                  move.l	(a0,d5.l*4),(a1,d3.l*4)	;AuxVec[i]=SouVec[SHI]
	  addq.l	#1,d5	;inc SHI
	 owhen  d5»d2	;if SH elems over
	  move.l	(a0,d4.l*4),(a1,d3.l*4)	;AuxVec[i]=SouVec[FHI]
	  addq.l	#1,d4	;inc FHI
	 othw
	  move.l	(a0,d4.l*4),d6	;SouVec[FHI]
	  move.l	(a0,d5.l*4),d7	;SouVec[SHI]

	  when.s d6<=d7	;if SouVec[FHI]<=SouVec[SHI]
	   move.l	d6,(a1,d3.l*4)	;AuxVec[i]=SouVec[FHI]
	   addq.l	#1,d4	;inc FHI
	  othw		;if SouVec[FHI]<SouVec[SHI]
	   move.l	d7,(a1,d3.l*4)	;AuxVec[i]=SouVec[SHI]
	   addq.l	#1,d5	;inc SHI
	  ewhen

	 ewhen

	 addq.l	#1,d3	;inc i
	ewhile

	for d3 = d0 upto d2
	 move.l	(a1,d3.l*4),(a0,d3.l*4)
	next.s

	eproc
