link00seven 0 Newbie Poster

Hi all,

I have been asked for a homework assignment to write, in Motorola 68K Assembly, an algorithm to sort an array and place the sorted array into another array.

I have absolutely no idea how to do this...I have been working on it but i'm having a hard time trying to piece things together.

If anyone could nudge me along, I would really appreciate. I've attached both the sorting algorithm given (which I believe is an insertion sort), and the code that I've written so far.

Thank you!!

The Sort (in C)

/* a[0] to a[n-1] is the array to sort, n=8 */
int iPos;
int iMin;
/* advance the position through the entire array */
/* (could do iPos < n-1 because single element is also min element) */
for (iPos = 0; iPos < n; iPos++)
{
/* find the min element in the unsorted a[iPos .. n-1] */
/* assume the min is the first element */
iMin = iPos;
/* test against all other elements */
for (i = iPos+1; i < n; i++)
{
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin])
{
/* found new minimum; remember its index */
iMin = i;
}
}
/* iMin is the index of the minimum element. Swap it with the current

The Assembly

ORG	$1000
START:	 ; first instruction of program

MOVEA	#Array1,A0	; copies array into A0
MOVEA	#SrtdArray,A1	; copies the blank array into A1
MOVE.B	#0,D0	 ; iPos
MOVE.B	#0,D1	 ; iMin
MOVE.B	#0,D2	 ; i
MOVE.B	#8,D3	 ; n = 8
MOVE.B	#0,D4	 ; temp variable for swap
MOVE.B	#0,D5	 ; temp variable for move

OUTER_LOOP:
CMP	D3,D0	 ; if iPos == 8, program ends
BEQ	END	 ; if the iPos = n, stop
MOVE.B	(A0),D1	 ; assume that min == pos

INNER_LOOP:
MOVE.B	D0,D2
ADD	#1,D2
MOVE.B	(A0),D5
CMP	D5,D2
BRA	SET_MIN

SET_MIN:
MOVE.B	D2,(A1)
BRA	INCREMENT

INCREMENT:
ADDA	#1,A0	 ; points to the next element in the array
ADDA	#1,A1
ADD	#1,D2	 ; increment the counter
ADD	#1,D0
BRA	OUTER_LOOP

END:
MOVE.B	#9,D0
TRAP	#15	 ; halt simulator

* Variables and Strings
Array1	 DC.B	6,4,3,7,8,5,1,2	; Array to be sorted
SrtdArray	DC.B	0,0,0,0,0,0,0,0 ; will be sorted array


END	START	 ; last line of source