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

(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC

: 08258 - 281039 – 281263, Fax: 08258 – 281265


Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

LAB MANUAL
Semester: IV Year: 2020-21

Course Title: Microprocessors and Peripherals Course Code: 19CS407

Course Plan Author/Coordinator: Krishna Prasad Date: 3/04/2021

Course Objectives:
This Course will enable students to:

1. Develop assembly level language program for 8086 using arithmetic, logical shifts and
rotate instructions.
2. Develop assembly level language program for 8086 using macro, interrupts, String
handling and flag set/reset instructions.
3. Analyze the working of miscellaneous instructions like XLAT.
4. Interface 8086 microprocessor to external I/O devices logic controller, seven segment
display.
5. Interface 8086 microprocessor to external I/O devices stepper motor, keypad.

Course Outcomes:
At the end of the course the student will be able to:
1. Develop assembly level language program for 8086 using arithmetic, logical shifts and
rotate instructions.
2. Develop assembly level language program for 8086 using macro, interrupts, String
handling and flag set/reset instructions.
3. Analyze the working of miscellaneous instructions like XLAT.
4. Interface 8086 microprocessor to external I/O devices logic controller, seven segment
display.
5. Interface 8086 microprocessor to external I/O devices stepper motor, keypad.

Page 1 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

Software tool used


EMU8086 -- EMULATOR

Microprocessors and Peripherals Laboratory Exercises


PART A:

1. Search a key element in a list of ‘n’ 16-bit numbers using the Linear search
algorithm. (Display only Success or Unsuccessful message)

2. Write ALP macros:

i. To read a character from the keyboard in the module (1) (different file).

ii. To display a character in module (2) (different file).

iii. Use the above two modules to read a string of characters from the
keyboard terminated by the carriage return and print the string on the
display in the next line.

3. Read an alphanumeric character and display its equivalent ASCII code at the
center of the screen.

4. Reverse a given string and check whether it is a palindrome or not.

5. Read your name from the keyboard and display it at a specified location on
the screen in front of the message “What is your name?” You must clear the
entire screen before display.

6. Compute the factorial of a positive integer ‘n’ using recursive procedure.

7. Generate the first ‘n’ Fibonacci numbers.

Page 2 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

8. Read the current time from the system and display it in the standard format
on the screen.

9. Create and open a file named myfile.txt, and write a text 'Welcome to
MICROPROCESSOR LAB' in this file & close. Next, the reads from the file
myfile.txt and it display the text on to the screen.

PART B:

1. Search a key element in a list of ‘n’ 16-bit numbers using the Binary search
algorithm

2. Sort a given set of ‘n’ numbers in ascending and descending orders using the
Bubble Sort algorithm.

3. Sort a given set of ‘n’ numbers in ascending and descending orders using the
Selection Sort algorithm.

4. Read a pair of 16-bit numbers and compute their GCD.

5. Read two strings, store them in locations STR1 and STR2. Check whether they
are equal or not and display appropriated messages. Also display the length
of the stored strings.

6. Find out whether a given sub-string is present or not in a main string of


characters.

7. Read a pair of input co-ordinates in BCD and move the cursor to the specified
location on the screen.

8. Program to simulate a Decimal Up-Down counter to display 00-99.

Page 3 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

PART A:
1. Search a key element in a list of ‘n’ 16-bit numbers using the Linear search
algorithm
DATA SEGMENT
ARR DW 1000H,3402H,3501H,2234H,4567H
LEN DB $-ARR
KEY DW 2234H
SUCC DB 10,13, "SEARCH SUCCESSFUL $"
NOTSUCC DB 10,13,"UNSUCCESSFUL$"
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
SHR LEN,01
MOV CL,0
LEA SI, ARR
UP:
CMP CL, LEN
JE NOTFOUND
MOV AX,[SI]
CMP KEY,AX
JE FOUND
ADD SI,02
INC CL
JMP UP
FOUND:
LEA DX,SUCC
MOV AH,09
INT 21H
JMP EXIT

Page 4 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

NOTFOUND:
LEA DX,NOTSUCC
MOV AH,09
INT 21H
EXIT:
MOV AH,4CH
INT 21H
CODE ENDS
ENS START

2. Write ALP macros:


i. To read a character from the keyboard in the module (1) (in a different
file).
ii. To display a character in module(2) (from different file).
iii. Use the above two modules to read a string of characters from the
keyboard terminated by the carriage return and print the string on the
display in the next line.

READCHAR MACRO
MOV AH, 01H
INT 21H
ENDM
DISPCHAR MACRO
MOV AH, 02H
INT 21H
ENDM

INCLUDE F1.MAC
INCLUDE F2.MAC

DATA SEGMENT
msg1 db 10, 13, 'ENTER STRING : $'

Page 5 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

msg2 db 10, 13, 'ENTERED STRING IS : $'


str db 50 DUP(?)
n db ?
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
LEA DX, msg1
MOV AH, 09H
INT 21H
LEA SI, str
CALL READSTRING
MOV n, CL
LEA DX, msg2
MOV AH, 09H
INT 21H
LEA SI, str
MOV CL, n
CALL DISPSTRING
MOV AH, 4CH
INT 21H

READSTRING PROC NEAR


MOV CL, 00H
UP:
CMP CL, 50
JZ L1
READCHAR
CMP AL, 0DH
JZ L1
MOV [SI], AL

Page 6 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

INC SI
INC CL
JMP UP
L1: RET
READSTRING ENDP

DISPSTRING PROC NEAR


UP2:
CMP CL, 00
JZ L2
MOV DL, [SI]
DISPCHAR
INC SI
DEC CL
JMP UP2
L2: RET
DISPSTRING ENDP
CODE ENDS
END START

3. Read an alphanumeric character and display its equivalent ASCII code at the
center of the screen.
CLRSCR MACRO
MOV AH, 00H
MOV AL, 02H
INT 10H
ENDM

SETCURSOR MACRO row, col


MOV DL, col
MOV DH, row
MOV BH, 00H
MOV AH, 02H
INT 10H
ENDM

Page 7 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DATA SEGMENT
msg1 DB 10, 13, 'ENTER THE CHARACTER : $'
n DB ?
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
LEA DX, msg1
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
MOV n, AL
CLRSCR
MOV AL, 02H
SETCURSOR 12, 40
MOV BL, n
CALL DISPHEXA
MOV AH, 01H
INT 21H
MOV AH, 4CH
INT 21H
DISPHEXA PROC NEAR
MOV DL, BL
MOV CL, 04H
SHR DL, CL
CMP DL, 09H
JBE L1
ADD DL, 07H
L1:
ADD DL, 30H

Page 8 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV AH, 02HINT 21H


MOV DL, BL
AND DL, 0FH
CMP DL, 09H
JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
RET
DISPHEXA ENDP
CODE ENDS
END START

4. Reverse a given string and check whether it is a palindrome or not.


DATA SEGMENT
str1 DB 20 DUP(?)
str2 DB 20 DUP(?)
n DB ?
M1 DB 10, 13, 'ENTER THE STRING : $'
M2 DB 10, 13, 'STRING IS PALINDROME $'
M3 DB 10, 13, 'STRING IS NOT PALINDROME$'
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI, str1
LEA DI, str2
LEA DX, M1

Page 9 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV AH, 09H


INT 21H
CALL READSTRING
L2:
MOV n, CL
MOV CL, 00HDEC SI
UP1:
CMP CL, n
JZ CHECK
MOV AL, [SI]
MOV [DI], AL
DEC SI
INC CL
INC DI
JMP UP1
CHECK:
LEA SI, str1
LEA DI, str2
MOV CL, 00H
UP2:
CMP CL, n
JZ PAL
MOV AL, [SI]
CMP AL, [DI]
JNZ NOTPAL
INC SI
INC DI
INC CL
JMP UP2
NOTPAL:
LEA DX, M3
MOV AH, 09H
INT 21H
JMP EXIT

Page 10 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

PAL:
LEA DX, M2
MOV AH, 09H
INT 21H
EXIT:
MOV AH, 4CH
INT 21H

READSTRING PROC NEAR


MOV CL, 00H
UP:
MOV AH, 01H
INT 21H
CMP AL, 0DH
JZ L1
MOV [SI], AL
INC SI
INC CL
JMP UP
L1:
RET
READSTRING ENDP
CODE ENDS
END START

5.Read your name from the keyboard and display it at a specified location on the
screen in front of the message “What is your name?” You must clear the entire
screen before display.

CLRSCR MACRO
MOV AH, 00H
MOV AL, 02H
INT 10H
ENDM

Page 11 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

SETCURSOR MACRO row, col


MOV DL, col
MOV DH, row
MOV BH, 00H
MOV AH, 02H
INT 10H
ENDM

DATA SEGMENT
msg1 DB 'ENTER YOUR NAME:$'
msg2 DB 'What is your name?$'
str DB 30 dup(?)
n DB ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,msg1
MOV AH,09H
INT 21H
LEA SI, str
CALL READSTRING
MOV n,CL
CLRSCR
MOV AH, 02H
SETCURSOR 10,30
LEA DX,msg2
MOV AH,09H
INT 21H
LEA SI, str
MOV CL, n

Page 12 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

CALL DISPSTRING
MOV AH,01H
INT 21H
MOV AH,4CH
INT 21H
READSTRING PROC NEAR
MOV CL, 00H
UP: CMP CL,30
JZ L1
MOV AH,01H
INT 21H
CMP AL,0DH
JZ L1
MOV [SI],AL
INC SI
INC CL
JMP UP
L1: RET
READSTRING ENDP

DISPSTRING PROC NEAR


UP1: CMP CL,00H
JZ L2
MOV DL,[SI]
MOV AH,02H
INT 21H
INC SI
DEC CL
JMP UP1
L2: RET
DISPSTRING ENDP
CODE ENDS
END START

Page 13 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

6. Compute the factorial of a positive integer ‘n’ using recursive procedure.

DATA SEGMENT
n DB 06H
fact DW ?
msg DB 'FACTORIAL(6)=$'
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV AX,1
MOV BL,n
MOV BH,00
CALL FACTORIAL
MOV fact,AX
MOV BL, AH
LEA DX,msg
MOV AH,09H
INT 21H
CALL DISPHEXA
MOV BX,fact
CALL DISPHEXA
MOV AH,4CH
INT 21H
FACTORIAL PROC
CMP BX,1
JE L1
PUSH BX
DEC BX
CALL FACTORIAL
POP BX

Page 14 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MUL BX
L1: RET
FACTORIAL ENDP

DISPHEXA PROC
MOV DL,BL
MOV CL,04
SHR DL,CL
CMP DL,09H
JBE L2
ADD DL,07H
L2:
ADD DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
AND DL,0FH
CMP DL,09H
JBE L3
ADD DL,07H
L3:
ADD DL,30H
MOV AH,02H
INT 21H
RET
DISPHEXA ENDP
CODE ENDS
END START

7. Generate the first ‘n’ Fibonacci numbers.

DATA SEGMENT
f1 DB 00H
f2 DB 01H

Page 15 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

f3 DB ?
msg DB 'THE FIBONACCI SERIES IS:',10,13,'$'
N DB 10
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,msg
MOV AH,09H
INT 21H
MOV BL,f1
CALL DISPHEXA
MOV DL,' '
MOV AH,02H
INT 21H
MOV BL,f2
CALL DISPHEXA
MOV DL,' '
MOV AH,02H
INT 21H
MOV CL,00
SUB N, 02H
UP:
CMP CL,N
JZ EXIT
MOV AL,f1
ADD AL,f2
MOV f3,AL
MOV BL,f3
CALL DISPHEXA
MOV DL,' '

Page 16 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV AH,02H
INT 21H
MOV AL,f2
MOV f1,AL
MOV AL,f3
MOV f2,AL
INC CL
JMP UP
EXIT:
MOV AH,4CH
INT 21H
DISPHEXA PROC
PUSH CX
MOV DL,BL
MOV CL,04
SHR DL,CL
CMP DL,09H
JBE L1
ADD DL,07H
L1:
ADD DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
AND DL,0FH
CMP DL,09H
JBE L2
ADD DL,07H
L2:
ADD DL,30H
MOV AH,02H
INT 21H
POP CX
RET

Page 17 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DISPHEXA ENDP
CODE ENDS
END START

8. Read the current time from the system and display it in the standard format on
the screen.

DATA SEGMENT
HR DB ?
MIN DB ?
SEC DB ?
MSEC DB ?
msg DB 'THE CURRENT TIME IS:',10,13,'$'
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,msg
MOV AH,09H
INT 21H
MOV AH,2CH
INT 21H
MOV HR,CH
MOV MIN,CL
MOV SEC,DH
MOV MSEC,DL
MOV AL,HR
AAM
MOV BX,AX
CALL DISPUNPACKDBCD

Page 18 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV DL,':'
MOV AH,02H
INT 21H
MOV AL,MIN
AAM
MOV BX,AX
CALL DISPUNPACKDBCD
MOV DL,':'
MOV AH,02H
INT 21H
MOV AL,SEC
AAM
MOV BX,AX
CALL DISPUNPACKDBCD
MOV DL,':'
MOV AH,02H
INT 21H
MOV AL,MSEC
AAM
MOV BX,AX
CALL DISPUNPACKDBCD
MOV AH,4CH
INT 21H

DISPUNPACKDBCD PROC NEAR


MOV DL,BH
ADD DL,30H
MOV AH,02H
INT 21H
MOV DL,BL
ADD DL,30H
MOV AH,02H
INT 21H
RET

Page 19 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DISPUNPACKDBCD ENDP
CODE ENDS
END START

9. Create and open a file named myfile.txt, and write a text 'Welcome to
MICROPROCESSOR LAB' in this file & close it. Next, read from the file myfile.txt
and display the text on to the screen.
DATA SEGMENT
FILE DB 'MYFILE.TXT',0
MSG DB "WELCOME TO MICROPROCESSOR LAB$"
BUF DB 30 DUP (?)
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX,DATA
MOV DS, AX

LEA DX, FILE


MOV CX, 0
MOV AH, 3CH
INT 21H

LEA DX, MSG


MOV CX, 30
MOV BX, AX
MOV AH, 40H
INT 21H

MOV AH, 3EH


INT 21H

Page 20 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

LEA DX, FILE


MOV AL, 00H
MOV AH, 3DH
INT 21H

LEA DX, BUF


MOV BX, AX
MOV CX, 30
MOV AH, 3FH
INT 21H

MOV AH,3EH
INT 21H

LEA DX, BUF


MOV AH,09H
INT 21H

MOV AH,4CH
INT 21H
CODE ENDS
END START

Page 21 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

PART B

1. Search a key element in a list of ‘n’ 16-bit numbers using the Binary search
algorithm
DATA SEGMENT
arr DW 0012H, 0015H, 0063H, 0078H, 0156H, 0206H, 0558H len DB $-ARR
mid db ?
key dw 0079H
msg1 db 10, 13, 'KEY NOT FOUND', 10, 13, '$'
msg2 db 10, 13, 'KEY FOUND AT POSITION $'
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
SHR len, 01H
DEC len
MOV DL,00H
MOV DH, len
MOV CX, key
UP:
CMP DL, DH
JG NOTFOUND
MOV AL, DL
ADD AL, DH
SHR AL, 01H
MOV mid, AL
MOV BL, 02H
MUL BL
MOV BX, AX

Page 22 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

CMP CX, arr[BX]


JZ FOUND
JB FIRSTHALF
INC mid
MOV DL, mid
JMP UP
FIRSTHALF:
DEC mid
MOV DH, mid
JMP UP
NOTFOUND:
LEA DX, msg1
MOV AH, 09H
INT 21H
JMP EXIT
FOUND:
LEA DX, msg2
MOV AH, 09H
INT 21H
MOV BL, mid
CALL DISPHEXA
EXIT:
MOV AH, 4CH
INT 21H
DISPHEXA PROC NEAR
MOV DL, BL
MOV CL, 04H
SHR DL, CL
CMP DL, 09H
JBE L1
ADD DL, 07H
L1:
ADD DL, 30H
MOV AH, 02H

Page 23 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

INT 21H
MOV DL, BL
AND DL, 0FH
CMP DL, 09H
JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
RET
DISPHEXA ENDP
CODE ENDS
END START

2. Sort a given set of ‘n’ numbers in ascending/ descending order using the
Bubble Sort algorithm.

DATA SEGMENT
a DB 61H, 38H, 05H, 91H, 82H, 03H
len DB $-a
NL DB 10, 13, '$'
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
DEC len
MOV CL, 00H
UP2:
CMP CL, len
JZ DISPLAY
MOV BX, 0000H

Page 24 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV DL, len


SUB DL, CL
UP1:
CMP BL, DL
JZ D1
MOV AL, a[BX]
CMP AL, a[BX+1]
JB NOSWAP
MOV AH, a[BX+1]
MOV a[BX], AH
MOV a[BX+1], AL
NOSWAP:
INC BL
JMP UP1
D1:
INC CL
JMP UP2
DISPLAY:
MOV CL, 00
LEA SI, a
UP3:
CMP CL, len
JA EXIT
MOV BL, [SI]
CALL DISPHEXA
LEA DX, NL
MOV AH, 09H
INT 21H
INC CL
INC SI
JMP UP3
EXIT:
MOV AH, 4CH
INT 21H

Page 25 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DISPHEXA PROC NEAR


PUSH CX
PUSH DX
MOV DL, BL
MOV CL, 04H
SHR DL, CL
CMP DL, 09H
JBE L1
ADD DL, 07H
L1:
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
AND DL, 0FH
CMP DL, 09H
JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
POP DX
POP CX
RET
DISPHEXA ENDP
CODE ENDS
END START

3. Sort a given set of ‘n’ numbers in ascending / descending orders using the
Selection Sort algorithm.
DATA SEGMENT
a DB 91H, 82H,61H, 38H,0ABH, 05H, 03H

Page 26 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

n DB $-a
POS DB ?
NL DB 10,13,'$'
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
DEC N
MOV DX, 00H
MOV BX,0

UP2:
CMP DL, N
JZ DISPLAY
MOV BL, DL
MOV CX,DX
INC CL

UP1:
CMP CL, N
JA SWAP
LEA SI,A
ADD SI,CX
MOV AL, [SI]
CMP A[BX], AL
JBE SKIP
MOV BX, SI
SKIP:
INC CL
JMP UP1
SWAP:
LEA SI,A

Page 27 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

ADD SI,DX
MOV AL,[SI]
XCHG AL, A[BX]
MOV [SI], AL
INC DL
JMP UP2
DISPLAY:
MOV CL, 00
LEA SI, a
UP3:
CMP CL, N
JA EXIT
MOV BL, [SI]
CALL DISPHEXA
CALL NEWLINE
INC CL
INC SI
JMP UP3
EXIT:
MOV AH, 4CH
INT 21H
DISPHEXA PROC NEAR
PUSH CX
PUSH DX
MOV DL, BL
MOV CL, 04H
SHR DL, CL
CMP DL, 09H
JBE L1
ADD DL, 07H
L1:
ADD DL, 30H
MOV AH, 02H
INT 21H

Page 28 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV DL, BL
AND DL, 0FH
CMP DL, 09H
JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
POP DX
POP CX
RET
DISPHEXA ENDP
NEWLINE PROC NEAR
LEA DX, NL
MOV AH, 09H
INT 21H
RET
NEWLINE ENDP
CODE ENDS
END START

4. Read a pair of 16bit numbers and compute their GCD.


DATA SEGMENT
Num1 DW ?
Num2 DW ?
Ans DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX

Page 29 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

CALL READHEXA
MOV BYTE PTR NUM1+1, BL
CALL READHEXA
MOV BYTE PTR NUM1, BL

CALL READHEXA
MOV BYTE PTR NUM2+1, BL
CALL READHEXA
MOV BYTE PTR NUM2, BL

MOV AX, NUM1 ; Load first number to AX register


MOV BX, NUM2 ; Load second number to BX register
FIRST:
CMP AX, BX ; Compare two numbers
JA NEXT ; If AX contents are greater jump to label ‘next’
XCHG AX, BX ; Otherwise exchange the contents of AX and BX
NEXT:
MOV DX, 0000h ; Initialize DX register with 0000
DIV BX ; Divide the AX register contents by BX content
CMP DX, 0000h ; Compare whether remainder is 0
JE LAST ; If remainder is 0, jump to label ‘last’
MOV AX, DX ; Otherwise, copy the remainder to AX register
JMP FIRST ; Jump to label ‘first’
LAST:
MOV Ans, BX ; Move the contents of BX to memory location ‘Ans’
MOV BL, BYTE PTR ANS+1
CALL DISPHEXA
MOV BL, BYTE PTR ANS
CALL DISPHEXA
MOV AH, 4CH
INT 21H
READHEXA PROC
MOV BL,0

Page 30 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV AH,01H
INT 21H
CMP AL,39H
JBE SKIP1
ADD AL, 09H
SKIP1:
AND AL,0FH
ADD BL, AL
MOV AH,01H
INT 21H
CMP AL,39H
JBE SKIP2
ADD AL, 09H
SKIP2:
AND AL,0FH
MOV CL,04
SHL BL, CL
ADD BL,AL
RET
READHEXA ENDP
DISPHEXA PROC NEAR
MOV DL, BL
MOV CL, 04H
SHR DL, CL
CMP DL, 09H
JBE L1
ADD DL, 07H
L1:
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
AND DL, 0FH
CMP DL, 09H

Page 31 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
RET
DISPHEXA ENDP
CODE ENDS
END START

5. Read two strings, store them in locations STR1 and STR2. Check whether they
are equal or not and display appropriated messages. Also display the length of
the stored strings.

DATA SEGMENT
STR1 DB 20 DUP(?)
STR2 DB 20 DUP(?)
N1 DB ?
N2 DB ?
M1 DB 10, 13, 'STRINGS ARE EQUAL$'
M2 DB 10, 13, 'STRINGS ARE NOT EQUAL$'
M3 DB 10, 13, 'ENTER STRING 1 : $'
M4 DB 10, 13, 'ENTER STRING 2 : $'
M5 DB 10, 13, 'STRING 1 LENGTH : $'
M6 DB 10, 13, 'STRING 2 LENGTH : $'
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
LEA DX, M3

Page 32 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV AH, 09H


INT 21HLEA SI, STR1
CALL READSTRING
MOV N1, CL
LEA DX, M4
MOV AH, 09H
INT 21H
LEA SI, STR2
CALL READSTRING
MOV N2, CL
CMP N1, CL
JNZ NOTEQ
LEA SI, STR1
LEA DI, STR2
MOV CL, 00H
UP1:
CMP CL, N1
JZ STREQ
MOV AL, [SI]
CMP AL, [DI]
JNZ NOTEQ
INC SI
INC DI
INC CL
JMP UP1
NOTEQ:
LEA DX, M2
MOV AH, 09H
INT 21H
JMP DISPLEN
STREQ:
LEA DX, M1
MOV AH, 09H
INT 21H

Page 33 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DISPLEN:
LEA DX, M5
MOV AH, 09H
INT 21H
MOV BL, N1
CALL DISPHEXA
LEA DX, M6
MOV AH, 09H
INT 21H
MOV BL, N2
CALL DISPHEXA
MOV AH, 4CH
INT 21H

READSTRING PROC NEAR


MOV CL, 00H
UP:
MOV AH, 01H
INT 21H
CMP AL, 0DH
JZ K1
MOV [SI], AL
INC SI
INC CL
JMP UP
K1:
RET
READSTRING ENDP
DISPHEXA PROC NEAR
PUSH CX
PUSH DX
MOV DL, BLMOV CL, 04H
SHR DL, CL
CMP DL, 09H

Page 34 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

JBE L1
ADD DL, 07H
L1:
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
AND DL, 0FH
CMP DL, 09H
JBE L2
ADD DL, 07H
L2:
ADD DL, 30H
MOV AH, 02H
INT 21H
POP DX
POP CX
RET
DISPHEXA ENDP
CODE ENDS
END START

6. Find out whether a given sub-string is present or not in a main string of


characters.
DATA SEGMENT
T DB "NMAMIT"
N DB $-T
P DB "MIT"
PAGE 11 OF 34
M DB $-P
LEN DB ?

Page 35 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MSG1 DB "SUB STRING FOUND$"


MSG2 DB "SUB STRING NOT FOUND$"
TEMP DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI, T
LEA DI, P
MOV AL, N ;LEN=N-M
SUB AL, M
MOV LEN, AL
MOV CX, 0000H ; I->CX
UP1:
CMP CL, LEN
JA NOMATCH ;CHECK IF I<=N-M
MOV DX, 0000H ; J->DX
UP2:
CMP DL, M
JAE L1 ;CHECK IF J<M
MOV BX, DX
MOV AL, [DI][BX]
MOV TEMP,AL
ADD BX, CX
MOV AL, [SI][BX]
CMP AL, TEMP
JNZ L1

Page 36 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

INC DX
JMP UP2
L1:
CMP DL, M
JZ MATCH
INC CX
JMP UP1
MATCH:
LEA DX, MSG1
MOV AH, 09H
INT 21H
JMP EXIT
NOMATCH:
LEA DX, MSG2
MOV AH, 09H
INT 21H
EXIT:
MOV AH, 4CH
INT 21H
CODE ENDS
END START

7. Read a pair of input co-ordinates in BCD and move the cursor to the
specified location on the screen.

CLRSCR MACRO
MOV AH,00
MOV AL,02
INT 10H
ENDM

Page 37 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

SETCURSOR MACRO R,C


MOV DH,R
MOV DL,C
MOV BH,00H
MOV AH,02H
INT 10H
ENDM
DATA SEGMENT
BCD_X DB ?
BCD_Y DB ?
BIN_X DB ?
BIN_Y DB ?
msg1 DB 'ENTER THE X CO-ORDINATE: $'
msg2 DB 10,13,'ENTER THE Y CO-ORDINATE: $'
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,msg1
MOV AH,09H
INT 21H
CALL READBCD
MOV BCD_X,BL
LEA DX,msg2
MOV AH,09H
INT 21H
CALL READBCD
MOV BCD_Y,BL
MOV BL,BCD_X
CALL BCD_TO_BIN

Page 38 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

MOV BIN_X,BL
MOV BL,BCD_Y
CALL BCD_TO_BIN
MOV BIN_Y,BL
CLRSCR
SETCURSOR BIN_Y,BIN_X
MOV AH,01H
INT 21H
MOV AH,4CH
INT 21H
READBCD PROC NEAR
MOV AH,01H
INT 21H
MOV BL, AL
MOV CL,04H
SHL BL,CL
MOV AH,01H
INT 21H
AND AL,0FH
ADD BL,AL
RET
READBCD ENDP

BCD_TO_BIN PROC NEAR


MOV DH,BL
AND DH,0FH
MOV AL, BL
MOV CL,04H
SHR AL,CL
MOV DL,0AH
MUL DL
ADD AL, DH
MOV BL, AL
RET

Page 39 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

BCD_TO_BIN ENDP
CODE ENDS
END START

8. Program to simulate a Decimal Up-counter to display 00-99.

CLRSCR MACRO
MOV AH, 00H
MOV AL, 02H
INT 10H
ENDM

CODE SEGMENT
ASSUME CS:CODE
START:
CLRSCR
MOV AL, 00H
UP:
CMP AL, 99H
JZ DISP99
CALL CENTER
MOV BL, AL
CALL DISPLAY
CALL DELAY
ADD AL, 01H
DAA
JMP UP
DISP99:
CALL CENTER
MOV BL, AL
CALL DISPLAY
CALL DELAY
MOV AH, 4CH
INT 21H

Page 40 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

CENTER PROC
PUSH AX
MOV DL, 39
MOV DH, 12
MOV BH,0
MOV AH,02h
INT 10h
POP AX
RET
CENTER ENDP

DISPLAY PROC
PUSH AX
MOV DL, BL
MOV CL,04
SHR DL, CL
CMP DL,09
JBE DOWN1
ADD DL,07h
DOWN1:
ADD DL,30h
MOV AH,02h
INT 21h
MOV DL, BL
AND DL,0Fh
CMP DL,09H
JBE DOWN2
ADD DL,07h
DOWN2:
ADD DL,30h
MOV AH,02h
INT 21h
POP AX
RET

Page 41 of 42
(ISO 9001:2015 Certified), Accredited with ‘A’ Grade by NAAC
: 08258 - 281039 – 281263, Fax: 08258 – 281265
Department of Computer Science and Engineering
B.E. CSE Program Accredited by NBA, New Delhi from 1-7-2018 to 30-6-2021

DISPLAY ENDP

DELAY PROC
PUSH AX
PUSH BX
PUSH CX
MOV CX,100h
L1:
MOV BX,0FFFh
L2:
DEC BX
JNZ L2
LOOP L1
POP CX
POP BX
POP AX
RET
DELAY ENDP
CODE ENDS
END START

Page 42 of 42

You might also like