
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selection Sort in Descending Order using 8085
Now let us see a program of Intel 8085 Microprocessor. In this program we will see how to sort a sequence of numbers in reverse order using selection sort.
Problem Statement:
Write 8085 Assembly language program to sort a given sequence using selection sort in descending order. The numbers are stored at 8001H onwards. 8000H is holding the block size.
Discussion:
In the selection sorting technique, we will choose the minimum or the maximum term from a set of numbers. In this case we are considering the sorting in descending order, so we are choosing the maximum number. By taking the maximum number, we are swapping it with the element of currently pointed location. In each pass the maximum number of the sub block is placed at the correct position.
This program has two subroutines. The first subroutine will check the maximum number in a set of numbers, and the second subroutine will be used to swap the numbers.
Input:
Address |
Data |
---|---|
. . . |
. . . |
8000 |
06 |
8001 |
22 |
8002 |
55 |
8003 |
33 |
8004 |
66 |
8005 |
44 |
8006 |
11 |
. . . |
. . . |
Flow Diagram:
Program:
Address |
HEX Codes |
Labels |
Mnemonics |
Comments |
---|---|---|---|---|
F000 |
31, 00, 90 |
LXI SP,9000H |
Initialize Stack Pointer |
|
F003 |
21, 00, 80 |
LXI H,8000H |
Point to get the block size |
|
F006 |
4E |
MOV C,M |
Get the count |
|
F007 |
23 |
DO |
INX H |
Point to next location to get block |
F008 |
7E |
MOV A,M |
Load the element to A |
|
F009 |
CD, 18, F0 |
CALL MAX |
Find the maximum |
|
F00C |
BE |
CMP M |
Compare M and A |
|
F00D |
CA, 13, F0 |
JZ GO |
if Z = 1, they are same, skip swapping |
|
F010 |
CD, 2A, F0 |
CALL SWAP |
Swap minimum and current content |
|
F013 |
0D |
GO |
DCR C |
Decrease C by 1 |
F014 |
C2, 07, F0 |
JNZ DO |
If Z = 0, go to Do |
|
F017 |
76 |
HLT |
Terminate the program |
|
F018 |
E5 |
MAX |
PUSH H |
Push HL into Stack |
F019 |
C5 |
PUSH B |
Push BC into stack |
|
F01A |
0D |
DCR C |
Decrease C by 1 |
|
F01B |
23 |
LOOP |
INX H |
Point to next location |
F01C |
BE |
CMP M |
Compare memory data with A |
|
F01D |
D2, 23, F0 |
JNC SKIP |
If CY = 0, jump to SKIP |
|
F020 |
7E |
MOV A,M |
Update the value of A |
|
F021 |
54 |
MOV D,H |
Copy H to D |
|
F022 |
5D |
MOV E,L |
Copy L to E |
|
F023 |
0D |
SKIP |
DCR C |
Decrease C by 1 |
F024 |
C2, 1B, F0 |
JNZ LOOP |
If Z = 0, go to Loop |
|
F027 |
C1 |
POP B |
Pop BC from stack |
|
F028 |
E1 |
POP H |
Pop HL from stack |
|
F029 |
C9 |
RET |
Return from subroutine |
|
F02A |
F5 |
SWAP |
PUSH PSW |
Store AF into stack |
F02B |
C5 |
PUSH B |
Push BC into stack |
|
F02C |
1A |
LDAX D |
A = get data from location pointed by DE |
|
F02D |
47 |
MOV B,A |
Copy A to B |
|
F02E |
7E |
MOV A,M |
get data from location pointed by HL |
|
F02F |
12 |
STAX D |
Store A content into memory pointed by DE |
|
F030 |
70 |
MOV M,B |
Store B content to memory pointed by HL |
|
F031 |
C1 |
POP B |
Pop BC from Stack |
|
F032 |
F1 |
POP PSW |
Pop AF from stack |
|
F033 |
C9 |
RET |
Return from swap |
Output:
Address |
Data |
---|---|
. . . |
. . . |
8001 |
66 |
8002 |
55 |
8003 |
44 |
8004 |
33 |
8005 |
22 |
8006 |
11 |
. . . |
. . . |