4th Semester Microprocessor and Microcontroller Lab Manual: Program:1 ALGORITHM: Binary Search
4th Semester Microprocessor and Microcontroller Lab Manual: Program:1 ALGORITHM: Binary Search
Program :1
Step 1: Initialize the data segment with the starting address of array.
Step 2: Store lower index of array in bx, higher index of array in dx and key in cx.
Step 3: Compare the lower and higher indices, if its low > high then jump to label FAILURE else find the middle index=
(low index+ high index)/2
Step 4: Compare the middle element with skey. If skey> = mid element jump to label BIGGER. Else get new higher
index as (middle index-1) and continue the process from label AGAIN.
Step 5: if skey =mid element goto label SUCCESS. Else find the new lower index as (index of mid element )+1 go to
table AGAIN to repeat process.
Step 6: Store the result and display label appropriate message and terminate the program.
;Program :1
;program to search a key in a list of n elements using binary search algorithm
DATA SEGMENT
array dw 1111h,2222h,3333h,4455h,5544h,6106h,7770h ;a is an array
;of 7 words
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:mov ax,data ;initialise data segment with
mov ds,ax ;starting address of data
mov bx,00h ;bx contains the low value
mov dx,len ;dx contains the high value
mov cx,SKEY ;key to be searched is moved
;to counter register
AGAIN:cmp bx,dx ;compare lower and higher
;indices
ja FAILURE ;if low>high jump to FAILURE
;label
mov ax,bx ;else move low value to ax
add ax,dx ;add low and high indices and
;store in ax
shr ax,01 ;divide the sum by 2 to get
;the mid value
mov si,ax ;store mid value in index
;register
add si,si ;double to get the mid
;element
cmp cx,array[si] ;if key greater than or equal
;to array's mid element
jae BIGGER ;jump to BIGGER label
dec ax ;else get new higher index
mov dx,ax ;store in dx reg
jmp AGAIN ;continue process from label AGAIN
BIGGER:je SUCCESS ;if skey=mid go to label SUCCESS
inc ax ;else find new lower index
mov bx,ax ;store it in bx reg
jmp AGAIN ;go to label AGAIN to repeat
process
SUCCESS:add al,01h ;to get position of element
add al,'0' ;convert to ascii
mov result,al ;store position in result
lea dx,msg1 ;load effective address of
;msg1 to dx
jmp DISP ;jump to label DISP
Step 4: OUTLOOP
Until all passes are over (bx=cx=0).
Step 5: INLOOP
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
mov ax,DATA ;Initialize the data segment with the
;starting address of data
mov ds,ax
mov bx,size1 ;store the array size to bx
dec bx
OUTLOOP:mov cx,bx ;save the count in cx register
mov si,00h ;initialize the pointer
INLOOP:mov al,a[si] ;load the data pointed to by si in al
inc si ;increment the pointer
CMP al,a[si] ;if the count of al is less than data
;pointed by si
jc NEXT ;jump to label NEXT for ascending order.
;jnc for descending
order.
xchg al,a[si] ;else exchange the 2 data in memory
mov a[si-1],al
NEXT :loop INLOOP ;continue till all the comparisons are
;over
dec bx
jnz OUTLOOP ;if all the passes are completed if yes
;jump to label OUTLOOP
mov ah,4ch
int 21h ;dos interrupt to terminate the program
CODE ENDS ;end of code segment
END START ;physical end of the program
ALGORITHM : Palindrome
Description : To read a string by user , reverse the string and check for palindrome.
Step 1: Declare buff,rbuff messages in data segment.
Step 2: Read the string from the user and store the string in buff and mark si to point the string.
Step 9: Terminate.
;Program :3
;read a string,reverse a string,check for palindrome and display the length
data segment
CR EQU 0dh ;carriage return=13
LF EQU 0ah ;line feed=10
BUFF db 20 dup(20)
RBUFF db 20 dup(20)
msg1 db CR,LF,"enter a string $"
msg2 db CR,LF,"REVERSE STRING=$"
msg3 db CR,LF,"input string is a palin$"
msg4 db CR,LF,"input string is not a palin$"
msg5 db CR,LF,"length of the string=$"
data ends
code segment
assume cs:code,ds:data
start: mov ax,data ;initialise ds with starting
mov es,ax ;address of data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h ;display
message1
lea dx,buff
mov ah,0ah
int 21h ;read
string and store in buff
lea si,buff+2 ;point si to buff
mov bl,buff+1
mov bh,00h
add si,bx ;add
si to length, to point to end
lea di,rbuff+2 ;point di to
rbuff+2
mov cx,bx
dec si ;point si
to last character
copynextchar:mov al,[si] ;copy buff to rbuff
mov [di],al
inc di
dec si
loop copynextchar
lea dx,msg2
; msg to display reverse string
mov ah,09h
int 21h
mov al,’$’
;add $ at end of rbuff to display
mov [di],al
lea dx,rbuff+2
mov ah,09h
int 21h
Program :4
ALGORITHM : nCr
then nCr=1
Step 3: Check if r=1
then nCr=n
Step 4: Check if r=n-1
then nCr=n
Step 5: If none of the above, then push, pop and decrement values of n and r until r=n or n-1 or 1.
Step 6: End.
;Program :4
;Program to compute ncr using recursive procedure.
;Assume that 'n' and 'r' are non-negative integers.
.model small
.stack
.data
n dw 4
r dw 2
ncr dw 1 dup(0)
.code
start:mov ax,@data ;initialise data segment with
mov ds,ax ;starting address of data
mov ax,n ;move n value to ax
mov bx,r ;move r value to bx
call ncrpro ;call procedure ncrpro
mov ah,4ch ;terminate the program
int 21h ;interrupt to os
ncrpro proc ;start of procedure ncrpro
cmp bx,ax ;check if r=n,then ncr=1
je res1 ;jump to res1 if they are equal
cmp bx,00 ;compare if r=0,if so ncr=1
je res1 ;jump to res1 if r=0
cmp bx,01h ;check if r=1,ncr=n
je resn ;jump to resn if r=1
dec ax ;decrement n to check if r=n-1
cmp ax,bx ;compare if r=n-1,ncr=n
je incncr ;jump to incncr if r=n-1
push ax ;push ax on to the stack
push bx ;push bx on to the stack
call ncrpro ;call procedure ncrpro
pop bx ;pop bx from the stack
pop ax ;pop ax from the stack
dec bx ;decrement r
push ax ;push ax on to the stack
push bx ;push bx on to the stack
call ncrpro ;call procedure ncrpro
pop bx ;pop the value from the stack and put it in bx
pop ax ;pop the value from the stack and put it in ax
ret ;return to the calling place
res1:inc ncr ;increment ncr by 1
ret ;return to the calling place
incncr:inc ncr ;increment ncr by 1
resn:add ncr,ax ;add ax to ncr
ret ;return to the calling place
ncrpro endp ;end of procedure ncrpro
end start ;physical end of program
Program :5
Step 1: Read system time using 2ch function number and int 21h interrupt.
Step 2: System time is loaded into cx , with hours in ch and minutes in cl
Step 3: unpack the digits using aam and display them one by one in the format hh : mm.
;Program :5
;Program to read the current time from the system and display it in standard format on
the screen
.model small
.stack 64h
.data
msg db "system time is $"
.code
start:mov ax,@data ;Initialize the data segment to the starting
;address
mov ds,ax ;of DATA
mov ah,09h ;dos function 09 to display the message
lea dx,msg
int 21h ;dos interrupt to end the function
mov ah,2ch ;dos function 02 to get the system time
int 21h
mov al,ch ;store the hour from ch to al
mov bl,01h
mul bl
aam
call disp ;to call the disp procedure
mov dl,':' ;to display : on the standard output device
mov ah,02h
int 21h
mov al,cl ;store minute from cl to al
mul bl
aam
call disp ;call the disp proc to disp the time
mov ah,4ch
int 21h
disp proc ;display procedure
push ax
mov dl,ah ;to display msb of the digit
add dl,30h
mov ah,02h
int 21h
pop ax
mov dl,al
add dl,30h ;to display the lsb of the digit
mov ah,02h
int 21h
ret ;return to the calling program
disp endp ;end of disp procedure
end start ;end of the program
Program :6
ADD64
LDR R0,=0X1234E640
LDR R1,=0X43210010
LDR R2,=0X12348900
LDR R3,=0X43212102
ADDS R4,R1,R3
ADC R5,R0,R2
NOP
NOP
BX LR
END
*/
;/* RESULT CAN BE VIEWED IN REGISTERS R5 (REMINDER) & R6(QUOTIENT)
*/
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT
*/
;/* PROGRAM WRITTEN BY ALS R&D TEAM BENGALURU
DATE:25/12/2013
*/
EXPORT divide
divide
MOV R0,#0 ;
INTIALISE R0 TO ZERO
LOOP1
SUB R4,R4,R2 ; SUBTRACT R2 IN
R4 & STORE IN R4
ADD R0,R0,#1 ; INCREMENT
THE R0 EVERYTIME BY 1
CMP R4,R2 ;
COMPARE NUMBERS IN R4 & R2
BGE LOOP1 ; IF
THE NUMBER in R4 > R2 THEN GOTO
;
LOOP1
LOOP2
MOV R5,R4 ;
CHECK REMINDER IN R5
MOV R6,R0 ;
CHECK QUOTIENT IN R6
ERROR
NOP
NOP
; MOV PC,LR
BX LR
; TWO 32 BIT NUMBERS 00000002h,00000008h
VALUE
DCD 0X00000002
; divider
(ALWAYS SMALLER)
DCD 0X00000008
; dividend (ALWAYS GREATER)
END
; Mark end of fil
EXPORT MULTIPLY
AREA MULTI , CODE , READONLY
MULTIPLY
LDR R0,=0X706F
LDR R1,=0X0161
MULS R2,R1,R0
NOP
NOP
MOV PC,LR
END
EXPORT SUB64
AREA SUNTRACT , CODE , READONLY
SUB64
LDR R0,=0X1234E640
LDR R1,=0X43210010
LDR R2,=0X12348900
LDR R3,=0X43212102
SUB R4,R1,R0
SBC R5,R3,R2
NOP
NOP
BX LR
END
START
LDR R1,=VALUE1 ; LOADS THE ADDRESS
OF FIRST VALUE
LDR R2,[R1] ; WORD ALIGN T0
ARRAY ELEMENT
NOP
NOP
NOP
VALUE1
DCD 0X54444444
;
END
; Mark end of file
ENTRY
;Mark first instruction to execute
START
LDR R1,=VALUE1 ; LOADS THE
ADDRESS OF FIRST VALUE
LDR R2,[R1] ; WORD ALIGN T0 ARRAY
ELEMENT
LDR R1,=VALUE2 ; LOADS THE
ADDRESS OF FIRST VALUE
LDR R3,[R1]
AND R2,R3
LDR R4,=RESULT ; LOADS THE ADDRESS
OF RESULT
STR R2,[R4] ; STORES
THE RESULT IN R2 AND ATT MEMORY 0X40000000
NOP
NOP
NOP
VALUE1
DCD 0X55AAAA55 ;
VALUE2
DCD 0X5555AA55 ;
START
LDR R1,=VALUE1 ; LOADS THE
ADDRESS OF FIRST VALUE
LDR R2,[R1],#4 ; WORD ALIGN T0 ARRAY
ELEMENT
MVN R2,R2
LDR R4,=RESULT ; LOADS THE ADDRESS
OF RESULT
STR R2,[R4] ; STORES
THE RESULT IN R2
NOP
NOP
NOP
VALUE1
DCD 0XC123 ;
ENTRY
;Mark first instruction to execute
START
LDR R1,=VALUE1 ; LOADS THE
ADDRESS OF FIRST VALUE
LDR R2,[R1] ; WORD ALIGN T0 ARRAY
ELEMENT
LDR R1,=VALUE2 ; LOADS THE
ADDRESS OF FIRST VALUE
LDR R3,[R1]
ORR R2,R3
LDR R4,=RESULT ; LOADS THE ADDRESS
OF RESULT
STR R2,[R4] ; STORES
THE RESULT IN R2 AND ATT MEMORY 0X40000000
NOP
NOP
NOP
VALUE1
DCD 0X55AAAA55 ;
VALUE2
DCD 0XAA5555AA ;
END
; Mark end of file
Program :7
TO Write and simulate the c program for ARM processor using KEIL.
PART B
HARDWARE PROGRAMS
Program :8a
Algorithm for BCD up-down counter :
Step 1: Initialise control word to 80h to use all ports on 8255 as output.
Step 3: Increment or add 1 to it and adjust to decimal value using DAA and display on port A.
Step 5: Decrement or subtract 1 from al (string down count) and adjust decimal value after subtraction using DAS.
Step 7: End.
8. a. Design and develop an assembly program to demonstrate BCD Up-Down Counter (00-99)
on the Logic Controller Interface.
.model small
.data
pa equ 0c800h ;address of port A
cwr equ 0c803h ;address of control word register
upcnt equ 64h ;count up from 00 to 99
dncnt equ 63h ;count down from 99 to 00
msg1 db "bcd up counter counts from 00 to",upcnt,"$"
msg2 db "bcd down counter counts from",dncnt,"to 00$"
.code
begin: mov ax,@data ;initialise the data segment with
mov ds,ax ;starting address of data
lea dx,msg1 ;load EA of msg1 to dx
mov ah,09h ;function to display string on monitor
int 21h ;interrupt to OS
mov al,80h ;initialise cw=80h to make all ports as
mov dx,cwr ;output by storing address of CWR in dx
out dx,al
mov cl,upcnt ;load counter with UPCNT
mov al,00h ;store initial value 00 to al
mov dx,pa ;store address of port A to dx and move the
up: out dx,al ;initial value to port
call delay1 ;call to procedure delay1
add al,01h ;add 1 to get next count
daa ;adjust decimal value of al after addition
cmp al,99h ;check if number=UPCNT
jz L1 ;if true,jump to l1 level
jmp up ;else jump to level up
L1: out dx,al ;direct last value to port and
call delay1 ; call delay1
lea dx,msg2 ;load EA of msg2 to dx
mov ah,09h ;function to display string on monitor
int 21h ;interrupt to os
mov al,dncnt ;store DNCNT to al
mov dx,pa ;store address of port A to dx
down: out dx,al ;direct the data to the port
call delay1 ;call to the procedure delay1
sub al,01 ;subtract 1from al to decrement data
das ;decimal adjust of al after subtraction
jz L2 ;if zero jump to level L2
jmp down ;else jump to level DOWN
L2: out dx,al ;move last value to port
call delay1 ;call delay1
stop: mov ah,4ch ;function to terminate
int 21h ;interrupt to os
delay1 proc ;start of procedure delay1
push bx ;push value of bx to stack
Multiplication 8bit.
Algorithm to read 8 bit inputs:
Step 1: Initialise CW= 82h to use port A as output and port B as input.
Step 7: End.
8 .b. Design and develop an assembly program to read the status of two 8-bit inputs (X & Y)
from the Logic Controller Interface and display X*Y.
dispmsg macro msg ;macro dispmsg with argument msg
lea dx,msg ;load EA of msg to dx
mov ah,09h ;function to display string on monitor
int 21h ;interrupt to os
endm ;end of macro
.model small
.data
cwr equ 0c803h ;address of control word register
pa equ 0c800h ;address of port A
pb equ 0c801h ;address of port B
msg1 db "set values for x on pb:$"
msg2 db 10,13,"set values for y input on pb:$"
msg3 db 10,13,"lsb result is:$"
msg4 db 10,13,"msb result is:$"
.code
begin: mov ax,@data ;initialise data segment with
mov ds,ax ;starting address of data
mov al,82h ;initialise CWR to 82h
mov dx,cwr ;i.e ,port A is output and
out dx,al ;port B is input
dispmsg msg1 ;invoke the macro dispmsg
call confirm ;call to the procedure confirm
out dx,al
mov ah,4ch ;function to terminate
int 21h ;interrupt to os
Program :9
Algorithm to display Messeges :
Step 2:use SI to the codes for character of FIRE and HELP in right to left format for display i.e., E is displayed first then R,I,F.
Step 3: load the character codes to port B and rotate it to display each segment (on or off).
Step 5: Use cx to store the numbers of characters=4 and display them segment wise.
Step 8: use bh to count the number of times the message has to be displayed.
Step 9: End.
9. Design and develop an assembly program to display messages “FIRE” and “HELP”
alternately with flickering effects on a 7-segment display interface for a suitable period of
time. Ensure a flashing rate that makes it easy to read both the messages (Examiner does not
specify these delay values nor is it necessary for the student to compute these values).
.model small
.data
fcode db 86h,88h,0f9h,8eh ;code for E,R,I,F
scode db 8ch,0c7h,86h,89h ;code for P,L,E,H
cwr equ 0c803h
pb equ 0c801h
pc equ 0c802h
.code
start: mov ax,@data ;initialise data segment
mov ds,ax ;with starting address of data
mov al,80h ;initialise CW=80h i.e.,make
mov dx,cwr ;all ports output by storing the
out dx,al ;address in dx
mov bh,0ah ;no. Of times to display
Step 4: if the accepted character is ‘c’ then go to step 5,otherwise if the accepted character is ‘a’ then go to step 9.
Step 5:initialise one register with count and another with clockwise sequence in al.
Step 7: rotate the number bit right by one and decrement the count.
Step 8: jump to step 6 till count is zero or else jump to step 13 to terminate.
Step 9: initialise again register with the count and another with anticlockwise sequence.
Step 10: Make number available at port C and create some delay.
Step 11: rotate number bit left by one bit and decrement the count.
Step 12:Jump to step 10 till count is zero, else jump to step 13 to terminate.
Step 13: Terminate
10. Design and develop an assembly program to drive a Stepper Motor interface and rotate the
motor in specified direction (clockwise or counter-clockwise) by N steps (Direction and N
are specified by the examiner). Introduce suitable delay between successive steps. (Any
arbitrary value for the delay may be assumed by the student).
.model small
.data
cnt equ 32h
acnt1 equ (cnt/10h)+'0'
acnt2 equ (cnt mod 10h)+'0'
msg1 db 10,13, "motor running in CW dir for",acnt1,acnt2,"h steps$"
msg2 db 10,13, "motor running in ACW dir for",acnt1,acnt2,"h steps$"
msg3 db 10,13, "enter 'C' for clockwise 'A' for anticlockwise:$"
pc equ 0c802h
cw equ 0c803h
.code
Begin: mov ax,@data ;initialise starting of data segment
mov ds,ax ;to starting of address of data
mov al,80h ;all ports o/p
mov dx,cw ;initialise CWR
out dx,al ;initialise control word register to make port
mov bh,cnt
mov bl,cnt
lea dx,msg3 ;load EA of msg3 to dx
mov ah,09h ;function to accept a charecter
int 21h ;does interrupt
mov ah,01h
int 21h
cmp al,'c' ;if character enterd in c then
jz clock ;jump to clock level
aclock: lea dx,msg2 ;to load first number for ASWR
mov ah,09h
int 21h ; interrupt
mov al,0eeh
L2: call step ;call to step procedure
rol al,01 ;rotate al to left by 1 bit position
dec bl ;decrement bl
jnz l2 ;jump if non zero to l2
jmp stop ;jump to stop level
clock: lea dx,msg1 ;load EA of msg3 to dx
mov ah,09h ;function to display a string
int 21h
mov al,077h ; move 077h to al register
l1: call step ;call procedure step
ror al,01
dec bh ;decrement bh
jnz l1
stop: mov ah,4ch ;function to terminate a program
int 21h
step proc ;start of step procedure
push bx ;push bx value onto the stack
push cx ;
mov dx,pc
out dx,al
mov bx,0ff01h
mov cx,0fa0fh
agn: loop agn ;loop the level again
dec bx
jnz agn ;jump on non zero to agn level
pop cx
pop bx
ret
step endp ;end of step procedure
end begin ;physical end of program
Program :11a
Step 1: store the value at each angle i.e., 0 to 90 in steps of 10 in an array with 127as reference .
Step 4: for Li i.e., from 0to 90 curve add 128 to angular values of array in an increasing order to get points on curve.
Step 5: for L2 i.e., from 90 to 180 curve,repeat 4 with decreasing values of array i.e., bx is decreasing till 0.
Step 6: for L3 i.e., from 180to 270 curve subtract array values from 128 in an increasing order to get the points.
Step 7: for L4: i.e., from 270 to 360 repeat step 6 but with decreasing array values starting from last point in L3 till 0.
Step 8 : repeat the above steps for some count ah*cx ie., 10h* FFFFh no. Of times.
Step 9: End.
11. Design and develop an assembly language program to
a. Generate the Sine Wave using DAC interface (The output of the DAC is to be
displayed on the CRO).
.model small
.data
v090 db 00,22,43,64,82,97,110,119,125,127
pa equ 0c800h
cwr equ 0c803h
.code
start: mov ax,@data ;initialise data segment with starting
mov ds,ax ;address of data
mov al,80h ;initialise CW=80h
mov dx,cwr
out dx,al ;storing address of port to dx
mov ah,10 ;number of times wave has to be generated
Program :11b
Algorithm for generation of half rectified sine wave
Step 1: store angular point values onto an array from 0 to 90 with 128 as reference in steps of 10.
Step 4: for L1,curve between 0to 90 ;add 128 to array angular values in decreasing order of bx to get 10 points till bx points to last.
Step 5: for L2,curve between 90 to 180 ;add 128 to array values in decreasing order of bx starting from L1’s last point till 0.
Step 6: for L3,L4 send constant reference values 128 to port A for 10 points in each curve.
Step 8: end.
11 . b. Generate a Half Rectified Sine waveform using the DAC interface. (The output of
the DAC is to be displayed on the CRO).
.model small
.data
v090 db 00,22,43,64,82,97,110,119,125,127
pa equ 0c800h
cwr equ 0c803h
.code
start: mov ax,@data ;initialise data segment with
mov ds,ax ;starting address of data
mov al,80h ;initialise CW= 80h
mov dx,cwr
out dx,al
mov ah,10 ;no. Of times waves has to be
;generated
l10: mov cx,0ffffh
mov bx,00h ;initialise bx to 00
mov dx,pa ;store address of port A to dx
Program :12 : To interface LCD with ARM processor-- ARM7TDMI/LPC2148. Write and execute
programs in C language for displaying text messages and numbers on LCD.
// LCD INTERFACING
//----------------------------------------------------------------
// CONTROLLER : LPC-2148
// DATE : December - 2015
// Developed By : Advanced Electronic Systems Bangalore,India
//----------------------------------------------------------------
//----------------------------------------------------------------
// Predefined data will be displayed on LCD
//----------------------------------------------------------------
#include<lpc214x.h>
#include<stdio.h>
//Function prototypes
void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);
int main()
{
PINSEL0 = 0X00000000; // configure P0.0 TO
P0.15 as GPIO
IO0DIR = 0x000000FC; //configure o/p lines
for lcd [P0.2-P0.7]
temp1 = 0x81;
//Display starting address of first line 2nd pos
lcd_com();
//function to send command to lcd
ptr = disp;
// pointing data
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data(); //function
to send data to lcd
ptr ++;
}
} //end of main()
void lcd_com(void)
{
temp = temp1 & 0xf0; //masking
higher nibble first
wr_cn();
temp = temp1 & 0x0f; //masking lower
nibble
temp = temp << 4;
wr_cn();
delay(500);
// some delay
}
Program :13 : To interface Stepper motor with ARM processor-- ARM7TDMI/LPC2148. Write a program to rotate stepper
motor.
#include <LPC21xx.h>
void clock_wise(void) ;
void anti_clock_wise(void) ;
int main(void)
{
PINSEL2 = 0x00000000; //P1.20 to P1.23 GPIO
IO1DIR |= 0x00F00000 ; //P1.20 to P1.23 made
as output
while(1)
{
void clock_wise(void)
{
var1 = 0x00080000; //For Clockwise
for( i = 0 ; i <= 3 ; i++ ) // for A B C D Stepping
{
var1 <<= 1 ;
IO1SET = var1 ;
// setting perticular bit
void anti_clock_wise(void)
{
var1 = 0x00800000 ;
//For Anticlockwise
IO1CLR =0x00F00000 ;
//clearing all 4 bits
IO1SET = var1 ;
IO1SET = var1 ;
// setting perticular bit
}
}