****************************************************** * SORT.SRC * ****************************************************** CODE EQU $8000 ;program starts at $8000 DATA EQU $9000 ;data starts at $9000 ORG CODE ;program at $8000 MOVEA.L #BYTES,A0 MOVE.W #COUNT-1,D7 BSR.S SORT TRAP #14 ****************************************************** * SORT ascending SORT of 8-bit signed bytes * * * * ENTER A0 = address of list * * D7 = length of list * ****************************************************** SORT MOVEA.L A0,A1 ;save pointer LOOP2 MOVEA.L A1,A0 ;reset pointer CLR.W D5 ;use D5 as SWAP flag SUB.W #1,D7 ;number of comparisons MOVE.W D7,D6 ;use D6 within loop LOOP MOVE.B (A0)+,D4 ;get first byte CMP.B (A0),D4 ;compare with next BLE.S SKIP ;if 1st bigger, swap MOVE.B (A0),-1(A0) ;...put 2nd into 1st MOVE.B D4,(A0) ;...put 1st into 2nd ADDQ.W #1,D5 ;set SWAP flag SKIP DBRA D6,LOOP ;if last comparison, TST.W D5 ;any bytes swapped? BNE.S LOOP2 ;yes: repeat DONE RTS ;no: done * * Use DC directives to initialize RAM * ORG DATA ;data at $9000 BYTES DC.B 5,8,2,-1,7 COUNT EQU *-BYTES END SORT