8085 Program to Arrange an Array of Data in Descending Order

Problem:

Write a microprocessor 8085 program to arrange an array of data in descending order. The length of the block is in memory location 4200H and the block itself starts from memory location 4201H.

Arrange the numbers in descending order and store them at memory location from 4201H. Assume that the numbers in the block are all 8-bit unsigned binary numbers.

Algorithm:

  1. Initialize HL pair as memory pointer
  2. Get the count at 4200 into C – register
  3. Copy it in D – register (for bubble sort (N-1) times required)
  4. Get the first value in accumulator.
  5. Compare it with the value at next location.
  6. If they are out of order, exchange the contents of accumulator and Memory.
  7. Decrement D –register content by 1.
  8. Repeat steps 5 and 7 till the value in D- register become zero.
  9. Decrement C –register content by 1.
  10. Repeat steps 3 to 9 till the value in C – register becomes zero.

Program:

LXI H, 4200H

MOV C, M

DCR C

REPEAT: MOV D, C

LXI H, 2201H LOOP: MOV A, M

INX H

CMP M

JNC SKIP

MOV B, M

MOV M, A

DCX H

MOV M, B

INX H

SKIP: DCR D

JNZ LOOP

DCR C

JNZ REPEAT

HLT

Sample Example:

Input:

4200 05 (Array Size)

4201 45

4202 DE

4203 17

4204 20

4205 50

Output:

4200 05(Array Size)

4201 DE

4202 50

4203 45

4204 20

4205 17

Recent posts

Leave a Comment

Your email address will not be published. Required fields are marked *