Sunday, September 27, 2026

Sorting an array of data in Ascending order | 8085 Lab Program

 Section: Array Operations & Code Conversions

Aim: To sort the given numbers in the ascending order using 8085 microprocessor.

Apparatus Required: 8085 microprocessor kit, key board.

Algorithm:

  1. Get the numbers to be sorted from the memory locations.
  2. Compare the first two numbers and if the first number is larger than the second, then interchange the numbers.
  3. If the first number is smaller, go to step 4.
  4. Repeat steps 2 and 3 until the numbers are in the required order.

Program:

AddressLabelMnemonicsOperandComment
4100MVIB, 04Initialize B reg with number of comparisons (n-1)
4102LOOP3LXIH, 4200Initialize HL reg. to 4200H
4105MVIC, 04Initialize C reg with no. of comparisons (n-1)
4107LOOP2MOVA, MTransfer first data to acc.
4108INXHIncrement HL reg. to point next memory location
4109CMPMCompare M & A
410AJCLOOP1If A is less than M then go to loop1
410DMOVD, MTransfer data from M to D reg
410EMOVM, ATransfer data from acc to M
410FDCXHDecrement HL pair
4110MOVM, DTransfer data from D to M
4111INXHIncrement HL pair
4112LOOP1DCRCDecrement C reg
4113JNZLOOP2If C is not zero go to loop2
4116DCRBDecrement B reg
4117JNZLOOP3If B is not Zero go to loop3
4118HLTStop the program

Observation:

  • Input:
    • Address 4200: 01
    • Address 4201: 06
    • Address 4202: 03
    • Address 4203: 07
    • Address 4204: 02
  • Output:
    • Address 4200: 01
    • Address 4201: 02
    • Address 4202: 03
    • Address 4203: 06
    • Address 4204: 07

Result: Thus the ascending order program is executed and the numbers are arranged in ascending order.

Flowchart:

                  +-------------------+
                  |       START       |
                  +---------+---------+
                            |
                            v
                  +-------------------+
                  |    [B] <- 04H     |
                  +---------+---------+
                            |
                            v <-------------------------------+
                  +-------------------+                       |
                  |   [HL] <- 4200H   |                       |
                  +---------+---------+                       |
                            |                                 |
                            v                                 |
                  +-------------------+                       |
                  |    [C] <- 04H     |                       |
                  +---------+---------+                       |
                            |                                 |
                            v <-------------------+           |
                  +-------------------+           |           |
                  |    [A] <- [M]     |           |           |
                  +---------+---------+           |           |
                            |                     |           |
                            v                     |           |
                  +-------------------+           |           |
                  |  [HL] <- [HL] + 1 |           |           |
                  +---------+---------+           |           |
                            |                     |           |
                            v                     |           |
                          /   \                   |           |
                        /  Is   \                 |           |
                      / [A] < [M]?\               |           |
                     <  (CY = 1)   >              |           |
                      \           /               |           |
                        \       /                 |           |
                          \   /                   |           |
                            |                     |           |
               +------------+------------+        |           |
               |                         |        |           |
              YES                        NO       |           |
               |                         |        |           |
               |                         v        |           |
               |               +-------------------+          |
               |               |    [D] <- [M]     |          |
               |               |    [M] <- [A]     |          |
               |               |  [HL] <- [HL] - 1 |          |
               |               |    [M] <- [D]     |          |
               |               |  [HL] <- [HL] + 1 |          |
               |               +---------+---------+          |
               |                         |        |           |
               +------------+------------+        |           |
                            |                     |           |
                            v                     |           |
                  +-------------------+           |           |
                  |   [C] <- [C] - 1  |           |           |
                  +---------+---------+           |

|

                            v
                          /   \
                        /  Is   \
                      /  [C] = 0? \
                     <            >
                      \ Is zero? / -- NO ----------> (To LOOP2)
                        \      /
                          \  /
                           | YES
                           v
                 +-------------------+
                 |   [B] <- [B] - 1  |
                 +---------+---------+
                           |
                           v
                         /   \
                       /  Is   \
                     /  [B] = 0? \
                    <            >
                     \ Is zero? / -- NO -----------> (To LOOP3)
                       \      /
                         \  /
                          | YES
                          v
                 +-------------------+
                 |       STOP        |
                 +-------------------+


Viva Questions:

  • Explain the INX operation.

  • State the logic behind sorting an array of data in descending order.

  • What are the advantages of using memory segmentation in 8085?

  • What is a macro and when is it used?

  • What is the function of the direction flag?

  • What is DMA?

  • Define machine cycle and instruction cycle.

For all 2026 published articles list: click here: 

…till the next post, bye-bye & take care