MICROPROCESSOR LAB
MANUAL (SOFTWARE
PROGRAMS)
Note: Only programs have been given. The method of writing the programs and
designing of 8086 program using algorithms is explained in the text book:
Page 1
A. 1
A. 2 LAB PROGRAMS
Page 2
LAB PROGRAMS
(low + high ) / 2
MOV
MOV
SHL
CMP
JNE
AX, KEY
SI, MID
SI, 1
AX, A[SI]
L2
mid 1
; AX
; AX
mid
mid 1
Page 3
A. 3
A. 4 LAB PROGRAMS
MOV HIGH_, AX
; high
mid 1
JMP L1
; low
mid + 1
L3: MOV AX, MID
INC AX
MOV LOW_, AX
; AX
; AX
; low
mid
mid + 1
mid + 1
JMP L1
L4: PRINTF MSG2 ; print search unsuccessful
EXIT
; Terminate program
END begin
;------------------ ---- --- -- Code segment ends-----------------------------Note:Since LOW and HIGH are assembler directives in 8086 assembly language, we use
the identifiers LOW_ and HIGH_ instead of LOW and HIGH in the above program
Page 4
LAB PROGRAMS
A. 5
2A.ASM
.stack
;------------------ --- Data segment starts------------ -.data
extrn a:byte
;Defines that an array variable a is external to the file
msg1 db 0ah,0dh,Enter the characters:,24h
msg2 db 0ah, 0dh,The typed characters:,24h
;------------------ --- Data segment ends--------------;Macro to display the message
printf macro msg
mov
ah, 09h
; function to display the text
lea
dx, msg ; offset value of text to be displayed
int
21h
endm
;macro to terminate the program
exit macro
mov ah, 4ch ; function to terminate the program
int
21h
endm
;------------------ Code segment starts---------------.code
extrn getchar:far ;Function getchar is a defined in another file
extrn putchar:far ;Function putchar is defined in another file
Page 5
A. 6 LAB PROGRAMS
mov ah,4ch
int 21h
end begin
;end of the program
Note: After typing 2A.ASM, Give the following commands:
C:\>MASM 2A;
C:\>EDIT 2B.ASM
and type the program given in next page
Page 6
LAB PROGRAMS
.model small
.stack
2B.ASM
Page 7
A. 7
A. 8 LAB PROGRAMS
Problem 3: Sort a given set of 'n' numbers in ascending and descending orders using the
bubble sort algorithm. (Refer section 8.10.4, page 8.81)
; Program Name: BUBBLE SORT (BSORT.ASM)
; Author
:
A.M. Padma Reddy and YJ
; Purpose
:
To sort the given elements using bubble sort
; Inputs
:
List of elements to sort
; Outputs
:
Sorted array
.MODEL SMALL
;------------------ ---- --- -- Data segment starts- --- ----- ---- ----------------.DATA
A DW 5555H, 4444H, 3333H, 2222H, 1111H
N DW ($-A)/2 ; Number of terms of N
I DW ?
J DW ?
TEMP DW ?
;------------------ ---- --- -- Data segment ends------------------------------;------------------ ---- --- -- Code segment starts-----------------------------.CODE
begin: ; Initialization of data segment register
MOV AX, @DATA
MOV DS, AX
MOV J, 1 ; Initial pass
MOV CX, N
DEC CX ; CX N-1
L1: MOV SI, 0
; of array A
MOV DX, N
SUB DX, J ; DX = N J is the
; counter for inner loop
L2: MOV AX, A[SI]
; AX a[si]
CMP AX, A[SI+2] ; If a[si] <= a[si+2])
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 8
LAB PROGRAMS
JLE L3
; goto L3
a[si+2]
Page 9
A. 9
A. 10 LAB PROGRAMS
Problem 4: Read an alphanumeric character and display its equivalent ASCII code at the
center of the screen. (See section 9.3.1, page 9.12)
.MODEL SMALL
; Macro defintions
; Macro to read a character
GETCHAR MACRO
MOV AH, 01 ; Function to read a character
INT 21H
; AL has the character read
ENDM
; Macro display a character
PUTCHAR MACRO char
MOV AH, 02 ; Function to display a character
MOV DL, char ; Character to be displayed
INT 21H
ENDM
;Macro to display the message
PRINTF MACRO MSG
MOV AH, 09H ; Function to display the text
LEA DX, MSG ; Offset value of text to be displayed
INT 21H
ENDM
;Macro to ternminat the program
EXIT MACRO
MOV AH, 4CH ; Function to terminate the program
INT 21H
ENDM
;------------------ ---- --- -Data segment starts--- --- ---- ---- ---------.DATA
MSG1 DB "Enter a character",0ah, 0dh, 24h
MSG2 DB "The ASCII value: ",24h
;------------------ ---- --- -Data segment ends--------------------------
Page 10
LAB PROGRAMS
BH, AL
BL, AL
PUTCHAR BH
PUTCHAR BL
EXIT
END begin
Page 11
A. 11
A. 12 LAB PROGRAMS
Page 12
LAB PROGRAMS
; Compare N times
Page 13
A. 13
A. 14 LAB PROGRAMS
Problem 6 : 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. (see section 9.3.2, page 9.14)
.MODEL SMALL
; Macro definitions
; Macro to read a character
SCANF MACRO BUFFER
MOV AH, 0AH ; Function to read a string
LEA DX, BUFFER ; Address of the buffer
INT 21H
; AL has the character read
ENDM
;Macro to display the message
PRINTF MACRO MSG
MOV AH, 09H ; Function to display the text
LEA DX, MSG ; Offset value of text to be displayed
INT 21H
ENDM
;Macro to display a character
PUTCHAR MACRO CHAR
MOV AH, 02
MOV DL, CHAR
INT 21H
ENDM
;Macro to terminate the program
EXIT MACRO
MOV AH, 4CH ; Function to terminate the program
INT 21H
ENDM
;------------------ ---- --- -Data segment starts--- --- ---- ---- ---------.DATA
MSG1 DB 0AH, 0DH,"Enter the first string",0ah, 0dh, 24h
MSG2 DB 0AH,0DH,"Enter the second string, 0ah, 0dh,24h
MSG3 DB 0AH,0DHThe two strings are equal, 0AH, 0DH, 24H
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 14
LAB PROGRAMS
MSG4 DB 0AH,0DHThe two strings are not equal, 0AH, 0DH, 24H
MSG5 DB 0AH,0DHNo. of characters in first string :, 24H
MSG6 DB 0AH, 0DH, No. of characters in second string :, 24H
BUFFER1 DB 10
N1 DB ?
A1 DB 10 DUP(?)
BUFFER2 DB 10
N2 DB ?
A2 DB 10 DUP(?)
;------------------ ---- --- -Data segment ends-------------------------;------------------ ---- --- -Code segment starts-----------------------.CODE
begin: MOV AX, @DATA ; Initialization of data segment register
MOV DS, AX
MOV ES, AX ; Intialization of extra segment r egister
; It is must for string manipulation
PRINTF MSG1 ; printf(Enter the first string);
SCANF BUFFER1 ; scanf(%s,a1);
PRINTF MSG2 ; printf(Enter the second string);
SCANF BUFFER2 ; scanf(%s,a2);
; Check length of two strings
MOV AL, N1
CMP AL, N2
JE L1 ; If lengths are equal compare strings
PRINTF MSG4 ; printf(Two strings are not equal);
JMP L3
; Initialization before comparing
L1: LEA SI, A1 ; Offset value of first string
LEA DI, A2 ; Offset value of second string
MOV CL, N1 ; No of characters to be compared
CLD
; To increment increment index registers
REPE CMPSB ; Compare as long as they are equal
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 15
A. 15
A. 16 LAB PROGRAMS
Page 16
LAB PROGRAMS
A. 17
Problem 7 : 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.
.model small
;Macro to clear the screen
clrscr macro
mov
bh,07 ;Attribute of character
mov
ah,06
;Activity is to scroll
mov
al,0
;No. of lines to scroll-0 blank screen
mov
ch,00
;y-coordinate of upper left corner
mov
cl,00 ;x-coordinate of upper right corner
mov
dh,24 ;Number of rows; Lower right row
mov
dl,80 ;Number of columns; Lower right column
int
10h ; clear the screen
endm
;Macro to set the cursor
;
setcursor macro row, col ; row and col are the parameters
mov
ah, 02 ; Function to set the cursor
mov
bh, 0 ; Current page
mov
dh, row ; Which row
mov
dl, col ; Which column
int
10h ; Set the cursor
endm
;Macro equvivalent to
;
printf()
;
scanf()
pri_scn
macro val, msg ; val function to read or display
; msg is the parameter to be displayed
mov
dx, offset msg ; DX holds offset of message to be displayed
mov
ah, 09 ; Function to display the message
int
21h ; Display the message
endm
Page 17
A. 18 LAB PROGRAMS
db 10
db 20
Page 18
LAB PROGRAMS
setcursor row, col
cl,n
ch,0
si,00
top:
inc
loop
stop
; Terminate the program
end begin
Page 19
A. 19
A. 20 LAB PROGRAMS
Problem 8 : Compute nCr using recursive procedure. Assume that 'n' and 'r' are nonnegative integers (See section 9.5.1.2, page 9.32 for details)
.MODEL SMALL
.STACK 64
;------------------ ---- --- ---- -Start of data segment-------------------------------.DATA
N DB 6
K DB 3
RES DB 0
;------------------ ---- --- --- --Start of data segment-------------------------------;------------------ ---- --- ---- -Start of code segment-------------------------------.CODE
NCK PROC
CMP BL, 0
JNE L1 ; if k = 0
ADD RES, 1 ; return 1;
RET
L1: CMP BL, AL
JNE L2 ; if k = n
ADD RES, 1 ; return 1;
RET
L2: CMP BL, 1
JNE L3 ; if k = 1
ADD RES, AL ; return n
RET
L3: DEC AL ; n = n -1
CMP BL, AL
JNE L4 ; if k = n
INC AL ; return n+1
ADD RES, AL
RET
Page 20
LAB PROGRAMS
n-1
Ck
n-1
Ck-1
A. 21
;Compute nCk
06 03 148F
n k nCk = 14H
6C =
3
(20) 10
= (14)
16
Page 21
A. 22 LAB PROGRAMS
Problem 9 : Read the current time from the system and display it in the standard format
on the screen.
.model small
.stack
;Macro display the character
putchar macro val
push ax
mov dl,val ; Character to display
mov ah,02 ; Function to display the character
int 21h ; Display the character
pop
ax
endm
;---------------Start of the code segment ----------------.code
; Procedure to convert from HEX to ASCII and display
hex_asc proc
mov
ah,00
mov
bl,0ah ; Divide by 10 to obtain unpacked BCD
div
bl ; Obtain the unpacked BCD in AH and AL
; Higher nibble in AL
; Lower nibble in AH
add
endp
begin: mov
ah, 2ch ; Function to read the system time
int
21h
; Read the system time
: CH:CL contains hours:minutes
; DH:DL contains second:millisec
Page 22
LAB PROGRAMS
mov
al, ch
call
disp
putchar ':'
mov
al,cl ; Display the minute
call
disp
putchar ':'
mov
call
exit
; Terminate the program
end begin
Page 23
A. 23
A. 24 LAB PROGRAMS
.model small
.stack
;Macro to display a character
putchar macro val
push dx
push ax
mov dl,val ; Character to be displayed
mov ah,02 ; Function to read character
int 21h ; Read a character
pop ax
pop dx
endm
;Code segment starts
.code
;Procedure to convert packed BCD to ASCII and display the character
dec_asci
proc
mov
cl,04
mov
bh,al ; AL has the packed bcd
shr
bh,cl ; Move the higher nibble to lower nibble
mov
bl,al
and
bl,0fh ; Mask the higher nibble
add
bx,3030h ; Convert to ASCII
putchar bh ; Display higher nibble
putchar bl ; Display lower nibble
putchar 0ah ; Move cursor to next line
putchar 0dh ; Move cursor to beginning of current line
ret
dec_asci
endp
Page 24
LAB PROGRAMS
daa
; convert to decimal
call
cmp
je
add
jmp
down:
mov
ah,4ch
; Function to terminate the program
int
21h ; Terminate the program
end begin
Page 25
A. 25
A. 26 LAB PROGRAMS
Problem 11 : read a pair of input co=ordinates in BCD and move the cursor to the
specified location on the screen.
.model small
; Cursor to set at the specified row and column
setcursor macro row, col
mov
ah, 02 ; Function to set cursor
mov
bh, 00 ; page number
mov
dh, row ; Set at this row
mov
dl, col ; Set at this column
int
10h
endm
;Macro to terminate the program
exit macro
mov
ah, 4ch ; Function to terminate the program
int
21h ; Terminate the program
endm
;Macro to read a character
getchar macro
mov
ah,01h ; Function to read a character
int
21h ; AL contains the character typed
endm
;Macro to display the message
printf
macro msg ; msg is the parameter to be displayed
mov
ah, 09 ; Function to display the message
mov
dx, offset msg ; DX hold offset of message to be displayed
int
21h ; Display the message
endm
.data
row db ?
; Space to store row value
col db ?
; Space to store column value
msg1 db "Enter the row value :",0ah,0dh,24h
msg2 db "Enter the col value :",0ah,0dh,24h
.code
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore
Page 26
LAB PROGRAMS
mov
ds, ax
printf
call
msg1
read
mov
row, al
printf
call
mov
msg2
read
col, al
; Read a character
; Terminate the program
end begin
C:\MASM615>cursor
Enter the row value :10
Enter the col value :20
Note: Observe on the screen that cursor will be at row 10 and column 20
Page 27
A. 27
A. 28 LAB PROGRAMS
Problem 12 : program to create a file (input file) and to delete an existing file
.model small
.stack
; Macro to display the message and read the string
prn_scn macro val,msg ; val = 09 display the message msg
; val = 09 read into msg
mov
ah,val ; Function to read or print
mov
dx,offset msg ; Offset value of message or buffer
int
21h ; Read or write
endm
;Macro to terminate the program
exit macro
mov
ah,4ch ; Function to terminate the program
int
21h ; Terminate the program
endm
.data
msg1
buff
db 20 ; Max. no. of characters in the file
len
db ? ; Length of file name
fname db 20 dup(?) ; Space for storing the file name
msg2
msg3
Page 28
LAB PROGRAMS
Page 29
A. 29