You are on page 1of 5

Register No.

: 20BCE1734 Name: Mridul Jadon

Date: <27-01-2022> Exp.<3> <SORT ARRAY AND FIND OUT LARGEST


AND SMALLEST ELEMENT>

Aim: To sort a 15 8 bit-array in both ascending and descending order and


finding the smallest and the largest element.

Tool Used:

Assembler - MASM 611

Algorithm:

1. Initialise data segment by creating 8 bit 15 numbers array.


2. Also initialise smallest and largest number by using defined bit (DB ?).
3. Initialise code segment.
4. Move data to AX register.
5. Set DX as count-1 that is 0fh-1
6. Create a label loop 1 to move cx to dx.
7. Load the address of array to SI.
8. Create another label loop2 to move the address to AL.
9. Compare AL, and [SI+1]
10.exchange the contents of Al with SI
11.Decrement DX
12.If DX=0, JUMP TO LOOP 1
13.Similarly perform for descending order
14.Load the address of the first member of ascending order sorted array to
the variable small defined in the data segment .
15.Load the address of the first member of descending order sorted array
to the variable defined large in the data segment.
16.Halt the program.
17.The code ends
18.End start.

Program:

ASSUME CS:CODE, DS:DATA


Register No.: Name:

DATA SEGMENT

ARRAY DB
63H,71H,97H,22H,97H,61H,36H,17H,79H,22H,79H,16H,20H,10H,4
3H

ARRAY1 DB
63H,71H,97H,22H,97H,61H,36H,17H,79H,22H,79H,16H,20H,10H,4
3H

SMALL DB ?

LARGE DB ?

COUNT EQU 0FH

DATA ENDS

CODE SEGMENT

START:

MOV AX,DATA

MOV DS,AX

MOV DX,COUNT-1

LOOP1: MOV CX,DX

MOV SI,OFFSET ARRAY

LOOP2: MOV AL,[SI]

CMP AL,[SI+1]

JC END1

XCHG [SI+1],AL

XCHG [SI],AL

END1: ADD SI,01H

LOOP LOOP2
Register No.: Name:

DEC DX

JNZ LOOP1

MOV BX,OFFSET ARRAY

MOV AL,[BX]

MOV SMALL,AL

MOV DX,COUNT-1

LOOP3: MOV CX,DX

MOV SI,OFFSET ARRAY1

LOOP4: MOV AL,[SI]

CMP [SI+1],AL

JC END3

XCHG [SI+1],AL

XCHG [SI],AL

END3: ADD SI,01H

LOOP LOOP4

DEC DX

JNZ LOOP3

MOV BX,OFFSET ARRAY1

MOV AL, [BX]

MOV LARGE ,AL

HLT

CODE ENDS

END START
Register No.: Name:

Sample Input:

66h,83h,84h,85h,50h,65h,66h,38h,48h,58h,05h,56h,20h,13h,69h

Sample Output:

05h,13h,20h,38h,48h,50h,56h,58h,65h,66h,66h,69h,83h,84h,85h

85h,84h,83h,69h,66h,66h,65h,58h,56h,50h,48h,38h,29h,13h,05h

SMALLEST ELEMENT :05h

LARGEST ELEMENT :85h

Register/ Memory Contents for I/O:

Snapshot of the Output:


Register No.: Name:

Result:

SORTED THE ARRAY IN ASCENDING AND DESCENDING ORDER AND FOUND


OUT THE LARGEST AND THE SMALLEST ELEMENT IN THE ARRAY.

You might also like