8086 program to sort an integer array in descending order - GeeksforGeeks

29 Jun, 2018
Problem – Write a program in 8086 microprocessor to sort numbers in descending order in an array of n numbers, where size “n” is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501.
Example –
Example explanation:
Pass-1: 
32 05 14 50
32 05 14 50
32 14 05 50
32 14 50 05 (1 number got fix)

Pass-2:  
32 14 50 05
32 14 50 05
32 50 14 05 (2 number got fix)

Pass-3: 
32 50 14 05
50 32 14 05 (sorted)
Pass-1: 
32 05 14 50
32 05 14 50
32 14 05 50
32 14 50 05 (1 number got fix)


Pass-2:  
32 14 50 05
32 14 50 05
32 50 14 05 (2 number got fix)


Pass-3: 
32 50 14 05
50 32 14 05 (sorted)
Algorithm –
Load data from offset 500 to register CL (for count).Travel from starting memory location to last and compare two numbers if first number is smaller than second number then swap them.First pass fix the position for last number.Decrease the count by 1.Again travel from starting memory location to (last-1, by help of count) and compare two numbers if first number is smaller than second number then swap them.Second pass fix the position for last two numbers.Repeate.
Load data from offset 500 to register CL (for count).
Travel from starting memory location to last and compare two numbers if first number is smaller than second number then swap them.
First pass fix the position for last number.
Decrease the count by 1.
Again travel from starting memory location to (last-1, by help of count) and compare two numbers if first number is smaller than second number then swap them.
Second pass fix the position for last two numbers.
Repeate.
Program –
Explanation –
MOV SI, 500: set the value of SI to 500.MOV CL, [SI]: load data from offset SI to register CL.DEC CL: decrease value of register CL BY 1.MOV SI, 500: set the value of SI to 500.MOV CH, [SI]: load data from offset SI to register CH.DEC CH: decrease value of register CH BY 1.INC SI: increase value of SI BY 1.MOV AL, [SI]: load value from offset SI to register AL.INC SI: increase value of SI BY 1.CMP AL, [SI]: compares value of register AL and [SI] (AL-[SI]).JNC 41C: jump to address 41C if carry not generated.XCHG AL, [SI]: exchange the contents of register AL and SI.DEC SI: decrease value of SI by 1.XCHG AL, [SI]: exchange the contents of register AL and SI.INC SI: increase value of SI by 1.DEC CH: decrease value of register CH by 1.JNZ 40F: jump to address 40F if zero flat reset.DEC CL: decrease value of register CL by 1.JNZ 407: jump to address 407 if zero flat reset.HLT: stop.
MOV SI, 500: set the value of SI to 500.
MOV CL, [SI]: load data from offset SI to register CL.
DEC CL: decrease value of register CL BY 1.
MOV SI, 500: set the value of SI to 500.
MOV CH, [SI]: load data from offset SI to register CH.
DEC CH: decrease value of register CH BY 1.
INC SI: increase value of SI BY 1.
MOV AL, [SI]: load value from offset SI to register AL.
INC SI: increase value of SI BY 1.
CMP AL, [SI]: compares value of register AL and [SI] (AL-[SI]).
JNC 41C: jump to address 41C if carry not generated.
XCHG AL, [SI]: exchange the contents of register AL and SI.
DEC SI: decrease value of SI by 1.
XCHG AL, [SI]: exchange the contents of register AL and SI.
INC SI: increase value of SI by 1.
DEC CH: decrease value of register CH by 1.
JNZ 40F: jump to address 40F if zero flat reset.
DEC CL: decrease value of register CL by 1.
JNZ 407: jump to address 407 if zero flat reset.
HLT: stop.
microprocessor
system-programming
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Memory Hierarchy Design and its Characteristics
8085 program to add two 8 bit numbers
Architecture of 8085 microprocessor
Pin diagram of 8086 microprocessor
Computer Organization and Architecture | Pipelining | Set 1 (Execution, Stages and Throughput)
Architecture of 8086
Computer Organization | RISC and CISC
Memory mapped I/O and Isolated I/O
Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard)
Programmable peripheral interface 8255