Question
Please write the program in 68k language. Thank you.
Write a program to arrange a sequence of eight numbers in descending order. You can store the numbers in memory before the program is executed by means of the DC.B assembler directive. For example: Array 1 DC.B 1,2,5,4,8,5,4,2 There are many ways of performing this sorting operation. One of the simplest is to search the list for the largest number and put it at the top of the list, then do the same to the remaining numbers, and so on. At the end, display the sorted array on the screen. Use the 68K simulator to test your program.
Explanation / Answer
sorting program in 68000 to sort the given numbers. * min and max indexes of main table to be sorted min equ 0 * $3F = MEMORY window size max equ $3f * Program start address org $1000 * Stack pointer init, IT masking and full speed mode setting lea $7ffe,a7 ori.w #$700,sr andi.w #$7fff,sr * A0 holds start address of table lea $2000,a0 * D0 holds min index move.l #min,d0 * D1 holds max index move.l #max,d1 * Q_SORT subroutine call bsr q_sort * End of program by pseudo monitor call trap #0 ***************************************************************************** Q_SORT equ * * Save min and max indexes in the stack move.w d0,-(a7) move.w d1,-(a7) * D2 = "middle" index = D0 + ((D1 - D0) / 2) = "pivot" index * Why is this formula better than (D1+D0)/2 ? move.w d1,d2 sub.w d0,d2 lsr.w #1,d2 add.w d0,d2 * D3 = table "pivot" element move.b 0(a0,d2.w),d3 * Search for table 1st element > pivot, starting from table top next1 equ * cmp.b 0(a0,d0.w),d3 bls next2 addq.w #1,d0 bra next1 * Search for table 1st element