In this program, we will write an 8085 Program to sort the numbers in ascending order in the 8085 microprocessor with a program flow chart and explanation of the program.
8085 Program to sort the numbers in ascending order
Program Statement
Write an assembly language program of 8085 to sort the given N numbers from a block in ascending order. Assume that the memory block begins at D000H.
Explanation of Program
Consider that a block of N words is present. Now we have to arrange these N words in ascending order, Let N = 4 for example. We will use HL as a pointer to point the block of N words.
Initially, in the first iteration, we compare the first number with the second number. If the first number < second number, don’t interchange the contents, otherwise if the first number > second number swap the contents.
In the next iteration, we go on comparing the first number with the third number. If the first number < the third number, don’t interchange the contents. If the first number > the third number then swapping will be done.
Since the first two numbers are in ascending order the third number will go to first place, the first number in second place, and the second number will come in third place in the second iteration only if the first number > the third number.
In the next iteration, the first number is compared with the fourth number. So comparisons are done till all N numbers are arranged in ascending order. This method requires approximately n comparisons.
Flow Chart of Program
Assembly Language Program
Label | Instruction | Comment | Operation |
---|---|---|---|
MVI B, 09 | Initialize counter-1 | B = 09 H | |
START | LXI H, D000H | initialize memory pointer | H = D0H, L = 00H |
MVI C, 09H | Initialize counter-2 | C = 09H | |
Back: | MOV A, M | Get the number in accumulator | A ← M |
INX H | Increment memory pointer | HL = HL + 1 | |
CMP M | Compare the number with the next number | Compare | |
JC SKIP | if less, don’t interchange | if A < M, don’t interchange | |
JZ SKIP | if equal, don’t interchange | if A = M don’t interchange | |
MOV D, M | otherwise, swap the contents | D ← M | |
MOV M, A | M ← A | ||
DCX H | interchange number | HL = HL – 1 | |
MOV M, D | M ← D | ||
INX H | increment pointer to the next memory | HL = HL + 1 | |
SKIP: | DCR C | Decreemnt 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 |
Also Learn other 8085 Programs