8085 Program to sort the numbers in ascending order

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

8085 Program to sort the numbers in ascending order

Assembly Language Program

LabelInstructionCommentOperation
MVI B, 09Initialize counter-1B = 09 H
STARTLXI H, D000Hinitialize memory pointerH = D0H, L = 00H
MVI C, 09HInitialize counter-2C = 09H
Back:MOV A, MGet the number in accumulatorA ← M
INX HIncrement memory pointerHL = HL + 1
CMP MCompare the number with the next numberCompare
JC SKIPif less, don’t interchangeif A < M, don’t interchange
JZ SKIPif equal, don’t interchangeif A = M don’t interchange
MOV D, Motherwise, swap the contentsD ← M
MOV M, AM ← A
DCX Hinterchange numberHL = HL – 1
MOV M, DM ← D
INX Hincrement pointer to the next memoryHL = HL + 1
SKIP:DCR CDecreemnt counter-2C = C – 1
JNZ BACKif not zero, repeat
DCR Bdecrement counter-1B = B -1
JNZ STARTif not zero, repeat
HLTTerminate program executionStop

Also Learn other 8085 Programs

Sr. No.Name of the 8085 Programs
1.Write a program to load the data into the accumulator and any register.
2.Write a program to exchange the contents of memory locations.
3.Add two 8-bit numbers.
4.Subtract two 8-bit numbers.
5.Add two 16-bit numbers.
6.Subtract two 16-bit numbers
7.Find the 1’s complement of a Number
8.Find the 2’s complement of a Number
9.8085 Program To Mask The Lower Nibble
10.8085 Program To Mask The Upper Nibble
11.8085 program to pack the two unpacked BCD numbers
12.8085 program to Unpack the two packed BCD numbers
13.8085 program to find the sum of a series of 8-bit numbers
14.8085 Program to sort the numbers in ascending order
15.8085 Program to sort the numbers in Descending order
16.8085 Program to Multiply Two 8-bit Numbers
17.8085 Program to Divide 16-bit number by 8-bit number
18.8085 Program to Find the Number of Negative Numbers in Array
19.8085 Program to Find Maximum Number in Array
20.8085 Program to count the number of 1’s in a register

Hello friends, my name is Trupal Bhavsar, I am the Writer and Founder of this blog. I am Electronics Engineer(2014 pass out), Currently working as Junior Telecom Officer(B.S.N.L.) also I do Project Development, PCB designing and Teaching of Electronics Subjects.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

telegram logo Join Our Telegram Group!