Program for sorting an array for 8086

1.Ascending order

Aim:

To write an assembly language program to arrange the given numbers in ascending   order.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV DX, COUNT - 1
BACK : MOV CX, DX
MOV SI,OFFSET LIST
AGAIN : MOV AX, [SI]
CMP AX, [SI + 2]
JC GO
XCHG AX, [SI + 2]
XCHG AX, [SI]
GO:INC SI
INC SI
LOOP AGAIN
DEC DX
JNZ BACK
HLT
CODE ENDS
DATA SEGMENT
LIST DW 05H, 04H, 01H, 03H, 02H
COUNT EQU 05H
DATA ENDS
END

Flow Chart

Result:

Input:

LIST (DS : 0000H) = 05H, 04H, 01H, 03H, 02H

Output:

LIST (DS : 0000H) = 01H, 02H, 03H, 04H, 05H

 

2. Descending order

Aim:

To write an assembly language program to arrange the given numbers in descending order.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV DX, COUNT - 1
BACK : MOV CX, DX
MOV SI, OFFSET LIST
AGAIN : MOV AX, [SI]
CMP AX, [SI + 2]
JNC GO
XCHG AX, [SI + 2]
XCHG AX, [SI]
GO:INC SI
INC SI
LOOP AGAIN
DEC DX
JNZ BACK
HLT
CODE ENDS
DATA SEGMENT
LIST DW 03H, 04H, 01H, 05H, 02H
COUNT EQU 05H
DATA ENDS
END

Flow Chart:

Result:

Input:

LIST (DS : 0000H) = 03H, 04H, 01H, 05H, 02H

Output:

LIST (DS : 0000H) = 05H, 04H, 03H, 02H, 01H