Вы находитесь на странице: 1из 42

R.V.

College of Engineering
Bangalore - 59
(Autonomous Institution affiliated to VTU, Belgaum)

Department of Electrical and Electronics


Engineering

Microprocessor & Microcontroller


16EE63

Laboratory Manual

( 2016 SCHEME )
Microprocessor And Microcontroller Lab
(12EE63)
List of Experiments
Program
Week Programmes
No
Data Transfer Programs: Block Moves (With & Without Overlap)
1
1 with &without String Instructions
A,B,C,D,E
& Data Exchange
2 Arithmetic Operations: Addition, Subtraction, Multiplication &
2
A,B,C,D Division on 32-Bit Data.

3 Code Conversions: Use XLAT Instruction to Convert Binary to


3
A,B,C BCD, Binary to ASCII, Binary to gray

4 (a) Linear Search (b)Binary Search.


4
A,B,C,D (c)Bubble Sort (d) Selection Sort

5 ASCII Operations: Addition, Subtraction, Multiplication & Division


5
A,B,C,D on 16-Bit Data.

6 6 UP/down counters –binary & BCD on flash 8051

7 7/8 Stepper motor /DAC interface

8 8/7 DAC/Stepper motor interface

9 9/10 Hex keypad & 7 segment display interface / Elevator interface

10 10/9 Elevator interface /hex keypad & 7 –segment display interface

11 11/12(a,b) LCD/ADC interface and program on timer

12 12(a,b)/11 ADC and program on timer /LCD interface


Program : 1

DATA TRANSFER PROGRAMS

a) Write 8086 Assembly Language Program to transfer block of data using string
instruction without overlap.
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred to array2
Step 2: Find the length of defined array1
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 to counter register
Step 5: Clear the direction flag, so that SI and DI are incremented by one automatically
Step 6: Move the contents of array1 to array2 until counter=0 using string instruction
Step 7: Terminate the program.
Program:
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $ - ARRAY1 ; finding length of array1
ARRAY2 DB 4 DUP (?) ; allocating memory for array2
. CODE
MOV AX, @ DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2
MOV CX, LEN ; taking length of array1 to CX register
CLD ; clear direction flag
REP MOVSB ; move string of bytes repeatedly from SI
;to DI location until CX=0
MOV AH, 4CH ; terminate a program
INT 21H
END

Output:
Run 1:
Keep on executing the program till the instruction given in italics through single
stepping (F8 key). Observe the contents of SI, DI and CX registers. The SI will be
initialized to starting address of array1. Similarly, DI will be initialized to starting
address of array2.The CX will be initialized to number elements of the array1.
To observe data transfer operation, examine the contents of data segment by
displaying its locations consisting of array1 and array2.
For example if SI=0000h, then DI=0007H
To display contents of data segment type command
After execution of remaining instructions of program, examine the contents of data
Segment to observe the contents array2 same as array1.
d ds: 00 15 (press enter key)
4AE1:0000 02 04 06 08 0A 05 00 02-04 06 08 0A 00 00 00 00
Run 2:

b) Write 8086 ALP to transfer block of data without using string instruction without
overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred to
array2
Step 2: Find the length of defined array1
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 to count register
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by destination
index
register
Step 6: Increment the index registers to point to next location
Step 7: Move the contents of array1 to array2 until count=0 using string instruction
Step 8: Terminate the program.
Program:
.MODEL SMALL
.DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $ - ARRAY1 ; finding length of array1
ARRAY2 DB 4 DUP (?) ; allocating memory for array2
.CODE
MOV AX, @ DATA ; Initialization of data segment
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2

MOV CX, LEN ; taking length of array1 to CX register


BACK: MOV AL, [SI] ; exchanging the content of SI and DI
; Until CX=0 by using AL register
MOV [DI], AL
INC SI
INC DI
LOOP BACK
MOV AH, 4CH ; to terminate a program
INT 21H
END

Output:

c) Write an ALP to transfer block of data without using string instruction with
overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred.
Step 2: Find the length of defined array1
Step 3: Move the length of array1 to count register
Step 4: Use index registers to point to last element of array1 and to a location above
the last element of the array1 by overlap amount (The program below
is written for overlap amount of 2)
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by
destination index register
Step 6: Decrement the index registers to point to next location
Step 7: Move the contents of array1 until count=0.
Step 8: Terminate the program.
Program
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $-ARRAY1 ; finding length of array1
.CODE
MOV AX, @DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES,AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV CX, LEN ; taking count to CX register
ADD SI, CX
DEC SI
MOV DI, SI
ADD DI, 02 ; overlapping occurs at second
; position in array1
BACK: MOV AL, [SI] ; exchanging the content of SI and DI
; Until CX=0 repeat loop back
MOV [DI], AL
DEC SI
DEC DI
LOOP BACK
MOV AH, 4CH ; to terminate a program
INT 21H
END
Output:

d) Write an ALP to transfer block of data using string instruction with overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred.
Step 2: Find the length of defined array1
Step 3: Move the length of array1 to count register
Step 4: Use index registers to point to last element of array1 and to a location above
the last element of the array1 by overlap amount (The program below
is written for overlap amount of 2)
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by
destination index register using string instruction.
Step 6: Terminate the program.
Program:
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
COUNT DW $-ARRAY1 ; finding length of array1
. CODE
MOV AX, @DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES,AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV CX, COUNT ; taking count to CX register
ADD SI, CX
DEC SI
MOV DI, SI
ADD DI, 02 ; overlapping occurs at second position
; in array1
STD ; set direction flag
REP MOVSB ; move string of bytes repeatedly from SI
; to DI location until CX=0
MOV AH, 4CH ; to terminate a program
INT 21H
END
Output:
e) Write an ALP to exchange block of data between locations of data segment.
Algorithm
Step 1: Initialize an array1 and array2 of N bytes in data segment
Step 2: Find the lengths of defined array1 and array2
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 or array2 which is greater to count register
Step 5: Move the contents of source index register to 8-bit register and the contents of
destination Index register to another 8-bit register
Step 6: Exchange the contents of the specified two 8-bit registers each other
Step 7: Increment the index registers to point to next location
Step 8: Move the contents of array1 to array2 until count=0 using string instruction
Step 9: Terminate the program.

Program:
. MODEL SMALL
. DATA
ARRAY1 DB 01H,02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H ;
LEN1 DW $- ARRAY1 ; finding length of array1
ARRAY2 DB 11H, 12H, 13H, 14H, 15H, 16H, 17H, 18H,19H ;array2 of 9
Elements
. CODE
MOV AX, @ DATA ; Initialization of DS and ES
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2
MOV CX, LEN1 ; taking count to CX register
REPT: MOV AL, [SI] ; move data pointed by SI to AL
MOV AH, [DI] ; move data pointed by DI to AH
MOV [SI], AH ; exchanging the data until CX=0
MOV [DI], AL
INC SI
INC DI
LOOP REPT
MOV AH, 4CH
INT 21H
END

Output:
Program : 2(WEEK 2)

PROGRAMS TO DEMONSTRATE ARITHMETIC


OPERATIONS

a) Write an ALP to add of two 32 bit numbers


Algorithm:
Step 1: Write the 32 bit augend and addend to the memory
Step 2: Read the LS 16 bits of input from the memory, add it and write the sum to the
output memory location.
Step 3: Read the MS 16 bits of inputs and add it with carry of previous addition.
Step 4: Move the sum to the output memory location
Step 5: Get the carry and move the carry to the output memory location
Step 6: Terminate the program
Program:
.MODEL SMALL
.DATA
X DW 6011H, 5022H ; 32 bit addend
Y DW 0AAAAH, 0BBBBH ; 32 bit augend
SUM DW 2 DUP(0) ; 32 bit location reserved for the sum
CARRY DB ? ; 8 bit location reserved for carry
.CODE
MOV AX,@DATA ; Initialize the data segment
MOV DS,AX
MOV AX,X+2 ; read the LS 16 bit augend and addend
MOV BX,Y+2 ; from the memory location X and Y
ADD AX, BX
MOV SUM, AX
MOV AX, X
MOV BX, Y
ADC AX, BX ;add the MS data word with carry
MOV SUM+2,AX
MOV AL, 00
ADC AL, 00
MOV CARRY,AL ; Get the carry and move store it.
. MOV AH, 4CH ; Terminate the Program
INT 21H
END
Output:
b) Write an ALP to subtract two 32 bit numbers
Algorithm
Step 1: Write the input values to the memory
Step 2: Subtract the lower 16 bit minuend from the subtrahend and write the
difference data to the memory location
Step 3: Subtract the higher 16 bit minuend from the subtrahend with the previous
borrow
Step 4: Write the output difference and final borrow to the memory
Step 5: Terminate the program
Program:
.MODEL SMALL
.DATA
X DW 0BBBBH, 0BBBBH ; Double data word subtrahend
Y DW 0AAAAH, 0BBBBH ; Double data word minuend
DIFF DW 2 DUP(0) ; Memory reserved for Difference and Borrow.
BORROW DB 1 DUP(0)
.CODE
MOV AX,@DATA ; initialize the data segment.
MOV DS,AX
MOV AX,X+2
MOV BX,Y+2 ;subtract LS dataword of Y from the LS
SUB AX,BX ;dataword of memory X and store the result
MOV DIFF,AX ;in memory location DIFF

MOV AX,X ; subtract the MS dataword of memory location


MOV BX,Y ;Y and X from the MS dataword of
;memory location X
SBB AX,BX ; move the difference
MOV DIFF+2,AX ;to memory location DIFF+2

MOV AL,00H ;display FF in memory location Borrow


;if final borrow is generated else display
;00h
SBB AL,00H
MOV BORROW, AL

MOV AH,4CH ;terminate the program


INT 21H
END
Output:
c) Write an ALP to perform multiplication of two 32 bit numbers.
Algorithm
Step 1: Initialize pointers to multiplier, multiplicand and product
Step 2: Multiply the lower 16 bits of operands and output the lower 16 bit product
and save the higher 16 bit product
Step 3: Multiply the lower 16 bit multiplier with the higher 16 bit multiplicand and
save the result
Step 4: Multiply the higher 16 bit multiplier data with lower 16 bit multiplicand
Step 5: Add the above product with the previous saved result and write the next 16
bit output product
Step 6: Multiply the higher 16 bit multiplicand and multiplier
Step 7:Add with the previous result values and write the higher 32 bit output
product to the memory
Program:
.MODEL SMALL
.DATA
N1 DW 2222H,1111H ;2222H is Most Significant Word(MSW)
N2 DW 4444H,3333H ;4444H is MSW & 3333H is LSW
RESULT DW 4 DUP(0)
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX

MOV SI,OFFSET N1
MOV DI,OFFSET N2
MOV BP,OFFSET RESULT

MOV AX,[SI+2] ;AX=N1 of LSW


MOV DX,[DI+2] ;DX=N2 of LSW
MUL DX
MOV DS:[BP],AX ; Store LSW of RESULT
MOV CX,DX
MOV AX,[SI+2] ;AX=N1 of LSW
MOV DX,[DI] ;DX=N2 of MSW
MUL DX
MOV BX,DX
ADD CX,AX
ADC BX,0000H
MOV AX,[SI] ;AX=N1 OF MSW
MOV DX,[DI+2] ;DX=N2 OF LSW
MUL DX
ADD CX,AX
ADC BX,DX
MOV DS:[BP+2],CX ;Store Second Word of RESULT
MOV CX,0000H
ADC CX,0000H
MOV AX,[SI] ;AX=N1 OF MSW
MOV DX,[DI] ;DX=N2 OF MSW
MUL DX
ADD AX,BX
ADC DX,CX
MOV DS:[BP+4],AX ;Store third Word of RESULT
MOV DS:[BP+6],DX ;Store MSW of RESULT
MOV AH,4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:

d) Write an ALP to divide 32 bit number by 16 bit number.


Algorithm:
Step 1: Initialize pointers to divisor & dividend.
Step 2: Divide the 32 bit dividend by 16 bit divisor.
Step 3: Store the quotient and remainder.
Program:
.MODEL SMALL
.DATA
N1 DW 1234H,5678H ;DIVIDEND
N2 DW 0FFFFH ;DIVISOR
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV DX,[SI] ;DX=N1 of MSW
MOV AX,[SI+2] ;AX=N1 of LSW
MOV BX,[DI] ;BX=N2(DIVISOR)
DIV BX
MOV [DI+2],AX ;[DI+2]=QUOTIENT
MOV [DI+4],DX ;[DI+4]=REMAINDER
MOV AH,4CH
INT 21H
END
Output:

Program : 3

PROGRAMS TO DEMONSTRATE CODE CONVERSION


SING XLAT
a) Write an ALP to convert Binary numbers into its equivalent BCD value
Algorithm:
Step 1: Display the message on the console which asks the input value
Step 2: Accept the input values entered through keyboard
Step 3: Convert the input and translate the input into its equivalent using lookup table
Step 4: Display a message and display the output BCD value (MS and LS nibble
output) on the screen.
Step 5: Ask the user to terminate or to continue conversion
Step 6: Accept the user decision and continue conversion or terminate the program

Program:
.MODEL SMALL
.DATA
MSG1 DB 0DH, 0AH,"ENTER THE INPUT TO BE CONVERTED :( 0H-FH)",'$'
MSG2 DB 0DH, 0AH,"BCD EQUIVALENT OF BINARY IS:",'$'
MSG3 DB 0DH, 0AH,"DO U WANT TO CONTINUE: Y/N",'$'
LK_UP DB
00H,01H,02H,03H,04H,05H,06H,07H,08H,09H,10H,11H,12H,13H,14H,15H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX

AGAIN: LEA DX,MSG1 ;ask for input to be converted


MOV AH,09H
INT 21H
MOV AH,01H ;accept the input to be converted
INT 21H
SUB AL,30H ;convert the input
CMP AL,0AH
JNA NXT1;check if the input >9
SUB AL,07H ;get the hex equivalent of ASCII input >9

NXT1: LEA BX, LK_UP


XLAT;translate the input in to its equivalent
PUSH AX
CMP AL,09H
JNA NXT2
LEA DX,MSG2
MOV AH,09H ;display the msg2
INT 21H
POP AX
MOV BL,AL
AND AL,0F0H ;mask the lower nibble output data
MOV CL,04H
ROR AL,CL
ADD AL,30H ;get the ascii value of ms nibble to display on
;console
MOV DL,AL
MOV AH,02H ;display the upper nibble output
INT 21H
MOV AL,BL
AND AL,0FH ;mask the higher ( ms nibble)
ADD AL,30H ;get the ASCII equivalent value of lower nibble
MOV DL,AL
MOV AH,02H ;display the lower nibble
INT 21H
JMP LAST

NXT2: LEA DX,MSG2


MOV AH,09H ;display the msg2
INT 21H
POP AX
ADD AL,30H
MOV DL,AL
MOV AH,02H ;display the output
INT 21H

LAST: LEA DX,MSG3 ;ask whether to terminate or continue


;conversion
MOV AH,09H
INT 21H
MOV AH,01H ;accept the decision
INT 21H
CMP AL,"Y";continue executing the program once again
; if button Y is pressed
JE AGAIN
MOV AH,4CH ;terminate the program
INT 21H
END
Output:

b) Write an ALP to convert Binary numbers into its equivalent ASCII value
Algorithm:
Step 1: Display the message on the console which asks the input value
Step 2: Accept the input values entered through keyboard
Step 3: Convert the input and translate the input into its ASCII equivalent using lookup
table
Step 4: display the output ASCII value (MS and LS nibble output) on the VDU(Video
Display Unit-SCREEN)
Step 5: Prompt the user to enter Y/N to continue or terminate.
Step 6: Accept the user decision and continue conversion or terminate the program

.MODEL SMALL ; Create a small model with each segment size of 64


KB
.STACK 64 ; Initialize the stack segment register for 64 locations.
.DATA ; Direct the assembler to data segment.
MSG1 DB 0DH, 0AH,"ENTER THE INPUT TO BE CONVERTED:(00H-FH)",'$'
MSG2 DB 0DH, 0AH,"ASCII EQUIVALENT OF BINARY IS:",'$'
MSG3 DB 0DH, 0AH,"DO U WANT TO REPEAT: Y/N",'$'
LK_UPDB 30H,31H,32H,33H,34H,35H,36H,37H,38H,39H,41H,42H,43H,44H,45H, 46H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
AGAIN: LEA DX,MSG1 ;ask for input to be converted
MOV AH,09H
INT 21H
MOV AH,01H ;accept the input to be converted
INT 21H
SUB AL,30H ;convert the ASCII inpu
CMP AL,0AH
JNA NXT1;Check whether the input is greater than 9
SUB AL,07H
NXT1: LEA BX,LK_UP
XLAT;translate the input in to its equivalent
PUSH AX
LEA DX,MSG2
MOV AH,09H
INT 21H
POP AX
MOV BL,AL
AND AL,0F0H ;mask the ls nibble to display ms nibble on the
;vdu
MOV CL,04H
ROR AL,CL
ADD AL,30H ;get the ascii equivalent value of ms nibble
MOV DL,AL ;display it on vdu
MOV AH,02H
INT 21H
MOV AL,BL
AND AL,0FH
ADD AL,30H
MOV DL,AL ;display the ls nibble on the vdu
MOV AH,02H
INT 21H
LEA DX,MSG3 ;ask whether to terminate or continue
MOV AH,09H
INT 21H
MOV AH,01H ;accept the decision
INT 21H
CMP AL,"Y"
JE AGAIN
MOV AH,4CH ;terminate the program
INT 21H
END

Output:

c) Write an ALP to convert Binary numbers into its equivalent gray value.
Algorithm
Step 1: Display the message on the console to prompt user to enter the binary value.
Step 2: Accept the input values entered through keyboard.
Step 3: Convert the input and translate the input into its gray equivalent using lookup
table
Step 4: Display a message and display the output GRAY value (MS and LS nibble
output) on the VDU
Step 5: Ask the user to terminate or to continue conversion.
Step 6: Accept the user decision and continue conversion or terminate the program.
Program:
.MODEL SMALL
.STACK 64 ; initialize the stack segment
.DATA
MSG1 DB 0DH,0AH,"ENTER THE INPUT TO BE CONVERTED:(00H-FH)",'$'
MSG2 DB 0DH,0AH,"GRAY EQUIVALENT OF BINARY IS:",'$'
MSG3 DB 0DH,0AH,"DO U WANT TO REPEAT: Y/N",'$'
LK_UP DB
00H,01H,03H,02H,06H,07H,05H,04H,12H,13H,15H,14H,10H,11H,09H,08H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX

AGAIN: LEA DX,MSG1 ;ask for input to be converted


MOV AH,09H
INT 21H
MOV AH,01H ;accept the input to be converted
INT 21H
SUB AL,30H ;convert the ASCII input
CMP AL,0AH
JNA NXT1
SUB AL,07H ;get the hex value if the input is >9

NXT1: LEA BX,LK_UP


XLAT;translate the input in to its equivalent
PUSH AX
LEA DX,MSG2
MOV AH,09H
INT 21H;display the message2 on console
POP AX
MOV BL,AL
AND AL,0F0H
MOV CL,04H
ROR AL,CL
ADD AL,30H
MOV DL,AL
MOV AH,02H ;display the MS nibble of the output data
;on VDU.
INT 21H
MOV AL,BL
AND AL,0FH
ADD AL,30H
MOV DL,AL
MOV AH,02H ;display the ls nibble output on vdu
INT 21H
LEA DX,MSG3 ;ask whether to terminate or continue
MOV AH,09H
INT 21H
MOV AH,01H ;accept the decision
INT 21H
CMP AL,"Y"
JE AGAIN
MOV AH,4CH;terminate the program
INT 21H
END
Output:

Program : 4

PROGRAMS TO DEMONSTRATE SEARCHING & SORTING

a) Write an ALP to find an element in Array of Elements using linear search

Algorithm
Step 1: Initialize the SEARCHKEY.
Step 2: Compare the search key with the first element of the array
Step 3: Stop the search and go to step7 if the search key is found else go to step4
Step 4: If all the elements of the array are not searched then go to step5 else go to step8
Step 5: Compare the next element of the array & if search key is found goto step 7.
Step 6: Repeat step4 if the search key is not found
Step 7: Display the Success message
Step 8: Display the Fail message
Program:
.MODEL SMALL
.DATA
ARRAY DB 55H,33H,44H,66H,22H
LENTH DW $-ARRAY
SRCHKEY EQU 55H
SUCMSG DB 'ELEMENT 55 FOUND AT POSITION:'
RESULT DB ? ,'$'
FAILMSG DB 'ELEMENT 55 NOT FOUND','$'

.CODE

MOV AX,@DATA
MOV DS,AX
MOV ES,AX
CLD
MOV DI,OFFSET ARRAY
MOV BX,OFFSET ARRAY
MOV AL,SRCHKEY
MOV CX,LENTH
REPNE SCASB
JE SUCCESS
LEA DX, FAILMSG
JMP DISPLAY
SUCCESS:
MOV AX,DI
SUB AX,BX
ADD AL,'0'
MOV RESULT,AL
LEA DX,SUCMSG
DISPLAY:
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
END
Output & Efficiency:

b) Write an ALP to find an element in Array of Elements using binary search


Algorithm
Step 1: Select the median (middle element) of the array.
Step 2: Compare the median value with the target value and determining if it is greater
than, less than, or equal to the target value.
Step 3: A guessed index whose value turns out to be too high becomes the new upper
bound of the span
Step 4: Make the median as the upper bound of assumed new array if the target element
is less in the above comparison
Step 5: Make the median as the lower index of the new assumed array if the target
element is greater than the median element
Step 6: Pursuing this strategy iteratively which soon finds the target value or else
determines that it is not in the list at all.
Program:
.MODEL SMALL
.DATA
ARRAY DW 1122H,2345H,3344H,4343H,8367H,9555H,9599H
LENTH DW ($-ARRAY)/2
SRCHKEY EQU 9555H
SUC_MSG DB "ELEMENT FOUND AT POSITION:", '$'
RESULT DB ?
FAIL_MSG DB 0DH, 0AH," ELEMENT NOT FOUND",'$'

.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
MOV BX,1
MOV DX,LENTH
MOV CX,SRCHKEY

AGAIN: CMP BX,DX


JA FAILURE

MOV AX,BX
ADD AX,DX
SHR AX,1

MOV SI,AX ; Point to the middle (mean data) of the array


DEC SI
ADD SI,SI

CMP CX,ARRAY[SI]
JAE BIGER
DEC AX
MOV DX,AX
JMP AGAIN

BIGER: JE SUCCESS ; Assume a new array


INC AX
MOV BX,AX
JMP AGAIN

SUCCESS: ADD AL,'0'


MOV RESULT,AL
MOV CL,AL
LEA DX,SUC_MSG
MOV AH,09H
INT 21H
MOV DL, CL
MOV AH, 02H
INT 21H
JMP QUIT

FAILURE: LEA DX,FAIL_MSG


MOV AH,09H
INT 21H ; Function to display the message on console

QUIT: MOV AH,4CH


INT 21H
END
Output & Efficiency:

c) Write a program to sort an array using bubble sort


Algorithm
Step 1: Initialize pointer to source, inner counter, outer counter.
Step 2: Compare each element of an array with its next element, if element is lesser, goto
step 4, otherwise goto step 3.
Step 3: Exchange the element and its next element in the array.
Step 4: Decrement the inner counter and check the other elements of the array
Step 5: Decrement the outer counter for zero value or the array is in ascending order and
stop sorting the data
Program:
.MODEL SMALL
.DATA
ARRAY DB 54H,67H,90H,87H,43H
LEN DW ($-ARRAY)

.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX ; Initialize the segment registers

MOV BX,LEN ;Outer loop iteration control variable


OUTLOOP: MOV SI,OFFSET ARRAY
MOV CX,LEN
DEC CX

INLOOP: MOV AL,[SI]


CMP AL,[SI+1]
JBE NO_SWAP ; Don’t swap if the data is in ascending
; order
XCHG AL,[SI+1]
MOV [SI],AL

NO_SWAP: INC SI
LOOP INLOOP
DEC BX
JNZ OUTLOOP

MOV AH,4CH
INT 21H
END ; Terminate the program
Output & Efficiency:

d) Write an ALP to sort an array using selection sort


Algorithm:
Step 1: We wish to sort the array in descending order.
Step 2: Repeatedly find the smallest element in the array and move it to its final position
in the sorted array.
Step 3: Begin this by selecting the smallest element and moving it to the highest index
position.
Step 4: Do this by swapping the element at the highest index and the smallest element.
Step 5: Reduce the effective size of the array by one element by leaving the last element
and repeat the process on the smaller (sub)array.
Step 6: The process stops when the effective size of the array becomes 1.
Given array: 55, 11, 66, 22, 33
After 1st pass: 55, 33, 66, 22, 11
After 2nd pass: 55, 33, 66, 22, 11
After 3rd pass: 55, 66, 33, 22, 11
After 4th pass: 66, 55, 33, 22, 11 (Sorted array)
`Program:
.MODEL SMALL
.DATA
ARRAY DB 11H,44H,22H,33H
LEN DW $-ARRAY

.CODE
MOV AX,@DATA ;initialize Data Segment
MOV DS,AX

MOV DX,LEN
DEC DX

OUTLOOP: MOV CX, DX


MOV SI, 0
MOV AH,ARRAY[SI]
MOV BX, SI

INLOOP: INC SI ;to point next element in the series


;increment no of comparisons made
CMP AH, A[SI] ;compare the element with the next
; element
JB AHEAD
MOV AH, A[SI] ;exchange
MOV BX, SI

AHEAD: LOOP INLOOP


XCHG AH,A[SI]
MOV A[BX],AH
DEC DX ;decrementeddx,if dx not equal to zero then
JNZ OUTLOOP ; Complete the sorting

MOV AH,4CH
INT 21H
END

Output & Efficiency:


Program : 5

PROGRAMS TO DEMONSTRATE ASCII OPERATIONS

a) Write a program for ASCII addition of two 16-Bit data


Algorithm:
Step 1: Store 16 bit ASCII numbers in data segement.
Step 2: Add LSBs of the operand store in AL.
Step 3: Use AAA to convert result in to unpacked BCD.
Step 4: Add MSBs of the operands with carry of previous addition and adjust the result.
Step 5: Display the result by converting back to ASCII.
Program
.MODEL SMALL
.DATA
N1 DB 39H, 36H
N2 DB 38H, 37H
ASUM DB 4 DUP(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES,AX
MOV DI, OFFSET ASUM
MOV AH, 00H
MOV AL, N1+1
MOV BL, N2+1
ADD AL, BL
AAA
MOV DX, AX
MOV AH, 00H
MOV AL, N1
MOV BL, N2
ADC AL, BL
AAA
ADD DL, 30H
MOV [DI], DL
ADD AX,3030H
MOV [DI+1], AL
MOV [DI+2], AH
MOV AH, 4CH
INT 21H
END
Output:

b) Write a program for ASCII subtraction of two 16-Bit data


Algorithm:
Step 1: Store 16 bit ASCII numbers in data segment.
Step 2: Subtract LSBs of the operand store in AL.
Step 3: Use AAS to adjust AX after subtraction.
Step 4: Subtract MSBs of the operands with carry of previous addition and adjust the
result.
Step 5: Display the result by converting back to ASCII.
Program
.MODEL SMALL
.DATA
N1 DB 32H, 38H
N2 DB 38H, 33H
ASUB DB 4 DUP(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES,AX
MOV DI, OFFSET ASUB
MOV AH, 00H
MOV AL, N1+1
MOV BL, N2+1
SUB AL, BL
AAS
MOV DL, AL
MOV AH, 00H
MOV AL, N1
MOV BL, N2
SBB AL, BL
AAS
ADD DL, 30H
MOV [DI],DL
ADD AX,3030H
MOV [DI+1], AL
MOV [DI+2], AH
MOV AH, 4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:

Note:2F Indicates the result is negative, when larger number is subtracted from the smaller
number

c) Write an ALP for ASCII Multiplication of two 16-Bit data


Program
.MODEL SMALL
.DATA
N1 DB 09H,08H
N2 DB 07H,06H
ASCPROD DW 4 DUP(?)
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
MOV DI, OFFSET ASCPROD
MOV AX,0000H
MOV AL,N1+1
MUL N2+1
AAM
MOV [DI],AL
MOV CL,AH
MOV AL, N1
MUL N2+1
AAM
MOV DX, AX
MOV AL, N1+1
MUL N2
AAM
ADD AL,CL
DAA
ADD AL,DL
DAA
MOV DL, AL
AND AL, 0FH
MOV [DI+1],AL
MOV AL, DL
AND AL, 0F0H
MOV CL, 04H
SHR AL, CL
MOV CH,AH
ADD CH,AL

MOV AL, N1
MUL N2
AAM
ADD AL,DH
DAA
ADD AL, CH
DAA
MOV DL,AL
AND AL,0FH
MOV [DI+2], AL
MOV CL, 04
MOV AL,DL
AND AL,0F0H
SHR AL,CL
ADD AL,AH
MOV [DI+3],AL
MOV AH,4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:
d) Write a program for ASCII division of 16-Bit data by 8 Bit data
Program
.MODEL SMALL
.DATA
N1 DW 0608H
N2 DB 09H
QUO_REM DW ?
.CODE
MOV AX,@DATA
MOV ES,AX
MOV DS,AX
MOV DI,OFFSET QUO_REM
MOV AX, N1
AAD
DIV N2
MOV [DI],AX
MOV AH,4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:
8051 Microcontroller
Program : 6

6. BCD UP/DOWN counter program in C. To display the values with certain delay
on the onboard 7 – segment display unit of flash 8051 (Philips 89C61X2) board.
// Follow indentation in every C program when practicing, writing programs in the record and
also on the data sheet.
#include<reg51.h> // to access ports, timer registers etc.,

unsigned char dig7[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


void main(void)
{
void delay(unsigned int);
int i=0, i1, i2, k; // i = 99 for down
P11 =0, P10 = 0; // configured as output
while(1)
{
i2 = i%10; // LS digit, unit place
i1 = i/10; // MS digit, tenth place
i++; // decrement for DOWN
for(k=0;k<125;k++) // loop repeated for 0.5 s
{
P0= dig7[i1]; P1 = 0x10; // place data on P0 and enable right LED
delay(250); // data on the bus for 2 ms.
P0= dig7[i2]; P1 =0x20; // P1 – bit 0 enables right LED, bit 1 enables left
LED (MSD)
delay(250);
}
if(i >99) // i < 0
i= 0;
else
;
}
}

void delay(unsigned int n)


{
unsignedint i; // can you write without i? Possible, How?
for (i=0; i<n;i++)
; // null executed. Is there a statement in C?
}

Program : 7

7. Program, in C, to rotate the Stepper motor by specified angle / turn continuously


step by step, clockwise or counterclockwise.
// by an angle. As per specifications, motor makes 200 steps / revolution or 1.8 for one step.
For 45, no of steps motor has to rotate is 25 (decimal). As motor is excited 4 times, count
value is 6.25 = 7.
#include <reg51.h>
void main()
{
void delay(unsigned int);
unsigned char x1[] = {0x03,0x09,0x0C,0x06};
unsigned char i;
P2 = 0x00; // configuring P2 as out
While(1)
{
for (j=0;j<=7;j++)
for(i=0;i<4;i++)
{ P2 = x1[i];
Delay(2000);
}

// forever wait until restart or reset


}
}

void delay(unsigned int n)


{
unsigned int i;
for(i=0;i< n ;i++);
}

//clockwise or counter clockwise depending upon switch status read through P1.6

#include <reg51.h>
unsigned char x1[] = {0x03,0x09,0x0C,0x06},temp;
//unsigned char x2[] = {0x03,0x01,0x09,0x08,0x0C,0x04,0x06, 0x02};
// x2 array for alternate phase excitation
void main()
{
voidclk_wise();
voidcclk_wise();
void delay(unsigned int);
P2 = 0x00; // configuring P2 as out
while(1)
{
temp = P1 & 0x40;
//delay(100);
if(temp!= 0)
clk_wise();
else
cclk_wise();
}
//while(1);

void delay(unsigned int n)


{
unsigned int i;
for(i=0;i< n ;i++);
}
voidclk_wise(void) // note data array is global
{
unsigned int i;
for(i=0;i<4;i++)
{

P2 = x1[i];
delay(5000);
}
}
void cclk_wise(void)
{
unsigned int i;
for(i=0;i<4;i++)
{

P2 = x1[3-i];
delay(5000);
}
}
Program : 8

8. To generate Square, triangular, ramp, staircase, and sine waveforms using a DAC
card interfaced to the flash board.
void main(void)
{
void delay(int);
unsigned char ch =0x00;
P0 = 0x00; P1 = 0x00; // ports P0, P1 are configured as OUT
while(1)
{
P0= ch;
P1= ch; // required only if Dual DAC card is used
delay(50);
P0 =~ch;
P1= ch;
delay(50);
}
}
void delay(int n)
{
int i;
for(i=0;i<n;i++)
;
}
Ramp / Triangular
#include<reg51.h>
void main(void)
{
unsigned char chu =0x00, chd = 0xff;
//unsigned flag= 0xFF;
while(1)
{
/*while(1) // commented to observe negative ramp, check initial value of chu
{
P0= chu;
chu++;
if(chu == 0xff)
break;
else
;
}*/
while(1)
{
P0= chd;
chd--;
if (chd == 0x00)
break;
else
;
}
} // end of forever while
} // end of main
// staircase signal
#include<reg51.h>
void main(void)
{
void delay(int);
unsigned char ch[] = {0x00, 0x33,0x66,0x99,0xCC,0xFF}; // corresponds to 0, 1, 2 , …5 V
Modify the program to get staircase signal in either direction.
unsigned char i =0;
while(1) // Can it be implemented with one loop?
{ // Don’t think of complex logic, use loops 2 or 3?
P0= ch[i];
//P1= ch[i];
delay (10);
i++;
if(i>6)
i=1;
else
;
}
}
// sine signal For 5 V output signal range, let V = 5 sin be the required signal. Calculate
voltage values for every 30 angle between 0 & 360. For 256 steps, steps per volt = 256/5 =
51.2. Multiply the voltage values by 51.2 to get integer values of the sine function. Replace
the char array by these values and change index, according to the number of samples, to view
crude sine wave.

9.Interfacing a hex keypad and seven segment display unit to 8051 flash board and
to realize simple calculator operations.

// save the following code as header file with name kdata.h


unsigned char LEDCODE[16]= { 0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71 };
unsigned int pos[] = {0x00,0x04,0x08,0x0c};
unsigned char row_data[] = {0xfe,0xfd,0xfb,0xf7};
unsigned char dig_disp[] = {0x8f,0x9f,0x0af,0x0bf,0x0cf,0x0df};
unsigned char scancode[16]={0xEE, 0xED, 0xEB, 0xE7,
0xDE, 0xDD, 0xDB, 0xD7,
0xBE, 0xBD, 0xBB, 0xB7,
0x7E, 0x7D, 0x7B, 0x77 };

// save the following code as header file with name kdisp.h


void digit_disp(unsigned int x )
{
int i=0; // x number to be displayed
unsigned int i1, i2 ;
if(x<100)
{
i1 = x/10;
i2 = x%10;
}
else
{
x = x -100;
i1 = x/10;
i2 = x%10;
}
for(i=0;i<500;i++)
{
P2 = 0x00;
P0 = LEDCODE[i2];
delay(250);
P2 = 0x10;
P0 = LEDCODE[i1];
delay(250);
}
P2 = 0xff;
}
void disp_op(unsigned int w)
{
int i;
for(i=0;i<250;i++)
{
P2 = 0x20;
P0 = LEDCODE[w];
delay(500);
}
P2 =0xff;
}
// save the following code as header file with name kpdrd.h

unsigned int key_rd(void)


{
unsigned char t1, t2, res, flag;
unsigned int i, num;
while(1)
{
do // check for key press by grounding all lines
{
P1 = 0xf0;
t2 = P2;
t2 = t2 & 0x0f;
} while(t2 == 0x0f);
delay(1000);

flag =0x00; // flag set to 0 to find which row key is pressed


for(i=0;i<4;i++) // row grounding
{
P1=row_data[i];
t2 = P2;
t2 = t2 & 0x0f;
if(t2 != 0x0f) // if t2 is not F, key pressed set flag to oxff
{
flag = 0xff;
break; // break from for loop
}
}
t2 = P2;
t2 = t2 & 0x0f;
t1 = P1;
t1 = t1 << 4;
t1 = t1 & 0xf0;
res = t1 | t2; // read once again key & find scan code
flag = 0x00;
for( i=0;i<16;i++)
{
if (res == scancode[i])
{
flag = 0xff; // for the key press, find position of key
num = i; // using the scan code table.
break;
}
}
if(flag ==0xff)
break; // break from while loop
} // end of while
return num; // return the key position
} // happens to be the char or number pasted on the key
// save the following code as header file with name delay.h

void delay(unsigned int n)


{
int i;
for(i=0;i<n;i++)
;
}

Main program of calculator

#include<reg51.h>
#include<kdata.h>
#include <kpdrd.h>
#include<del.h>
#include<kdisp.h>
void delay(int n);

void main(void)
{
unsigned int k,i1,i2,n1,n2,op, n, nxt;
do
{
nxt =14;
disp_op(nxt); // display E on 4th LED as prompt to enter number
k =11;
// read 1 st number
do
{
k = key_rd();
i1=k;
} while(k>10); // read a digit between 0 & 9 MSD
delay(65535); // come down to user speed to feed number
k =12;
do
{
k = key_rd();
i2 =k;
} while(k>11);// reads LSD
delay(65535);
n1 = i1*10+i2; // construct the number, 2 - digit
digit_disp(n1); // displayed on right corner
// read the operator
nxt = 14;
disp_op(nxt); // prompt to enter operator
op=6;
do
{
k = key_rd();
op =k;
}while(op<10);
delay(65535);
// read 2nd number
nxt =14;
disp_op(nxt);
k =11;
while(k>10)
{
k = key_rd();
i1 = k; // read MSD
}
delay(65535);
k =12;
while(k>11)
{
k = key_rd(); // read LSD
i2 = k;
}
delay(65535);
n2 = i1*10+i2;
digit_disp(n2);
switch(op)
{
case 10: n =n1+ n2;
break;
case 11: n =n1- n2;
break;
case 12: n = n1*n2;
break;
case 13: if (n2!=0)
n= n1/n2;
else
n = 0x00;
break;
default: n= n1%n2;
break;
}
disp_op(op);
digit_disp(n);
op =6; // to wait for proper op
}while(1);
}

10.Interfacing an elevator model to the 8051 flash board and to study the elevator
operations.

// code for the header file elevdata.h


unsigned char flr_ind[] = {0xff, 0xf0,0xf3,0xff,0xf6, 0xff,0xff,0xff, 0xf9};
unsigned char flr_no[] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char fclr[] = {0xff,0xE0,0xD3,0xff,0xB6,0xff,0xff,0xff,0x79};
//
code for del.h
void delay(unsigned int n)
{
int i;
for(i=0;i<n;i++)
;
}

#include <reg51.h>
#include<eledata.h>
#include<del.h>
main()
{
unsigned char reqflr,curflr = 0x01,i,j; // note all var’s are unsigned char
P0 = 0xf0;
while(1)
{
P1 = 0x0f; // lower nibble of P1 made input port
reqflr = P1 | 0xf0; // read floor request & append f (for upper nibble)
// loop until request appears
while (reqflr == 0xff)
reqflr = P1 | 0xf0; // Read Request Floor from P1

reqflr = ~ reqflr; // complement request becomes a power of 2 num

if(curflr == reqflr) // If Request floor is equal to Current Floor


{
P0 = fclr[curflr];
delay(65535); // Clear Floor Indicator
} // Go up to read again
else
if(curflr>reqflr) // If Current floor is > request floor
{
i = flr_no[curflr] - flr_no[reqflr]; // gives the floor difference 1,2,4,8…..
j = flr_ind[curflr]; // get the respective floor byte to glow led
for(;i>0;i--) /*Move the indicator down */
{
P0 = j; // keep displaying LED’s
j--;
delay(32767); // a delay of 0.5 sec
}
}
else // If Current floor is < request floor
{
i = flr_no[reqflr] - flr_no[curflr]; // Get the no of floors to travel
j = flr_ind[curflr];
for(;i>0;i--) // Move the indicator Up
{
P0 = j;
j++;
delay(32767);
}
} // end of else
curflr = reqflr; //request floor becomes the current floor
P0 = fclr[curflr]; // Clear the indicator
}
}

READ PORTA AS (PORT 0) PORTB AS PORT 1

Program : 11Interfacing the given external LCD unit to the 8051 flash board and to
display the given string on line 1 or line 2.
#include<reg51.h>
sbit rs = P2^4;
sbit rw = P2^5;
sbit en = P2^6;

void main()
{
void delay(unsigned int n);
void lcdcmd(unsigned char cmd); // command function
void lcddata(unsigned char d); // write data function
unsigned char x[] = {"Hello RV"}; // string to be displayed
unsigned char i = 0;
lcdcmd(0x3C); // command 0x38 for 1 line & 5X7 dots ;
delay(100); // write time required for LCD to set the function (40 s)
lcdcmd(0x0e);
lcdcmd(0x01); // clears display screen
delay(100);
lcdcmd(0x06); // shift display to right after each char dispaly
delay(100);
//lcdcmd(0x0C); // cursor off & display on
//delay(100);
lcdcmd(0x80); // cursor off & display on
delay(100);
for(i=0;i< 8;i++) // time for sending data
{
lcddata(x[i]); // display the string of length 8
delay(1000);
}

/* lcdcmd(0xC0); // sets cursor on the 2nd half line


delay(50); // display your name by sending 8 char of your name
*/
while(1); // after displaying enters infinite loop
}

void lcdcmd(unsigned char cmd)


{
P0= cmd;
rs =0; // register select 0
rw = 0; // read / write 0 for writing
en = 1; // high to low pulse
delay(20);
en = 0;
return;
}

void lcddata(unsigned char d)


{
P0 = d;
rs = 1; // for data 1
rw = 0;
en = 1;
delay(20);
en = 0;
return;
}

void delay(unsigned int n)


{
unsigned int i;
for(i=0;i<n;i++)
;
}

Program : 12-aInterfacing the given ADC with temperature sensor to the 8051 flash
board and to display temperature suitably.

adc_data.h
//adc.data
Unsigned char adc_table[55]=
{ 0x00, 0x03,0x05,0x08, 0x0a,0x0d,0x0f, 0x11,0x14,0x16,
0x19,0x1c,0x1e, 0x21,0x24,0x27, 0x2a,0x2c,0x2e, 0x32,
0x34, 0x36, 0x39, 0x3c, 0x3f, 0x42, 0x45, 0x48, 0x4a, 0x4c,
0x4e, 0x50, 0x52,0x54, 0x56,0x58,0x5b, 0x61, 0x64, 0x67,
0x6a, 0x6d,0x70, 0x72,0x74,0x77, 0x7a,0x7d,0x7f, 0x82,
0x85,0x87,0x8a,0x8d,0x90}

unsigned char LEDCODE[16]= { 0x3f, 0x06, 0x5b, 0x4f,


0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71 };

#include <reg51.h>
#include<adc_data.h>
#include<del.h>
sbit start = P3^5;
sbitch_sel = P3^6;
sbiteoc = P3^3;
void main ()
{
idata unsigned char adc_val,temperature,i;
P2 = 0xff; // P2 as input
ch_sel = 0x00; // channel 1 selection Wr = 1 setb p3.6
start = 0; // clr p3.5
delay (200);
while (1)
{
start = 1;
delay(200);
start = 0; // start of conversion initiated
while (eoc != 1) ; // wait for eoc = 0
delay(200);// after eoc, read the adc data from P2

adc_val = P2;// get adc value


for(i=0;i<100;i++)
{
if(adc_val<=adc_table[i])
{
temperature = i;
break;
}
}
for(i=0;i<125;i++) // displaying digits of temperature
{
P1 =0x10;
P0 = LED_CODE[temperature/10];
delay(250);
P1 =0x20;
P0 = LED_CODE[temperature%10];
delay(250);
}
} // end of while(1)

Program : 12-bTo toggle all bits of P2 continuously every 500 ms, using timer 1 in mode
#include<reg51.h>
void main(void)
{
void timer delay(void);
unsigned char x;
P2 = 0x55;
while(1)
{
P2 =~ P2;
for(x =0;x<20;x++)
timer_delay();
}
}

void timer_delay(void)
{
TMOD = 0x10; // timer 1 in mode 1
TL1 = 0xFe; // loading values
//A5FE = 42494; 65536 - 42494 = 23042 X 1-085 = 20 ms.
TH1 = 0xA5;
TR1 =1;
while(TF1==0); // wait for flag to roll over
TR1 =0; // turn off timer1
TF1 =0; // clear flag
}

Вам также может понравиться