0% found this document useful (0 votes)
136 views

4th Semester Microprocessor and Microcontroller Lab Manual: Program:1 ALGORITHM: Binary Search

This program demonstrates an algorithm to check if a string is a palindrome by reversing the string and comparing it to the original. It performs the following steps: 1. Read a string from the user and store it in a buffer. Store the length of the string. 2. Copy the characters from the end of the string to a reverse buffer, reversing the order. 3. Display the original and reversed strings. Compare the two buffers and print if the string is a palindrome or not. 4. Print the length of the original string.

Uploaded by

Paras Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

4th Semester Microprocessor and Microcontroller Lab Manual: Program:1 ALGORITHM: Binary Search

This program demonstrates an algorithm to check if a string is a palindrome by reversing the string and comparing it to the original. It performs the following steps: 1. Read a string from the user and store it in a buffer. Store the length of the string. 2. Copy the characters from the end of the string to a reverse buffer, reversing the order. 3. Display the original and reversed strings. Compare the two buffers and print if the string is a palindrome or not. 4. Print the length of the original string.

Uploaded by

Paras Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

4th Semester Microprocessor and Microcontroller Lab Manual

Program :1

ALGORITHM : Binary Search


Description : Search a key element with the starting address of data.

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

LEN dw ($-array)/2 ;lenth of the array is


;calculated
SKEY EQU 4455h ;search key is equated to
;4455h
msg1 db "element is found at " ;if search is successful
;display msg and result
result db ' rd/st position',13,10,"$"
msg2 db "element is not found $" ;if search is unsuccessful
;display this msg
DATA ENDS

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

FAILURE:lea dx,msg2 ;if unsuccessful load


;effective
;address of msg2 to dx
DISP:mov ah,09h ;to interrupt display
int 21h ;dos interrupt to terminate
;the program
mov ah,4ch ;and go back to prompt
int 21h ;interrupt to os
CODE ENDS ;end of code segment
END START ;physical end of program
Program :2

ALGORITHM : Bubble Sort

Step 1: Array of n elements is declared.


Step 2: Initialize data segment with starting address of data.
Step 3: Get pass number to bx, comparision index to cx

Step 4: OUTLOOP
Until all passes are over (bx=cx=0).
Step 5: INLOOP

Compare previous and next numbers.


Step 6: if Next is greater than previous we get carry don’t swap .
Step 7: For descending order JNC.

Step 8: Jump to next label.


Step 9: Else exchange.
Step 10: Repeat INLOOP.

Step 11: Until pass number is zero.


;Program :2
;Program to sort a given set of ‘N’ numbers in ascending order using the bubble sort
algorithm
DATA SEGMENT
a db 23h,45h,55h,22h,64h ;a is an array of 5 elements
size1 dw ($-a) ;to find number of bytes
X dw 5 dup(0) ;make next blocks in memory to zero
DATA ENDS

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

NOTE: For word operation modify as under in data segment


a dw 1234h,4312h,3214h,2314h,1342h
size1 dw ($-a)/2
In code segment replace AL by AX
Inc si ;twice to point to next word
Mov a[si-2],ax ;in place of mov a[si-1],al
Program :3

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 3: Store the length of the string in bl.


Step 4: Make si to point to the last character of string.
Step 5: Copy each character pointed by si and keep decrementing it and store it in rbuff locations. Finally end the rbuff
by ‘$’ sign.

Step 6: Display the original and reversed string.


Step 7: Compare buff and rbuff. If they are same, print the message “String is palindrome”. Else print the message
“Not a palindrome”.
Step 8: Print the length of 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

lea si,buff+2 ;point si


to buff,di to rbuff,
lea di,rbuff+2 ;cl to
length
mov ch,00h
mov cl,bl
cld
;DF=0 to make si di to increment
repe cmpsb
;compare [si]and[di]till cl=0
jnz l1
lea dx,msg3
;if equal display msg3
jmp l2
l1:lea dx,msg4 ;
else display msg4

l2: mov ah,09h


int 21h
lea dx,msg5 ; display
length msg
mov ah,09h
int 21h
add bl,’0’
mov dl,bl
mov ah,02h
int 21h
;convert length to ascii
mov ah,4ch
int 21h
code ends
end start

Program :4

ALGORITHM : nCr

Step 1: Store n and r values to ax and bx respectively.


Step 2: Call procedure.
nCr pro procedure

Step 1: Check if n=r


then nCr=1
Step 2: Check if r=0

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

ALGORITHM : Current time

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

NOTE:Also use mov ah,2ah;to accept system date and display


int 21h

Program :6

; program to add two words


; R1,=0X43210010 + R3,=0X43212102 = R4 =
0x8642212
; R0,=0X1234E640 + R2,=0X12348900 = R5 = 0x24696f40
;
EXPORT ADD64
AREA ADDITION , CODE , READONLY

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

//Mark end of file

;/* PROGRAM TO Divide TWO NUMBERS


*/
;/* TAKE TWO NUMBERS 0X00000002, 0X00000008

*/
;/* 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

AREA DIVISION , CODE, READONLY

divide

MOV R0,#0 ;
INTIALISE R0 TO ZERO

LDR R1,=VALUE ; LOADS


THE ADDRESS OF FIRST VALUE
LDR R2,[R1],#4 ; WORD ALIGN T0
ARRAY ELEMENT (First
;
element)
LDR R4,[R1],#4 ; WORD ALIGN T0
ARRAY ELEMENT (Second
; element)
CMP R2,R4
BHI ERROR

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

; program to multiply two 32 bit numbers


; r0 = 0x706F
; r1 = 0X0161
; r2 = r0 * r1 = 0X009B090F
; check the result in r2 register

EXPORT MULTIPLY
AREA MULTI , CODE , READONLY

MULTIPLY

LDR R0,=0X706F
LDR R1,=0X0161
MULS R2,R1,R0
NOP
NOP
MOV PC,LR

END

//Mark end of file

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

//Mark end of file

;PROGRAM TO TRANSFER DATA FROM CODE AREA TO DATA AREA


; COMPILE AND DEBUG PROGRAM
; SET BREAKPOINT AT NOP INSTRUCTION
; PRESS F5 TO RUN THE PROGRAM
; HAVE MEMORY1 WINDOW OPENED AND SET ADDRESS AT 0X40000000
; AND AFTER EXECUTION CHECK 0X44444444 APPEAR AT MEMORY1 WINDOW

AREA LARGEST , CODE, READONLY

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 R4,=RESULT ; LOADS THE


ADDRESS OF RESULT
STR R2,[R4] ; STORES THE
RESULT IN R2

NOP
NOP
NOP

; ARRAY OF 32 BIT NUMBERS(N=7)

VALUE1
DCD 0X54444444
;

AREA DATA2,DATA,READWRITE ; TO STORE


RESULT IN GIVEN ADDRESS
RESULT DCD 0X0

END
; Mark end of file

;PROGRAM TO DEMONSTRATE LOGICAL OR INSTRUCTION


; COMPILE AND DEBUG PROGRAM
; SET BREAKPOINT AT NOP INSTRUCTION
; PRESS F5 TO RUN THE PROGRAM
;STEP THROUGH THE PROGRAM AND FIND
;FIRST OPERNAD AT R2= 55AAAA55
;SECOND OPERNAD AT R3= 5555AA55
; AND AFTER EXECUTION CHECK REGISTER R2 = 5500AA55
; AND AT 0X40000000 5500AA55 AT MEMORY 1 WINDOW

AREA LARGEST , CODE, READONLY

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

; ARRAY OF 32 BIT NUMBERS(N=7)

VALUE1
DCD 0X55AAAA55 ;

VALUE2
DCD 0X5555AA55 ;

AREA DATA2,DATA,READWRITE ; TO STORE RESULT IN


GIVEN ADDRESS
RESULT DCD 0X0
END ; Mark end of file
;PROGRAM TO DEMONSTRATE LOGICAL NOT INSTRUCTION
; COMPILE AND DEBUG PROGRAM
; SET BREAKPOINT AT NOP INSTRUCTION
; PRESS F5 TO RUN THE PROGRAM
; AND AFTER EXECUTION CHECK REGISTER R1 = FFFF3EDC

AREA LARGEST , CODE, READONLY

ENTRY ;Mark first instruction to


execute

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

; ARRAY OF 32 BIT NUMBERS(N=7)

VALUE1
DCD 0XC123 ;

AREA DATA2,DATA,READWRITE ; TO STORE RESULT IN


GIVEN ADDRESS
RESULT DCD 0X0

END ; Mark end of file

;PROGRAM TO DEMONSTRATE LOGICAL OR INSTRUCTION


; COMPILE AND DEBUG PROGRAM
; SET BREAKPOINT AT NOP INSTRUCTION
; PRESS F5 TO RUN THE PROGRAM
;STEP THROUGH THE PROGRAM AND FIND
;FIRST OPERNAD AT R2= 55AAAA55
;SECOND OPERNAD AT R3= AA5555AA
; AND AFTER EXECUTION CHECK REGISTER R2 = FFFFFFFF
; AND AT 0X40000000 FFFFFFFF AT MEMORY 1 WINDOW

AREA LARGEST , CODE, READONLY

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

; ARRAY OF 32 BIT NUMBERS(N=7)

VALUE1
DCD 0X55AAAA55 ;

VALUE2
DCD 0XAA5555AA ;

AREA DATA2,DATA,READWRITE ; TO STORE RESULT IN


GIVEN ADDRESS
RESULT DCD 0X0

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 2: Store 00(start) to al register.

Step 3: Increment or add 1 to it and adjust to decimal value using DAA and display on port A.

Step 4: Compare al with last count and display it.(upcount)

Step 5: Decrement or subtract 1 from al (string down count) and adjust decimal value after subtraction using DAS.

Step 6: Display each value on port A until 00.(down count)

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

push cx ;push value of cx to stack


mov cx,0ffffh ;move 1FFFh to cx register
loop1: mov bx,0fffh ;move FFFFh to bx register
loop2: dec bx ;subtract 1 from content of bx
jnz loop2 ;if bx!=0then jump to L2 level
loop loop1 ;else loop the loop1 level
pop cx ;pop the value from stack and store in cx
pop bx ;pop the value from stack and store in bx
ret ;return
delay1 endp ;end of delay procedure
end begin ;end of begin
Program :8b

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 2: Read X value from port B and store in cl.

Step 3: Read y value from port B and read to al.

Step 4: Multiply X with Y and store result in ah(MSB)+al(LSB).

Step 5: Display LSB output in al on port A.

Step 6: Display MSB output in ah on port A .

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

again: mov dx,pb ;store the address of port B to dx


in al,dx ;read the input (X)
mov cl,al ;store the value of X to cl
dispmsg msg2 ;invoke the macro to display msg2
call confirm ;call to the procedure confirm
mov dx,pb ;store the address of port B to dx
in al,dx ;and rad value of Y
mov ah,00h ;initialise ah=00,to store result on multiplication
mul cl ;al*cl=>al+ah(16 bit)
push ax ;push ax value to stack
dispmsg msg3 ;invoke the macro dispmsg
call confirm ;call to the procedure confirm
pop ax ;pop the value from stack and store in ax
mov dx,pa ;move address of port A to dx
out dx,al ;display LSB on port A
push ax ;push value of ax to stack
dispmsg msg4 ;invoke the macro dispmsg
call confirm ;call to the procedure confirm
pop ax ;pop value from stack and store in ax
mov dx,pa ;move address of port A to dx
mov al,ah ;display MSB on port A

out dx,al
mov ah,4ch ;function to terminate
int 21h ;interrupt to os

confirm proc ;start of the procedure confirm


l1: mov ah,01h ;function to read character
int 21h ;interrupt to os
cmp al,0dh ;check if character =carriage return
jnz l1 ;if not again jump to l1 and read
ret ;return
confirm endp ;end of procedure endp
end begin ;end of begin

Program :9
Algorithm to display Messeges :

Step 1:initialise CW = 80h to use all ports as output.

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 4: Repeat step 3 untill segment of a single character using bl register .

Step 5: Use cx to store the numbers of characters=4 and display them segment wise.

Step 6: display the charecters starting from rightmost to left most.

Step 7: display FIRE and HELP alternatively with delay in between.

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

again: mov si,offset fcode ;point SI to E


call display1 ;display FIRE
call delay1 ;call to delay procedure
mov si,offset scode ;point SI to P
call display1 ;display HELP
call delay1 ;call to delay procedure
dec bh ;decrement value of bh
cmp bh,00h ;check if bh=0
jnz again ;if not ,jump to again level
.exit ;terminate DOS
display1 proc ;start f procedure display1
mov cx,04h ;store the number of chars=4 to cx

dnextc:mov bl,08h ; store no. Of segments =8 to bl


mov al,[si] ; store the first character to al
nexts: rol al,01h ;rotate it to 1bit left
mov dx,0c801h ;end it to the port B
out dx,al ;load data to port
push ax ;push ax value to stack
mov al,0ffh ;send the clock pulse data
mov dx,0c802h
out dx,al
mov al,00h
out dx,al ;load data to port
pop ax ;pop value out of the stack and store
dec bl ;in ax and decrement seg no. bl
jz nextc ;if all the seg. of clear are 1’s jump to nextc
jmp nexts ;else jump to nexts level
nextc: inc si ;point to new character
loop dnextc ;repeat until all the chars are displayed
ret ;return to previous instruction

display1 endp ;end of display1 procedure

delay1 proc ;start of delay procedure


push ax
push cx
mov cx,0aaah ;use ax and cx to store delay loops
loop1: mov ax,0fffah ;and count to decrement them to
loop2: dec ax ;generate delay
jnz loop2
loop loop1
pop cx
pop ax
ret ;return to calling place
delay1 endp ;end of delay procedure

end start ;end of start


Program :10
Algorithm to drive stepper motor:
Step 1: initialise data segment with starting address of data.

Step 2: print the message to display the directions to be rotated.

Step 3: make all the ports as outputs.

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 6:Create delay loop using two register.

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

Algorithm for generation of full sine wave with DAC(0830)

Step 1: store the value at each angle i.e., 0 to 90 in steps of 10 in an array with 127as reference .

Step 2: use bx to point to angular values in array.

Step 3: for using all ports as output(port A is used) initialise CW=80h.

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

l10: mov cx,0ffffh


mov bx,00h ;point bx to 1st value of wave
mov dx,pa ;store the address of port A to dx

l1: mov al,v090[bx] ;store first value to al


add al,128 ;add 128 to that values
out dx,al ;send it to port A
inc bx ;increment bx to point to next value
cmp bx,09h ;check if pointing to last value
jb l1 ;if not jump to L1 level

l2: mov al,v090[bx] ;store the last value to al register


add al,128 ;add 128 to it
out dx,al ;send the data to port
nop ;no operation for 3 cycles per second
dec bx ;points to previous value
jnz l2 ;if doesn’t point to value jump to L2

l3: mov al,128 ;else store 128 value to al


sub al,v090[bx] ;subtract 1st value from 128
out dx,al ;send data to port
inc bx ;point to next value
cmp bx,09 ;check if last value is pointed
jb l3 ;if not jump to L3

l4: mov al,128 ;store 128 to al


sub al,v090[bx] ;subtract last value from 128
out dx,al ;send it to port C
nop ;no operation for 3cycles per second
dec bx ;point to previous value
jnz l4 ;if not zero jump to l4 level
loop l1 ;else repeat wave
dec ah ;decrement ah
jnz l10 ;if not zero repeat l10
mov ah,4ch ;function to terminate
int 21h ;interrupt to os
end start ;physical end of program

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 2: initialise CW=80h i.e, to use port A as output.

Step 3: initialise bx=0 to point to array values.

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 7: repeat wave for specified count.

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

l1: mov al,v090[bx] ; store 1st value to al


add al,128 ;add 128 to it
out dx,al ;send it to the port
inc bx ;point to next value
cmp bx,09h ;check the last value
jb l1
l2: mov al,v090[bx] ;store last angle value to al
add al,128 ;add 128 to it
out dx,al ;send it to the port
nop ;no operation for 3 cps
dec bx ;point to previous value
jnz l2 ;if not zero,jump to L2 level

l3: mov al,128 ;store 128 to al


inc bx ;send it to the port
cmp bx,09h ;increment to next place
jb l3 ;if not jump to l3 level

l4: mov al,128 ;store 128 to al


out dx,al ;send it to the port
nop ;no operation for 3cps
dec bx ;point to previous place
jnz l4 ;if non zero,jump to L4 level
loop l1 ;generate wave again till cx
dec ah ;decrement count
jnz l10 ;if not zero,jump to l10 level
mov ah,4ch ;function to terminate
int 21h ;interrupt to os

end start ;physical end of program

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);

unsigned char temp1;


unsigned long int temp,r=0;
unsigned char *ptr,disp[] = "ALS BENGALURU",disp1[] = "LCD INTERFACING";

int main()
{
PINSEL0 = 0X00000000; // configure P0.0 TO
P0.15 as GPIO
IO0DIR = 0x000000FC; //configure o/p lines
for lcd [P0.2-P0.7]

lcd_init(); //lcd intialisation


delay(3200); //
delay 1.06ms
clr_disp();
//clear display
delay(3200); // delay 1.06ms

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 ++;
}

temp1 = 0xC0; // Display


starting address of second line 1st pos
lcd_com(); //function to
send command to lcd

ptr = disp1; // pointing second


data
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data(); //send data to lcd
ptr ++;
}
while(1);

} //end of main()

// lcd initialisation routine.


void lcd_init(void)
{
temp = 0x30; //command to
test LCD voltage level
wr_cn();
delay(3200);

temp = 0x30; //command to


test LCD voltage level
wr_cn();
delay(3200);

temp = 0x30; //command to


test LCD voltage level
wr_cn();
delay(3200);

temp = 0x20; // change to 4 bit mode from default 8 bit


mode
wr_cn();
delay(3200);

temp1 = 0x28; // load command for lcd function setting


with lcd in 4 bit mode,
lcd_com(); // 2 line and 5x7 matrix display
delay(3200);

temp1 = 0x0C; // load a command for display on, cursor on


and blinking off
lcd_com();
delay(800);

temp1 = 0x06; // command for cursor increment after data


dump
lcd_com();
delay(800);

temp1 = 0x80; // set the cursor to beginning of line 1


lcd_com();
delay(800);
}

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
}

// command nibble o/p routine


void wr_cn(void) //write command reg
{
IO0CLR = 0x000000FC; // clear the port
lines.
IO0SET = temp;
// Assign the value to the PORT lines
IO0CLR = 0x00000004; // clear bit RS = 0
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008; //E=0
}
// data nibble o/p routine
void wr_dn(void) ////write data reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the
value to the PORT lines
IO0SET = 0x00000004; // set bit RS = 1
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008; //E=0
}

// data o/p routine which also outputs high nibble first


// and lower nibble next
void lcd_data(void)
{
temp = temp1 & 0xf0; //masking higher
nibble first
temp = temp ;
wr_dn();
temp= temp1 & 0x0f; //masking lower nibble
temp= temp << 4; //shift 4bit to left
wr_dn();
delay(100);
}

void clr_disp(void) // function to clear


the LCD screen
{
temp1 = 0x01;
lcd_com();
delay(500);
}

void delay(unsigned int r1) // delay function using for


loop
{
for(r=0;r<r1;r++);
}

Program :13 : To interface Stepper motor with ARM processor-- ARM7TDMI/LPC2148. Write a program to rotate stepper
motor.

// STEPPER MOTOR INTERFACING


//--------------------------------------------------------------
// CONTROLLER : LPC-2148
// DATE : JULY - 2016
// Developed By : Advanced Electronic Systems Bangalore,India
//----------------------------------------------------------------
//-------------------------------------------------------------------
// A stepper motor direction is controlled by shifting the voltage across
// the coils. Port lines : P1.20 to P1.23
//-------------------------------------------------------------------

#include <LPC21xx.h>

void clock_wise(void) ;
void anti_clock_wise(void) ;

unsigned int var1 ;


unsigned long int i = 0 , j = 0 , k = 0 ;

int main(void)
{
PINSEL2 = 0x00000000; //P1.20 to P1.23 GPIO
IO1DIR |= 0x00F00000 ; //P1.20 to P1.23 made
as output

while(1)
{

for( j = 0 ; j < 50 ; j++ ) // 50 times in Clock


wise Rotation
clock_wise() ;
// rotate one round clockwise

for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show


anti_clock Rotation

for( j=0 ; j < 50 ; j++ ) // 50 times in Anti


Clock wise Rotation
anti_clock_wise() ;
// rotate one round anticlockwise

for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show


ANTI_clock Rotation

}// End of main

void clock_wise(void)
{
var1 = 0x00080000; //For Clockwise
for( i = 0 ; i <= 3 ; i++ ) // for A B C D Stepping
{
var1 <<= 1 ;

IO1CLR =0x00F00000 ; //clearing all 4


bits

IO1SET = var1 ;
// setting perticular bit

for( k = 0 ; k < 3000 ; k++ );


//for step speed variation
}

void anti_clock_wise(void)
{
var1 = 0x00800000 ;
//For Anticlockwise

IO1CLR =0x00F00000 ;
//clearing all 4 bits

IO1SET = var1 ;

for( k = 0 ; k < 3000 ; k++ ) ;

for( i = 0 ; i < 3 ; i++ ) // for A B C D


Stepping
{
var1 >>=1;
//rotating bits
IO1CLR =0x00F00000 ; //
clar all bits before setting

IO1SET = var1 ;
// setting perticular bit

for( k = 0 ; k < 3000 ; k++ ) ; //for step speed


variation

}
}

Dept of CS & E , BIT Page 1

You might also like