Microcontroller Lab Programs
Microcontroller Lab Programs
ORG 00h
MOV R0,#30h ;Initialize source memory pointer
MOV R1,#40h ; Initialize destination memory pointer
MOV R2,#05h ; Initialize counter
loop: MOV A,@r0 ; move source data to accumulator
MOV @r1,A ; move accumulator data to destination
INC R0 ; Increment source memory pointer
INC R1 ; Increment destination memory pointer
DJNZ R2,loop ; Decrement R2 if not zero jump to loop
start: SJMP start
END
EX:
Input Output
(30) = 01 (40) = 01
(31) = 02 (41) = 02
(32) = 03 (42) = 03
(33) = 04 (43) = 04
(34) = 05 (44) = 05
2) Write an assembly language program to transfer n =5 bytes of
data from external memory to internal memory (without overlap).
ORG 00h
MOV DPTR,#0f00h ; Data pointer to external RAM
memory
MOV R1,#40h ; Data pointer to internal RAM memory
MOV R2,#5 ; Initialize counter
loop: MOVX A,@DPTR ; move source data to accumulator
MOV @R1,A ; Move accumulator data to destination
INC R1 ; Increment source memory pointer
INC DPTR ; Increment destination memory pointer
DJNZ R2,loop ; Decrement R2 if not zero jump to loop
start: SJMP start
END
EX:
Input: Output
(0F00) = 01 (40) = 01
(0F01) = 02 (41) = 02
(0F02) = 03 (42) = 03
(0F03) = 04 (43) = 04
(0F04) = 05 (44) = 05
3) WAP to interchange ‘N’ bytes of data between internal RAM
locations X and Y.
Logic: Initialize pointers R0 to location X and pointer R1 to location Y.
The count ‘N’ is stored in R2. The contents of location X is exchanged with
loc Y through accumulator the pointers are incremented by 1, counter R2 is
decremented by 1. If R2 !=0 repeat the procedure.
ORG 00h
MOV R0,#30H ;Initialize source memory pointer
MOV R1,#40H ;Initialize destination memory pointer
MOV R2,#5 ; Initialize counter
loop: MOV A,@R0 ; Move source data to accumulator
XCH A,@R1 ; Exchange accumulator data with dst data
MOV @R0,A ; Move accumulator data to source
INC R0 ; Increment source memory pointer
INC R1 ; Increment destination memory pointer
DJNZ R2,loop ; Decrement counter if not zero jump to loop
EX:
Inputs: outputs:
(30) = 01 (40) = 11 (30) = 11 (40) = 01
(31) = 02 (41) = 12 (31) = 12 (41) = 02
(32) = 03 (42) = 13 (32) = 13 (42) = 03
(33) = 04 (43) = 14 (33) = 14 (43) = 04
(34) = 05 (44) = 15 (34) = 15 (44) = 05
4)WAP to add two 16 bit no.
ORG 00h
MOV R0,#20h ; R0 pointing to lsb of data1
MOV R1,#30h ; R1 pointing to lsb of data2
MOV A,@R0 ; move contents of 20h to acc.
ADD A,@R1 ; Add lsbs
MOV 40h,A ; Store lsb of result in 40h
INC R0 ; (r0) = (r0)+1
INC R1 ; (r1) = (r1)+1
MOV A,@R0 ; Move msb of data1 to acc.
ADDC A,@R1 ; Add msbs with carry
MOV 41h,A ; Store the result in 41h
MOV 42h,#00h ; Clear 42h to save carry
JNC start ; If carry is not set jump
MOV 42h,#01h ; store carry bit in 42h
start: SJMP start
END
EX:
Input: Output
ORG 00h
MOV R0,#20h ; R0 pointing to lsb of data1
MOV R1,#30h ; R1 pointing to lsb of data2
MOV A,@R0 ; move contents of 20h to acc
SUBB A,@R1 ; Subtract lsbs
MOV 40h,A ; Store lsb of result in 40h
INC R0 ; (r0)=(r0)+1
INC R1 ; (r1)=(r1)+1
MOV A,@R0 ; Move msb of data1 to acc.
SUBB A,@R1 ; Subtract msbs with borrow
MOV 41h,A ;Store msb of the result in 41h
start: SJMP start
END
Input: Output:
(20) = 45 ( 30) = AC (40) = 99
(21) = 38 (31) = 23 (41) = 14
5) Write a program to add N bytes of data from location X. Store
the result in locations LOCY and LOCY+1.
ORG 00h
MOV R0,#30h ; R0 pointing to array of data
CLR A ; Clear accumulator
MOV R2,#05h ; Move count into R2
MOV R3,#00h ; Clear R3 to save carry
Repeat: ADD A,@R0 ; Get one byte of data from memory
JNC Next ;If no carry jump
INC R3 ;If there is carry increment R3
Next: INC R0 ; Either way increment pointer
DJNZ R2,Repeat ; Decrement counter if not zero .
MOV 40h, A ;Move sum in accumulator to 40h
MOV 41h,R3 ; move carry accumulated to 41h.
Here: SJMP Here
END
Logic: Divide the given no. by 100d. The quotient of the division is
hundredth position digit. Divide the remainder by 10d. The quotient is tenth
position digit and the remainder is the units position digit.
ORG 00h
start: MOV R0,#40h ; R0 pointing to src data
MOV A,@R0
MOV 0F0h,#64h ; load B reg with 100d or 64h
DIV AB ; a=a/b (a=a/100d)
INC R0 ;( r0) = (r0) + 1
MOV @R0,A ; Store decimal digit in hundred
; position in 41h
MOV A,0F0h ; Remainder of first division is
; moved to acc.
MOV 0F0h,#0Ah ; load B reg with 10d or 0ah
DIV AB ; a=a/10d
INC R0 ; (r0) = (r0) + 1
MOV @R0,A ; Store decimal digit in ten pos.in 42h
INC R0 ; (R0) = (R0) +1
MOV A,0F0h ;Remainder of second division is
;moved to acc.
MOV @R0,A ;store digit in units position in 42h
here: SJMP here
END
EX:
Input Output
(40) = FF (41) = 02
(42) = 05
(43) = 05
7)WAP to convert given 2 digit BCD no. to equivalent hexadecimal no.
Logic: First the given BCD number is unpacked. The upper digit is
multiplied by 10d. and then the lower digit is added to get the binary
equivalent.
ORG 00h
MOV R0,#30h ; Memory pointer to decimal data
MOV A,@R0 ; Move data to accumulator
Logic: The given two digits are separated by ANDing the no. with 0F0h to
get the lower digit the no. is SWAPed and ANDed with 0F0h to get the
higher digit .Each no. is added with 30h to convert in to ASCII no.s
ORG 00h
start: MOV R0,#30h ;R0 pointing to source location
MOV A,@R0 ;Acc. loaded with decimal no.
ANL A,#0Fh ;obtain lower nibble
MOV R1,A ;Lower nibble stored in r1
MOV A,@R0
SWAP A ;Exchange lower and higher nibble
ANL A,#0Fh ;obtain higher nibble in lower position
ADD A,#30h
MOV 40h,A ;40h has ASCII value of higher nibble of
no.
MOV A,R1
ADD A,#30h
MOV 41h,A ;41h has ASCII value of lower nibble of no.
Stsrt: SJMP start
END
EX:
Input: (30) = 48
Output: (40) = 34 (41) = 38
9) WAP to find largest element in an array of N bytes stored in internal
RAM.
ORG 00h
LAR EQU 040h ;LAR stores largest element in the array
MOV R0,#50h ; r0 pointing to array location
MOV R1,#05h ; counter for array elements
loop: MOV A,@r0 ; Move array element to accumulator
CJNE A,LAR,neq ;Compare two elements if notequaljump
AJMP skip ; Take short jump
neq: JC skip ; Jump if (a)<lar
MOV LAR,A ; Move large no. to lar
skip: INC R0 ; r0=r0+1
DJNZ R1,loop ; Decrement cuonter if not zero repeat
here: SJMP here
END
Input: (50) = 05
(51) = 0A
(52) = F1
(53) = 12
(54) = 0C
Output: (40) = F1
10) Write an assembly language program to sort an array of n= 6 bytes
of data in ascending order stored from location 50h.
Logic: In Bubble sort there are (n-1) number of passes and in the first pass
there (n-1) comparisons, in the second pass there will be (n-2) comparisons
and so on. The last pass will have 1 comparison. The first no. is compared
with second if greater then the two no.s are exchanged otherwise they are
retained. The above process is repeated until all comparisons and passes are
completed.
ORG 00h
NUM NUM equ 040h
MOV R3,#04h ;r3 Counter for no. of passes
back1: MOV R0,#50h ;Store N=10 elements from 50h
MOV R1,#50h ;r0 and r1 are used as memory pointers
MOV R2,#04h ;r2 Counter for no.of comparisons
back: MOV A,@R0 ;Compare ((r0)) with ((r1))
INC R1
MOV NUM,@R1
CJNE A,NUM,loop
SJMP next
loop: JC next ;if ((r0)) < ((r1)) continue with comparison
;Exchange of two no.s
XCH A,@R1 ;Else exchange the two no.
XCH A,@R0
next: INC R0
DJNZ R2,back ;Decrement no. of comparisons
DJNZ R3,back1 ;Decrement no. of passes
here: SJMP here
END
Logic: Initialize accumulator to 00h, display the number and after a delay
increment accumulator by 01 and repeat the loop.
ORG 00h
MOV A,#00h ; Move initial count in to acc.
BACK: ACALL DELAY ; Call delay routine
MOV 40h,A
INC A ; increment acc.
JNC BACK ; jump if acc. Is not zero
start: SJMP start
Result: To run this program, after selecting DEBUG session in the main menu use
View-> Watch& call Stack window, in the Watches select watch 1(or 2) and
press F2 and enter a (for accumulator A)
12) WAP to illustrate hex. Down counter
ORG 00h
MOV A,#0ffh ; Move initial count in to acc.
BACK: ACALL DELAY ; Call delay routine
DEC A ; decrement acc.
JNZ BACK ; jump if acc. Is not zero
start: SJMP start
ORG 00h
MOV A,#00h ; Move initial count in to acc.
BACK: ACALL DELAY ; Call delay routine
ADD A,#01h ; decrement acc.
DAA
JNZ BACK ; jump if acc. Is not zero
start: SJMP start
ORG 00h
MOV A,#99h ; Move initial count in to acc.
BACK: ACALL DELAY ; Call delay routine
ADD A,#99h ; decrement acc.
DAA
JNZ BACK ; jump if acc. Is not zero
start: SJMP start
ORG 00h
MOV TMOD,#20h;timer 1 mode 2
MOV TH1,#0FD ;FD loaded into TH1 for 9600 baud
MOV SCON,#50h ;8-bit, 1 stop bit, REN enabled
SETB TR1 ;Start timer 1
MOV A,#’B’ ;transfer "B"
ACALL TRANS
MOV A,#’M’ ;transfer "M"
ACALL TRANS
MOV A,#’S’ ;transfer "S"
ACALL TRANS
AGAIN: SJMP AGAIN
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;Wait for last bit to transfer
CLR TI ;get ready for next byte
RET
END
Note-to use result of this program, after selecting DEBUG session in the
main menu use
View-> serial window #1. On running & halting the program, the data is
seen in the serial window.
OR
Logic: Same as above, here code memory is used to save the character string
to be sent through serial port. DPTR is used to access code memory
ORG 00H
MOV TMOD,#20H
MOV TH1,#0FDH
MOV SCON,#50H
SETB TR1
MOV DPTR,#MES1
NEXT: CLR A
MOVC A,@A+DPTR
JZ S1
ACALL SENDCHAR
INC DPTR
SJMP NEXT
S1: SJMP S1
SENDCHAR: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
MES1: DB"BMS COLLEGE OF ENGG."
END
14) Write a program to perform the following operations on 3bytes of data
stored from 40h.
a. OR the lower nibble of 40h with upper nibble of 41h.
b. Result of (a) is EX-ORed with 42h
Store the result in 43h.
Logic: The upper nibble of 40h is masked to retain the lower nibble. Then
the lower nibble of 41h is masked and swaped to bring upper nibble to lower
nibble. OR the two values. Then EX-OR the result with the content of 42h.
ORG 00H
MOV A,40H ;(A) = (40H)
ANL A,#0Fh
MOV R3,A
MOV A,41H
ANL A,#0F0h
SWAP A ; Upper nibble is masked
ORL A,R3
XRL A,42h
HERE: SJMP HERE
END