You are on page 1of 12

MICROCONTROLLER 8051

LABMANUAL
Contents

I. PROGRAMMING

1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.

2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube


– (16 bits Arithmetic operations – bit addressable).

3. Counters.

4. Boolean & Logical Instructions (Bit manipulations).

5. Conditional CALL & RETURN.


.

6. Programs to generate delay, Programs using serial port and on- Chip timer/ counter.
1. Write an ALP to move block of data bytes present in internal memory with starting address
10h and ending address 20h to the destination memory with starting address 30h.
(Without overlap).

Address Label Mnemonic Comment


9000 MOV R1,#10H Starting addr of src
MOV R2,#20H Ending addr of src
MOV R0,#30H Starting addr of desti
CLR C
MOV A,R2 Determination of size And
SUBB A,R1 stored in R2
MOV R2,A
LOOP MOV A,@R1
Copy data byte
MOV @R0,A
INC R1
INC R0
DJNZ R2,LOOP
LCALL 0003

2. Write an ALP to move block of data bytes present in internal memory with starting address
10h and ending address 20h to the destination memory with starting address 15h.
(With overlap).
Address Label Mnemonic Comment
9000 MOV R1,#10H Starting addr of src
MOV R2,#20H Ending addr of src
MOV R0,#15H Starting addr of desti
CLR C
MOV A,R2 Determination of size And
SUBB A,R1 stored in R2
MOV R2,A
MOV A,R1
ADD A,R2 End addr of src
MOV R1,A
MOV A,R0
ADD A,R2 End addr of desti
MOV R0,A
INC R2
LOOP MOV A,@R1
Copy data byte
MOV @R0,A
DEC R1
DEC R0
DJNZ R2,LOOP
LCALL 0003
3. Write an ALP to move block of data bytes present in external memory with starting address
8000h to the destination memory with starting address 9000h and size of array is 10h.

Address Label Mnemonic Comment


8500 MOV R0,#10H Size of an array
MOV 82H,#00H DPL=00
LOOP MOV 83H,#80H DPH=80
MOVX A,@DPTR Src data to acc
MOV 83H,#90H DPH=90
MOV @DPTR,A Acc to desti
INC DPTR
DJNZ R0,LOOP
LCALL 0003H

4. Write an ALP to exchange block of data bytes present in external memory. Starting address
of first is 8000h and starting address of other block 9000h and size of array is 10h.

Address Label Mnemonic Comment


8500 MOV R0,#10H Size of an array
MOV 82H,#00H DPL=00h
LOOP MOV 83H,#80H DPH=80h
MOVX A,@DPTR Src data to acc
MOV R1,A
MOV 83H,#90H DPH=90h
MOV A,@DPTR
XCH A,R1
MOVX @DPTR,A
MOV 83H,#80H DPH=80h
MOV A,R1
MOVX @DPTR,A
INC DPTR
DJNZ R0,LOOP
LCALL 0003H
5. Write an ALP to sort a given array present in external memory with a starting address 9000h
and size of an array is 10h using bubble sort technique.

Address Label Mnemonic Comment


8000 MOV R1,#10H Outer loop count
OUTLOOP MOV R0,#10H Inner loop count
MOV DPTR,#9000H
INLOOP CLR C Carry=0
MOVX A,@DPTR
MOV R2,A R2=first no
INC DPTR
MOVX A,@DPTR Acc=second no
MOV R3,A
SUBB A,R3 Compare
JNC SKIP
XCH A,R2 Exchange
SKIP MOVX @DPTR,A Big no€mem
DEC 82H DPL=DPL-1
MOV A,R2
MOVX @DPTR,A Small no€mem
INC DPTR
DEC R0
CJNE R0,#01H,INLOOP
DEC R1
CJNE R1,#01H,OUTLOOP
LCALL 0003H

6. Write an ALP to add ‘n’ bytes stored in external RAM (Starting address 9000 and no of bytes
is 10 or 0Ah)

Address Label Mnemonic Comment


8000 MOV R0,#0A No of bytes
MOV R1,#00 R1=SUM=0
MOV DPTR,#9000 DPTR=9000
LOOP MOVX A,@DPTR
ADD A,R1 Sum=sum+n[i]
MOV R1,A
INC DPTR
DJNZ R0,LOOP
LCALL 0003
7. Write an ALP to find largest element in a given array present in external memory with a
starting address 9000h and size of an array is 10h.

Address Label Mnemonic Comment


8000 MOV R0,#10H count
MOV DPTR,#9000H
LOOP CLR C Carry=0
MOVX A,@DPTR
MOV R2,A R2=first no
INC DPTR
MOVX A,@DPTR Acc=second no
MOV R3,A
SUBB A,R3 Compare
JNC SKIP
XCH A,R2 Exchange
SKIP MOVX @DPTR,A Big no€mem
DEC 82H DPL=DPL-1
MOV A,R2
MOVX @DPTR,A Small no€mem
INC DPTR
DEC R0
CJNE R0,#01H, LOOP
INC DPTR
MOV A,@DPTR A=largest no
LCALL 0003H

8. Write an ALP to search a byte in an array of bytes stored in external RAM.

Address Label Mnemonic Comment


8000 MOV R0,#0A Array size
MOV R1,#10 Search value
MOV R2,#00 Count
MOV DPTR,#9000
MOVX A,@DPTR
CLR C
SUBB A,R1 compare
INC DPTR
JNZ SKIP
INC R2
DJNZ R0,LOOP
LCALL 0003
9. Write an ALP to illustrate addition, subtraction, multiplication and division of two 8 bit
numbers.

Address Label Mnemonic Comment


8000 MOV R1,#20H First no
MOV R2,#10H Second no
MOV A,R1 ADDITION
ADD A,R2 R0=R1+R2
MOV R0,A
CLR C SUBTRACTION
MOV A,R1 R3=R1-R2
SUBB A,R2
MOV R3,A
MOV A,R1 MULTIPLICATION
MOV F0,R2 R4=R1xR2
MUL AB
MOV R4,A
MOV A,R1 Division
MOV B,R2 R5=R1/R2
DIV AB
MOV R5,A
LCALL 0003H

10. Write an ALP to illustrate logical operations like AND, OR, NOT and XOR

Address Label Mnemonic Comment


8000 MOV R1,#20H First BYTE
MOV R2,#10H Second BYTE
MOV A,R1 ANDING R0=R1
ANL A,R2 AND R2
MOV R0,A
MOV A,R1 ORING R3=R1
ORL A,R2 OR R2
MOV R3,A
MOV A,R1 NEGATION
CPL A R4=~R1
MOV R4,A
MOV A,R1 XORING R5=R1
XRL A,R2 XOR R2
MOV R5,A
LCALL 0003H
11. Write an ALP to add two 2 byte numbers.

Address Label Mnemonic Comment


8000 MOV R1,#12 3412
MOV R2,#34 7856
-------
MOV R3,#56
0AC68
MOV R4,#78
MOV R7,#00 3rd byte=0
CLR C
MOV A,R1
ADD A,R3
MOV R5,A
MOV A,R2
ADDC A,R4
MOV R6,A
JNC SKIP
MOV R7,#01 3rd byte=1
SKIP LCALL 0003

12. Write an ALP to subtract 2 byte number from another 2 byte number.

Address Label Mnemonic Comment


8000 MOV R1,#56 7856
MOV R2,#78 3412
-------
MOV R3,#12
4444
MOV R4,#34
CLR C
MOV A,R1
SUBB A,R3
MOV R5,A
MOV A,R2
SUBB A,R4
MOV R6,A
LCALL 0003
13. Write an ALP to illustrate hexadecimal up counter with a given starting and ending value.

Address Label Mnemonic Comment


8000 MAIN MOV A,#00 Starting value
MOV F0,#FF Ending value
LOOP MOV R6,A
MOV R3,A
LCALL 677D Display R6 data
MOV R0,#FF
MOV R1,#FF
LCALL 6850 Delay
MOV R0,#FF
MOV R1,#FF
MOV A,R3
INC A Next value
CJNE A,F0,LOOP
LJMP MAIN

14. Write an ALP to illustrate hexadecimal down counter with a given staring and ending value.

Address Label Mnemonic Comment


8000 MAIN MOV A,#FF Starting value
MOV F0,#00 Ending value
LOOP MOV R6,A
MOV R3,A
LCALL 677D Display R6 data
MOV R0,#FF
MOV R1,#FF
LCALL 6850 Delay
MOV R0,#FF
MOV R1,#FF
MOV A,R3
DEC A Next value
CJNE A,F0,LOOP
LJMP MAIN
15. Write an ALP to illustrate decimal up counter with a given staring and ending value.

Address Label Mnemonic Comment


8000 MAIN MOV A,#00 Starting value
MOV F0,#99 Ending value
LOOP ADD A,#00
DA A
MOV R6,A
MOV R3,A
LCALL 677D Display R6 data
MOV R0,#FF
MOV R1,#FF
LCALL 6850 Delay
MOV R0,#FF
MOV R1,#FF
MOV A,R3
INC A Next value
CJNE A,F0,LOOP
LJMP MAIN

16. Write an ALP to illustrate decimal down counter with a given staring and ending value.

Address Label Mnemonic Comment


8000 MAIN MOV A,#99 Starting value
MOV F0,#00 Ending value
LOOP ADD A,#00
DA A
MOV R6,A
MOV R3,A
LCALL 677D Display R6 data
MOV R0,#FF
MOV R1,#FF
LCALL 6850 Delay
MOV R0,#FF
MOV R1,#FF
MOV A,R3
DEC A Next value
CJNE A,F0,LOOP
LJMP MAIN
Unit 1: Data Transfer Operation

a. ALP : Data transfer using immediate addressing method

Address Label Mnemonic Comment


9000 MOV A,#10 A=10h
MOV F0,#20 F0=20h or B=20h
MOV R0,#FF R0=FF
MOV R1,#41 R1=41
LCALL 0003 Halt instruction

Before Execution After Execution


A=xx A=10
B=xx B=20
R0=xx R0=FF
R1=xx R1=41

b. Data transfer using direct addressing method

Address Label Mnemonic Comment


9000 MOV A,10 A=&10
MOV F0,20 F0=&20 or B=&20
MOV R0,FF R0=&FF
MOV R1,41 R1=&41
LCALL 0003 Halt instruction

Before Execution After Execution


A=xx A=10
B=xx B=20
R0=xx R0=FF
R1=xx R1=41

b. Data transfer using indirect addressing method

Address Label Mnemonic Comment


9000 MOV R0,#10 R0=10
MOV A,@R0 A=&R0(=&10)
MOV R0,#20
MOV F0,@R0 F0=&R0(=&20)
LCALL 0003 Halt instruction

Before Execution After Execution


A=xx A=20
B=xx B=40
R0=xx R0=20
&10=20
&20=40

You might also like