In this program, we will write an 8085 Program to sort the numbers in Descending order in the 8085 microprocessor with a program flow chart and explanation of the program.
8085 Program to sort the numbers in Descending order
Write a program in the assembly language of 8085 to sort the given N words from a block in descending order. Assume that the block begins at D000H.
Explanation of Program
- Consider that a block of N words is present.
- Now we have to arrange these N numbers in descending order, Let N = 4 for example.
- We will use HL as a pointer to point the block of N numbers.
- Initially, in the first iteration, we compare the first number with the second number. If the first number > the second number don’t interchange the contents. If the first number < the second number swap their contents. Now at the end of this iteration first two elements are sorted in descending order.
- In the next iteration, we will compare the first number along with the third. If first > third don’t interchange contents otherwise swap the contents. At the end of this iteration first three elements are sorted in descending order. Go on comparing till all the elements are arranged in descending order. This method requires approximately n comparisons.
Flow Chart of Program
Assembly Language Program
|MVI B, 09H||Initialize counter-1||B = 09 H|
|START:||LXI H, D000H||Initialize memory pointer||H = D0H, L = 00H|
|MVI C, 09H||Initialize counter-2||C = 09 H|
|BACK:||MOV A, M||Get the number in the accumulator||A ← M|
|INX H||Increment memory pointer||HL = HL + 1|
|CMP M||Compare the number with the next number|
|JNC SKIP||if more, don’t interchange|
|JZ SKIP||if equal, don’t interchange|
|MOV D, M||otherwise, swap the content|
|DCX M, A||Interchange numbers||M ← A|
|DCX H||Decrement memory pointer||HL = HL – 1|
|MOV M, D||D ← M|
|INX H||Increment pointer to next memory location||HL = HL + 1|
|SKIP:||DCR C||Decrement counter-2||C = C – 1|
|JNZ BACK||If not zero, repeat|
|DCR B||Decrement counter-1||B = B -1|
|JNZ START||if not zero, repeat|
|HLT||Terminate program execution||Stop|