Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CSE2006

MICROPROCESSOR AND INTERFACING

LAB ASSIGNMENT – 4
NAME: Karthik Chidurala
Reg No: 20BCE2164

1. Sort the array in ascending order


Algorithm:
1. Move pointer to location 1200h where the array is stored
2. Store the elements of array in the AL register
3. Compare the element at [SI] and AL, if element at AL>[SI] then
exchange the elements
4. Decrement counter
5. Continue till counter is 0
Code:
;ascending order
MOV SI,1200h
MOV CL,[SI]
DEC CL
LOOP3:
MOV SI,1200h
MOV CH,[SI]
DEC CH
INC SI

LOOP2:
MOV AL,[SI]
INC SI
CMP AL,[SI]
JC LOOP1
XCHG AL,[SI]
XCHG [SI-1],AL

LOOP1:
DEC CH
JNZ LOOP2
DEC CL
JNZ LOOP3
HLT
Input:
SI pointing at 1200h

Giving input: size=4, array= 04,03,02,01


Output:
After sorting

Code written in emu8086:


2. Sort the array in descending order
Algorithm:
1. Move pointer to location 1200h where the array is stored
2. Store the elements of array in the AL register
3. Compare the element at [SI] and AL, if element at AL<[SI] then
exchange the elements
4. Decrement counter
5. Continue till counter is 0

Code:
;Descending order code

MOV SI,1200h
MOV CL,[SI]
DEC CL
LOOP3:
MOV SI,1200h
MOV CH,[SI]
DEC CH
INC SI

LOOP2:
MOV AL,[SI]
INC SI
CMP AL,[SI]
JC LOOP1
XCHG AL,[SI]
XCHG [SI-1],AL

LOOP1:
DEC CH
JNZ LOOP2
DEC CL
JNZ LOOP3
HLT
Input:
Input: size of array=4 and array=01,02,03,04

Output:
Code written in emu8086:
3. Find the smallest number in an array
Algorithm:
1. Take array input as 8,14,5,10,15
2. Initialize res
3. Move array to AX register
4. Set count as 4
5. Set bl as 79h (A very high number)
6. Store the array element to AL
7. Compare AL and BL, if AL<BL then set BL=AL
8. Decrement counter, if not 0 then go to step 6

Code:
;Smallest number
data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends

code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov cx, 04h

mov bl, 79h


LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jge nxt
mov bl, al
nxt:
inc si
dec cx
jnz up
mov res,bl
HLT

Output:

SI at the end of all the iterations will be set to 4 which indicates the
end of the array
Code written in emu8086:
4. Find the largest number in an array
Algorithm:
1. Take array input as 8,14,5,3,9
2. Initialize res
3. Move array to AX register
4. Set count as 4
5. Set bl as 79h (A very high number)
6. Store the array element to AL
7. Compare AL and BL, if AL>BL then set BL=AL
8. Decrement counter, if not 0 then go to step 6

Code:
;largest number
data segment
STRING1 DB 08h,14h,05h,03h,09h
res db ?
data ends

code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h

mov bl, 00h


LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jl nxt
mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
int 3
code ends
end start

Output:

SI becomes 4 after the code has been executed


Code written in emu8086:

You might also like