15CS205J-Microprocessors and Microcontrollers Lab Manual
15CS205J-Microprocessors and Microcontrollers Lab Manual
SRM UNIVERSITY,
Port Programming
7
Timer-Counter Programming
8
Serial Programming
9
Interrupt Programming
10
PREPARED BY:
APPROVED BY,
MASM:
8086 PROGRAM:
Addition
AIM: To implement assembly language program for addition of two 16-bit numbers.
PROGRAM:
DATA SEGMENT
N1 DW 1234H
N2 DW 2134H
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV AX, N1
MOV BX, N2
ADD AX, BX
MOV RES, AX
INT 21H
CODE ENDS
END START
RESULT:
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 3 | 57
AX = 3368h
Subtraction
AIM: To implement assembly language program for subtraction of two 16-bit numbers.
PROGRAM:
DATA SEGMENT
N1 DW 4444H
N2 DW 2121H
RES DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,N1
MOV BX,N2
SUB AX,BX
MOV RES,AX
INT 21H
CODE ENDS
END START
RESULT:
AX = 2323h
Multiplication
AIM: To implement assembly language program for Multiplication of two 16-bit numbers.
PROGRAM:
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MUL OPR2
MOV RESLW, AX
MOV RESHW, DX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2000H
OPR2 DW 4000H
RESLW DW ?
RESHW DW ?
DATA ENDS
END
(DX)
Input:
OPR1 = 2000H
OPR2 = 4000H
Output:
Division
AIM: To implement assembly language program for Division of two 16-bit numbers.
Program:
CODE SEGMENT
MOV DS, AX
DIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2C58H
OPR2 DB 56H
RESQ DB ?
RESR DB ?
DATA ENDS
END
Input:
OPR1 = 2C58H (DIVIDEND)
Program:
DATA SEGMENT
X DW 06H
FACT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,01H
MOV CX,X
UP: MUL CX
LOOP UP
MOV FACT,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START
Input: 06
Output: 2D0H
AIM: To implement assembly language program for Byte and Word data transfer in different
addressing modes
Program:
DATA SEGMENT
DATA1 DB 23H
DATA2 DW 1234H
DATA3 DB 0H
DATA4 DW 0H
DATA5 DW 2345H,6789H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA ; //Initialize DS to point to start of the memory
MOV DS,AX ; //set aside for storing of data
MOV AL,25X ; //copy 25H into 8 bit AL register
MOV AX,2345H ;// copy 2345H into 16 bit AX register
MOV BX,AX ;// copy the content of AX into BX register(16 bit)
MOV CL,AL ;// copy the content of AL into CL register
MOV AL,DATA1 ;// copies the byte contents of data segment memory
location DATA1 into 8 bit AL
MOV AX,DATA2 ;// copies the word contents of data segment memory
;location DATA2 into 16 bit AX
MOV DATA3,AL ;// copies the AL content into the byte contents of data
;segment memory location DATA3
MOV DATA4,AX ;//copies the AX content into the word contents of
;data segment memory location DATA4
MOV BX,OFFSET DATA5 ;// The 16 bit offset address of DS memeory location
; DATA5 is copied into BX
MOV AX,[BX] ; //copies the word content of data segment
;memory location addressed by BX into
;AX(register indirect addressing)
MOV DI,02H ; address element
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 8 | 57
MOV AX,[BX+DI} ; copies the word content of data segment
;memory location addressed by BX+DI into
;AX(base plus indirect addressing)
MOV AX,[BX+0002H] ; copies the word content of data segment
;(16 bit)
MOV AL,[DI+2] ; register relative addressing
MOV AX,[BX+DI+0002H] ; copies the word content of data segment
;memory location addressed by BX+DI+0002H
;into AX(16 bit)
MOV AH,4CH ; Exit to DOS with function call 4CH
INT 21H
CODE ENDS ; Assembler stop reading
END START
Without overlapping
Program:
DATA SEGMENT
X DB 01H,02H,03H,04H,05H ; Initialize Data Segments Memory Locations
Y DB 05 DUP(0)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA ; Initialize DS to point to start of the memory
MOV DS,AX ; set aside for storing of data
MOV CX,05H ; Load counter
LEA SI,X+04 ; SI pointer pointed to top of the memory block
LEA DI,X+04+03 ; 03 is displacement of over lapping, DI pointed to
;the top of the destination block
Before execution
00
00
00
00
00
05
04
03
02
01
After execution
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 10 | 57
05
04
03
02
01
05
04
03
02
01
Y,DI
X, SI
With Overlapping
Program:
DATA SEGMENT
X DB 01H,02H,03H,04H,05H ; Initialize Data Segments Memory Locations
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA ; Initialize DS to point to start of the memory
MOV DS,AX ; set aside for storing of data
MOV CX,05H ; Load counter
LEA SI,X+04 ; SI pointer pointed to top of the memory block
LEA DI,X+04+03 ; 03 is displacement of over lapping, DI pointed to
;the top of the destination block
UP: MOV BL,[SI] ; Move the SI content to BL register
MOV [DI],BL ; Move the BL register to content of DI
DEC SI ; Update SI and DI
DEC DI
DEC CX ; Decrement the counter till it becomes zero
JNZ UP
MOV AH,4CH
INT 21H
CODE ENDS
END START
DS Before execution
xx
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 11 | 57
xx
xx
xx
xx
05
04
03
02
01
DI
SI
X
DS After execution
xx
xx
05
04
03
- 02
01
03
02
01
-
AIM: To implement assembly language program involving bit manipulation instruction If given
data is positive or negative
Program:
DATA SEGMENT
NUM DB 12H
MES1 DB 10,13,'DATA IS POSITIVE $'
MES2 DB 10,13,'DATA IS NEGATIVE $'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AL,NUM
ROL AL,1
JC NEGA
;Move the Number to AL.
;Perform the rotate left side for 1 bit position.
;Check for the negative number.
MOV DX,OFFSET MES1 ;Declare it positive.
JMP EXIT ;Exit program.
NEGA: MOV DX,OFFSET MES2;Declare it negative.
EXIT: MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 13 | 57
Output: Data is positive
Positive Numbers: 00-7F
Negative numbers: 80-FF
ExNo:4 Bubble sort using MASM
Program:
Ascending Order:
model small
.data
arr db 5h,7h,6h,4h,10h,09h
len db $-arr
.code
start: mov ax,@data
mov ds,ax
mov cl,len
lp1: mov bx,cx
lea si,arr
lp2: mov al,[si]
inc si
cmp [si],al
jb lp3
xchg [si],al
mov [si-1],al
lp3: dec bx
jnz lp2
loop lp1
mov ah,4ch
Descending Order:
.model small
.data
arr db 5h,7h,6h,4h,10h,09h
len db $-arr
.code
start: mov ax,@data
mov ds,ax
mov cl,len
lp1: mov bx,cx
lea si,arr
lp2: mov al,[si]
inc si
cmp [si],al
jb lp3
xchg [si],al
mov [si-1],al
lp3: dec bx
jnz lp2
loop lp1
mov ah,4ch
int 21h
end start
AIM: To implement assembly language program to check whether the string is a palindrome.
Program:
DATA SEGMENT
STR1 DB 'LIRIL'
LEN EQU $-STR1
STR2 DB 20 DUP(0)
;start of data segment
MES1 DB 10,13,'WORD IS PALINDROME$'
MES2 DB 10,13,'WORD IS NOT PALINDROME$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:DATA
START: MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA SI,STR1
LEA DI,STR2+LEN-1
MOV CX,LEN
UP: CLD
LODSB
STD
STOSB
LOOP UP
LEA SI,STR1
LEA DI,STR2
CLD
MOV CX,LEN
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 16 | 57
REPE CMPSB
CMP CX,0H
JNZ NOTPALIN
LEA DX,MES1
MOV AH,09H
INT 21H
JMP EXIT
NOTPALIN: LEA DX,MES2
MOV AH,09H
INT 21H
EXIT: MOV AH,4CH
INT 21H
CODE ENDS
END START
ExNo:5 ii) Program to use DOS interrupt INT 21H function called for reading
a character from
keyboard, buffered keyboard input, display of character and string on
console.
Program:
DATA SEGMENT
INKEY DB ?
BUF DB 20 DUP(0)
MES DB 10,13, ' SRM UNIVERSITY $'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE , DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AH,01H
15CS205J/Microprocessors and Microcontrollers Lab CSE/SRM 17 | 57
INT 21H
;DOS function to read a character from keyboard ;with
echo. [AL = 8bit character]
MOV INKEY,AL ;Returns ASCII value of the pressed key.
MOV BUF,10
MOV AH,0AH
LEA DX,BUF
INT 21H
MOV AH,06H
MOV DL,'A'
INT 21H
MOV AH,09H
LEA DX,MES
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
Program:
DATA SEGMENT
HOUR DB ?
MIN DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
DISCHAR MACRO CHAR
PUSH AX
PUSH DX
MOV DL,CHAR
MOV AH,02
INT 21H
POP DX
POP AX
ENDM
START: MOV AX,DATA
MOV DS,AX
CALL TIME
MOV AH,4CH
INT21H
TIME PROC NEAR
MOV AH,2CH ;function to read system time
//Signed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Solution:
#include <reg51.h>
void T0M2Delay(void);
sbit mybit=P1^5;
void main(void){
unsigned char x,y;
while (1) {
mybit=~mybit;
for (x=0;x<250;x++)
for (y=0;y<36;y++) //we put 36, not 40
T0M2Delay();
}
}
void T0M2Delay(void){
TMOD=0x02;
TH0=-23;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
#include <reg51.h>
void T1M2Delay(void);
sbit mybit=P2^7;
void main(void){
unsigned char x;
while (1) {
mybit=~mybit;
T1M2Delay();
}
}
void T1M2Delay(void){
TMOD=0x20;
TH1=-184;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}
Write a program for the 8051 to transfer letter “A” serially at 4800
baud, continuously.
Solution:
Write a program in which the 8051 reads data from P1 and writes it to
P2 continuously while giving a copy of it to the serial COM port to be
transferred serially. Assume that XTAL=11.0592. Set the baud rate at
9600.
Solution:
ORG 0000H
LJMP MAIN
ORG 23H
ORG 30H