8085 Program to perform bubble sort in descending order

MicrocontrollerMicroprocessor8085

In this program we will see how to sort a block of bytes in descending order using bubble sorting technique.

Problem Statement

Write8085 Assembly language program to sort numbers in descending order where n number of numbers are stored in consecutive memory locations starting from 8041H and the value of n is available in memory location 8040H (Using BUBBLE sort).

Discussion

In this program we will arrange the numbers in bubble sorting technique. In this sorting technique, it will be executed in different pass. In each pass the smallest number is stored at the end of the list. Here we are taking the numbers from location 8041H to 8046H. The array size is stored at8040H.

Input

AddressData
......
804006
804106
804205
804304
804401
804502
804603
......


Flow Diagram

Program

AddressHEX CodesLabelsMnemonicsComments
800021, 40, 80STARTLXI H, 8040H Pointer to the IN-BUFFER
800316, 00
MVI D, 00H The D register is used as a flag register
80054E 
MOV C, M Initialize reg. C with data count
80060D 
DCR C Set Reg. C for comparison count
800723
INX H Pointing to the next location
80087E CHECKMOV A, M Get the number
800923
INX H Go to next location
800A BE 
CMP M Compare the contents of the current memory location with the contents of the accumulator
800B D2, 14, 80
JNC NEXTBYT If (A) >= second byte, do not exchange
800E 46
MOV B, M Get second byte for exchange
800F 77
MOV M, A Store first byte in second location
80102B 
DCX H Point to first location
801170
MOV M, B Store second byte in first location
801223
INX H Get ready for next comparison
801316, 01
MVI D, 01H Load 1 in D as a remainder for exchange
80150D NEXTBYTDCR C Decrement comparison count
8016C2, 08, 80
JNZ CHECK If comparison count not 0, go back
80197A 
MOV A, D Get flag bit in A
801A 0F 
RRC Place flag bit D0in carry
801B DA, 00, 80
JC START If flag is 1, exchange occurred
801E 76
HLT Terminate the program


Output

AddressData
......
804106
804205
804304
804403
804502
804601
......
raja
Published on 21-Jan-2019 14:08:50
Advertisements