0% found this document useful (0 votes)
32 views92 pages

Manual - Ec-408 - Microcontroller Programming Lab Practice

Uploaded by

Shânnû X Sâí
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views92 pages

Manual - Ec-408 - Microcontroller Programming Lab Practice

Uploaded by

Shânnû X Sâí
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Microcontroller Lab Department of ECE

Dr.BRAGMRPW (SC), Karimnagar 1


Microcontroller Lab Department of ECE

MICROCONTROLLER
PROGRAMMING LAB
PRACTICE MANUAL
C-21: IV-SEM: EC-408
Prepared by

Mr. N.SRINIVASA RAO M.Tech

Head of ECE Section

Diploma in Electronics & Communication Engineering Section


Dr.B.R.Ambedkar GMR Polytechnic for Women (S.C),
KARIMNAGAR
(Affiliated to SBTET, Hyderabad, TS & Approved by AICTE, New Delhi)

Dr.BRAGMRPW (SC), Karimnagar 2


Microcontroller Lab Department of ECE

Index Page

S.No Name of the Content Page No.


1 Course Details 5
2 Syllabus 6
3 Introduction to 8051 7
4 Procedure for execution of programs using Keil μVision-3/4/5 9
I. Programs related to data transfer instructions.
5 Program to move the data from one Register to another Register 12
6 Program to move the data from I- RAM one location to another location 13
7 Program to move the data from E- RAM one location to another location 14
8 Program to move the data from E- RAM to I- RAM and vice versa 15
9 Program to Exchange the data between two Registers. 17
10 Program to Exchange the data between two I- RAM locations. 18
II. Programs on arithmetic instructions.
11 Program to add two 8-bit numbers present in I-RAM locations 20
12 Program to add two 8-bit numbers present in (OFF- chip) E-RAM locations 22
13 Program to add two 16-bit numbers present in I-RAM locations 24
14 Program to add two 16-bit numbers present in (OFF- chip) E-RAM locations 27
15 Program to subtract two 8-bit numbers present in registers and store the difference 30
in I-RAM location.
16 Program to subtract two 8-bit numbers present in I-RAM locations and E-RAM 31
locations.
17 Program to Multiply two 8-bit numbers present in I-RAM locations. 33
18 Program to Multiply two 8-bit numbers present in (OFF- chip) E-RAM locations. 35
19 Program to perform division between two 8-bit numbers present in I-RAM locations. 37
20 Program to perform division between two 8-bit numbers present in (OFF- chip) E- 39
RAM locations.
III. Programs on logical instructions.
21 Program to mask the higher order nibble of the given 8-bit number. 42
22 Program to mask the lower order nibble of the given 8-bit number. 43
23 Program to mask any one bit only of the given 8-bitnumber. 44
24 Program to rotate the given 8-bit number left twice with carry set. 45
25 Program to rotate the given 8-bit number rotate right twice with carry reset. 46
26 Program to find the a) 1’s complement b) 2’s complement of a given number. 47
IV. Programs to perform Data Transfer
Dr.BRAGMRPW (SC), Karimnagar 3
Microcontroller Lab Department of ECE

27 Program to transfer a block of data from I - RAM to E- RAM. 49


28 Program to transfer a block of data from E- RAM to I- RAM. 51
29 Program to add a block of data present in I- RAM, Store the Sum and Carry in 53
registers.
30 Program to test if a given number is present in the block of data. 55
31 Program to arrange a block of data in ascending order. 57
32 Program to arrange a block of data in descending order. 59
V. Counters &Timers
33 Program to Implement a HEX up/down counter 62
34 Program to produce required time delay using instructions. 63
35 Program to produce required time delay using Timers 65
VI. ADDITIONAL PROGRAMS
36 Program to transfer block of data from One location area to another location area 69
in E-RAM
37 Program to Exchange a Block of Data in Memory 72
38 Program to count no. of 1’s and 0’s in the given number 74
39 Program to find the Biggest number in the Block of data 76
40 Program to find the Smallest number in the Block of data 78
41 Program to Find the sum of First N-Natural Numbers 80
42 Program to perform (32-bit) Multi-byte Addition 82
43 Viva Questions 85
44 Instruction set hexcodes 89

1. Course Details
Dr.BRAGMRPW (SC), Karimnagar 4
Microcontroller Lab Department of ECE

Microcontroller Course
Course Title : EC-408
Programming lab Practice Code
Semester IV Course Group :Core

Teaching
Scheme in 1:0:2 Credits : 1.5
Periods(L:T:P)
Total Contact
Methodology Lecture + Practical :45Pds
Hours :

CIE 60 Marks SEE 40 Marks

Pre requisites
This course requires the basic skills of Handling digital circuits and computer.

Course outcomes:
On successful completion of the course, the students will be able to attain below Course Outcome
Linked Teaching
Course Outcome CL
PO Hours

Execute programs for data


CO1 R/U/A 1,2,3,4,10 9
manipulation.

Execute programs to perform and


CO2 R/U/A 1,2,3,4,10 15
arithmetic and logical functions.

Write and execute programs to meet a


CO3 R/U/A 1,2,3,4,10 9
requirement..

Implement time delays by writing 12


CO4 R/U/A 1,2,3,4,10
programs.

45

2. SYLLABUS
Dr.BRAGMRPW (SC), Karimnagar 5
Microcontroller Lab Department of ECE

I. Programs related to data transfer instructions.

1. a) Write an assembly language program (ALP) to move the data from one register to another.
b) Write an ALP to move the data from internal RAM one location to another.
2. a) Write an ALP to move the data from external RAM one location to another.
b) Write an ALP to move the data from external RAM internal RAM and vice versa.
3. a) Write an ALP to exchange the data between two registers.
b) Write an ALP to exchange the data between two internal RAM locations.

II. Programs on arithmetic instructions.

4. a) Write an ALP to add two binary numbers in i-RAM location.


b) Write an ALP to add two binary numbers in OFF chip-RAM location.
5. a) Write an ALP to add two decimal numbers in OFF chip-RAM location.
b) Write an ALP to add two decimal numbers in OFF chip-RAM location and store the carry in any
register.
6. a) Write an ALP to add two 16-bit binary numbers in i-RAM location.
b) Write an ALP to subtract two binary numbers in registers and store the diff. In i-RAM.
7. Write an ALP to multiply two binary numbers.

III. Programs on logical instructions.

8. a) Write an ALP to mask the higher order nibble.


b) Write an ALP to mask the lower order nibble.
9. a) Write an ALP to mask any one bit only of the binary number.
b) Write an ALP to rotate a number left twice with carry set.
10. a) Write an ALP to rotate a number right twice with carry reset.
b) Write an ALP to find the a) 1’s complement b) 2’s complement of a given number.

IV. Programs to perform Data Transfer

11. a) Write an ALP to transfer a block of data from internal RAM over l ap. to external RAM with
b) Write an ALP to transfer a block of data from external RAM to internal RAM without overlap.
12. Write an ALP to add block of decimal numbers present in internal RAM. Store the sum and carry in
registers.
13. Write an ALP to test if a given number is present in the block of data.
14. a) Write an ALP to arrange a block of data in ascending order.
b) Write an ALP to arrange a block of data in descending order.

V. Counters/ &Timers

15. a) Implement a HEX up/down counter - (Program should check value @R0=0X30, if 0X30=0 then
up counter else down counter)
b) Write a program in assembly language to produce required time delay a) by using instructions
only b) by using Timers

Dr.BRAGMRPW (SC), Karimnagar 6


Microcontroller Lab Department of ECE

3. Introduction
Earlier to Microcontrollers, Microprocessors were greatly used for each and every
purpose. Microprocessors were containing ALU, general purpose register, stack pointer,
program counter, clock counter and so many other features which the today’s
Microcontroller also possesses. But the difference between them exists with respect to
the number of instructions, access times, size, reliability, PCB size and so on.

Microprocessor contains large instruction set called as CISC processor where as


Microcontroller contains less number of instructions and is called as RISC processor.
The access time is less in case of microcontrollers compared to microprocessors and the
PCB size reduces in case of microcontrollers.

In General-purpose microprocessors must add RAM, ROM, I/O ports, and


Timers externally to make them functional and make the system bulkier and much more
Expensive. Have the advantage of versatility on the amount of RAM, ROM, and I/O
ports. But where as in Microcontroller the fixed amount of on-chip ROM, RAM, and
number of I/O ports makes them ideal for many applications in which cost and space are
critical In many applications, the space it takes, the power it consumes, and the price per
unit are much more critical considerations than the computing power

There are 8-bit, 16-bit and 32-bit microcontrollers made by various chip makers.
Like Motorola’s 6811,Intel’s 8051,Zilog’s Z8,Microchip’s PIC.Meeting the
computing needs of the task at hand efficiently and cost effectively i.e. is Speed,
Packaging, Power consumption ,The amount of RAM and ROM on chip, The number of
I/O pins and the timer on chip, How easy to upgrade to higher performance or lower
power-consumption
Versions Cost per unit.

There are many versions of microcontrollers 8051, 8052, 8751, AT8951 from
Atmel Corporation and many more. The 8051 became widely popular after allowing
other manufactures to make and market any flavor of the 8051, but remaining code-
compatible.

The 8051 family has the largest number of diversified (multiple source) suppliers
Intel (original), Atmel, Philips/ Signetics, AMD, Infineon (formerly Siemens),
Matra, Dallas Semiconductor/Maxim.

Applications

Home: Appliances, intercom, telephones, security systems, garage door openers,


answering machines, fax machines, home computers, TVs, cable TV tuner,VCR,
camcorder, remote controls, video games, cellular phones, musical instruments, sewing
Machines, lighting control, paging, camera, pinball machines, toys, exercise equipment

Office: Telephones, computers, security systems, fax machines, microwave, copier, laser
printer, colour printer, paging
Dr.BRAGMRPW (SC), Karimnagar 7
Microcontroller Lab Department of ECE

Auto: Trip computer, engine control, air bag, ABS, instrumentation, security system,
transmission control, entertainment, climate control, cellular phone, keyless entry

In this manual we will study about the 8051 architecture, its features, programming
MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and is the
core for all MCS-51 devices. Intel introduced 8051, referred as MCS-51, in 1981

The main features of the 8051 core are:


 Operates with single Power Supply +5V.
 It is a 40 pin IC chip.
 8-bit CPU can work on only 8-bits of data at a time
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
 128 bytes of On-Chip Data Memory (Internal RAM):
 Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
 64K Bytes of Off-Chip Program Memory and Data Memory
 (External ROM & Internal RAM)
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two 16-bit timers/counters (T0 and T1).
 ne Serial Port
 5 Interrupts (2 from external devices, 2 from timer/counters & 1 from Serial port )
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.

Dr.BRAGMRPW (SC), Karimnagar 8


Microcontroller Lab Department of ECE

4. Procedure for execution of programs using Keil μVision-3/4/5

1. Double Click on the µ - Vision 3/4/5 icon on the desktop.

2. Close any previous projects that were opened using – Project -> Close.

3. Start Project – New Project, and select the CPU from the device database
(Database-Atmel- AT89C51 or AT89C51RD2 as per the board).On
Clicking ‘OK’, the following option is displayed. Choose ‘No’.

4. Create a source file (using File->New), type in the assembly or C program and
Save this (filename.asm/filename.c) and add this source file to the project using
Either one of the following two methods.

(i) Project->Manage->Components, Environment Books->addfiles-> browse to


The required file -> OK
“OR”
ii) Right click on the Source Group in the Project Window and the Add Files to Group
Option.

5. Set the Target options using -> Project – Options for Target opens the
μ Options for Target – Target configuration dialog. Set the Xtal
(Crystal frequency) frequency as 11.0592 MHz, and also the Options for
Target – Debug – use either Simulator / Keil Monitor- 51 driver.

Dr.BRAGMRPW (SC), Karimnagar 9


Microcontroller Lab Department of ECE

6. If Keil monitor- 51 drivers is used click on Settings -> COM Port settings
Select the COM Port to which the board is connected and select the baud rate
As 19200 or 9600 (recommended). Enable Serial Interrupt option if the user
Application is not using on-chip UART, to stop program execution.

7. Build the project; using Project -> Build Project. μ - Vision translates all the
User application and links. Any errors in the code are indicated by – “Target
not created” in the Build window, along with the error line. Debug the errors.
After an error free, to build go to Debug mode.

8. Now user can enter into Debug mode with Debug- Start / Stop Debug
Session dialog. Or by clicking in the icon.

9. The program is run using the Debug-Run command & halted using Debug-
Stop Running. Also the (reset, run, halt) icons can be used.
Additional icons are (step, step over, and step into, run till
cursor).

10. If it is an interface program the outputs can be seen on the LCD, CRO,
Motor, led status, etc. If it is a part-A program, the appropriate memory
Window is opened using View -> memory window (for data RAM & XRAM
Locations), Watch window (for timer program), serial window, etc.

11. Note: To access data RAM area type address as D: 0020h.


Similarly to Access the DPTR region (XRAM-present on chip in
AT89C51ED2) say 9000h location type in X: 09000H.

Dr.BRAGMRPW (SC), Karimnagar 10


Microcontroller Lab Department of ECE

I. Programs related to
data transfer
instructions.

Dr.BRAGMRPW (SC), Karimnagar 11


Microcontroller Lab Department of ECE

5. Experiment No.1 (a) Date:


Aim: Write an assembly language program (ALP) to move the data from one
Register to another Register

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in Register R1=12h, It is moved in to another Register R2.

ORG 00H
MOV R1, #12H
MOV A, R1
MOV R2, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV R1,#12H Move 12H data in to A
8001 12
8002 E9 MOV A,R1 Data in R1 is copied in to A
8003 FA MOV R2,A Data in A is copied in to R2
8004 00 END End of the Program

Sample Result:
Before Execution: R1= 12H, R2= 00H

After Execution: R1= 12H, R2= 12H

Example: INPUT: R1= OUTPUT: R2=


Dr.BRAGMRPW (SC), Karimnagar 12
Microcontroller Lab Department of ECE

6. Experiment No.1 (b) Date:


Aim: Write an assembly language program (ALP) to move the data from
One location to another location in Internal RAM

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let Data is taken in Internal RAM location address 40h=13h (data) and it is moved in to
another Internal RAM Location address as 50h.

ORG 00H
MOV 40H, #13H ; source address
MOV A, 40H
MOV 50H, A ; Destination address
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 75
Move 13H data in to I-RAM
8001 40 MOV 40H,#13H
address 40h
8002 13
8003 E5 Data in I-RAM address 40h
MOV A,40H
8004 40 copied in to A
8005 F5 Data in A is copied in to I-
MOV 50H,A
8006 50 RAM address 50h
8007 00 END End of the Program

Sample Result:
Before Execution: 40h=13H, 50h=00H;

After Execution: 40h=13H, 50h=13H

Example:

INPUT: 40h = OUTPUT: 50h =

Dr.BRAGMRPW (SC), Karimnagar 13


Microcontroller Lab Department of ECE

7. Experiment No.2 (a) Date:


Aim: Write an assembly Language Program (ALP) to move the data from
One location to another location in External RAM

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let Data is taken in External RAM location address 6200h=25h (data) and it is
moved in another External RAM Location address at 6300h.

ORG 00H
MOV DPTR, #6200H ; Source Address
MOVX A,@DPTR
MOV DPTR, #6300H ; Destination Address
MOVX @DPTR, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initializing the source
8001 62 MOV DPTR, #6200H
pointer
8002 00
Data in E-RAM address
8003 E0 MOVX A,@DPTR
6200h copied in to A
8004 90
Initializing the
8005 63 MOV DPTR, #6300H
Destination pointer
8006 00
Data in A is copied in to
8007 F0 MOVX @DPTR, A
E-RAM address 6300h
8008 00 END End of the Program

Sample Result:
Before Execution: 6200h= 25H , 6300h= 00H

After Execution: 6200h= 25H , 6300h= 25H

Example:

INPUT: 6200h = OUTPUT: 6300H =

Dr.BRAGMRPW (SC), Karimnagar 14


Microcontroller Lab Department of ECE

8. Experiment No.2 (b) Date:


Aim: Write an assembly Language Program (ALP) to move the data from
External RAM one Location to Internal RAM one Location and vice versa.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let Data is taken in External RAM location address 6200h=66h (data) and it is
moved Internal RAM location address as 40h.

Let Data is taken in Internal RAM location address 50h=77h (data) and it is
moved External RAM location address as 6300h.

ORG 00H
MOV DPTR, #6200H ; Source Address (off-chip RAM)
MOV R0, #40H ; Destination Address (I-RAM)
MOVX A,@DPTR
MOV @R0, A
MOV R1, #50H ; Source Address (I- RAM)
MOV DPTR, #6300H ; Destination Address (off chip-RAM)
MOV A,@R1
MOVX @DPTR, A
END

Sample Result:
E-RAM Location to I-RAM Location: data =66H
I-RAM Location to E-RAM Location: data =77H

Dr.BRAGMRPW (SC), Karimnagar 15


Microcontroller Lab Department of ECE

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initializing the source pointer
8001 62 MOV DPTR, #6200H ie source address 6200h.
8002 00
8003 78 Initialise the R0 as internal
MOV R0, #40H memory pointer.ie destination
8004 40 address 40h
Data in E-RAM address
8005 E0 MOVX A,@DPTR 6200h copied in to A
Data in A is copied in to
8006 F7 MOV @R0, A I-RAM address 40h
8007 79 Initialise the R1 as internal
MOV R1, #50H memory pointer. ie (source
8008 50 address 50h)
8009 90 Initializing the Destination
800A 63 MOV DPTR, #6300H pointer ie destination address
800B 00 6300h.
MOV A,@R1 Data in I-RAM address 50h
800C E7 copied in to A
Data in A is copied in to
800D F0 MOVX @DPTR, A E-RAM address 6300h
800E 00 END End of the Program

Example:

E-RAM Location to I-RAM Location:

INPUT: 6200h= OUTPUT: 40H=

I-RAM Location to E-RAM Location:

INPUT: 50h= OUTPUT: 6300H=

Dr.BRAGMRPW (SC), Karimnagar 16


Microcontroller Lab Department of ECE

9. Experiment No.3 (a) Date:


Aim: Write an assembly Language Program (ALP) to Exchange the data
between two registers.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in Registers R0=44h, R1=55h and it is Exchanged between the
Registers R0 and R1.

ORG 00H
MOV R0, #44H
MOV R1, #55H
MOV A, R0
XCH A, R1
MOV R0, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 78
MOV R0,#44H Move the data 44H in to R0
8001 44
8002 79
MOV R1, #55H Move the data 55H in to R1
8003 55
8004 E8 MOV A,R1 Data in R1is copied in to A
Exchange the contents A
8005 C9 XCH A,R1
with R1
8006 F8 MOV R0,A Data in A is copied in to R0
8007 00 END End of the Program

Sample Result:
Before Execution: R0= 44H, R1= 55H

After Execution: R0= 55H, R1= 44H

Example:
INPUT: R0= OUTPUT: R0=
R1= R1=

Dr.BRAGMRPW (SC), Karimnagar 17


Microcontroller Lab Department of ECE

10. Experiment No.3 (b) Date:


Aim: Write an Assembly Language Program (ALP) to Exchange the data
between two Internal RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is Exchanged between Internal RAM Locations 40h and 50h.

ORG 00H
MOV 40h, #88H
MOV 50h, #99H
MOV A, 40h
XCH A, 50h
MOV 40h, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 75
Move the data 88H in to I-
8001 40 MOV 40h,#88H
RAM address 40h
8002 88
8003 75
Move the data 99H in to I-
8004 50 MOV 50h, #99H
RAM address 50h
8005 99
8006 E5
MOV A,40h Data in 40h is copied in to A
8007 40
8008 C5 Exchange the contents A
XCH A,50h
8009 50 with 50h
800A F5
MOV 40h,A Data in A is copied in to 40h
800B 40
800C 00 END End of the Program

Sample Result:
Before Execution: 40h= 88H, 50h= 99H
After Execution: 40h= 99H, 50h= 88H
Example:
INPUT: 40h= OUTPUT: 40h=
50h= 50h=

Dr.BRAGMRPW (SC), Karimnagar 18


Microcontroller Lab Department of ECE

II.Programs on
arithmetic
instructions.

Dr.BRAGMRPW (SC), Karimnagar 19


Microcontroller Lab Department of ECE

11. Experiment No.4 (a) Date:


Aim: Write an Assembly Language Program (ALP) to add two 8 bit
Numbers present in Internal RAM Locations

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is given in the I-RAM Locations 50h and 51h and store the Result
(sum and carry) in the 52h (lower 8-bit) and 53h (higher 8-bit)

ORG 00H
MOV R0, #50H ; I-RAM address initialization.
MOV A,@R0
INC R0
ADD A,@R0
INC R0
MOV @R0, A
INC R0
JC TOP1
MOV A, #00H
MOV @R0, A
SJMP TOP2
TOP1: MOV A, #01H
MOV @R0, A
TOP2: SJMP TOP2
END

Dr.BRAGMRPW (SC), Karimnagar 20


Microcontroller Lab Department of ECE

Memory Hex
Label Mnemonics Comments
Address Code
8101 78 Initialise the R0 as internal
MOV R0,#50H memory pointer.ie source address
8102 50 50h
Data in I-RAM address 50h
8103 E6 MOV A,@R0 copied in to A
8104 08 INC R0 Update the pointer R0
8105 26 ADD A,@R0 Add the data 1 with data 2
8106 08 INC R0 Update the pointer R0
Data in A is copied in to I-RAM
8107 F6 MOV@R0,A address 52h
8108 08 INC R0 Update the pointer R0
8109 40 If carry=1, go to TOP1, otherwise
JC TOP1 continue.
810A 05
810B 74
MOV A,#00H Move the data 00H in to A
810C 00
Data in A is copied in to I-RAM
810D F6 MOV @R0, A address 53h
810E 80
SJMP TOP2 Jump to TOP2
810F 03
8110 74
TOP1 MOV A,#01H Move the data 01H in to A
8111 01
Data in A is copied in to I-RAM
8112 F6 MOV @R0, A address 53h
8113 80
TOP2 SJMP TOP2 Stop execution
8114 FE
8115 00 END End of the program

Sample Result:

Before Execution: 50h= 24H, 51h= 12H;

After Execution: 52h = 36H (SUM)

Example: INPUT: 50h = OUTPUT: 52h =


51h = 53h =
Dr.BRAGMRPW (SC), Karimnagar 21
Microcontroller Lab Department of ECE

12. Experiment No.4 (b) Date:


Aim: Write an Assembly Language Program (ALP) to add two 8 bit
Numbers present in External RAM Locations

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in External RAM Locations 8300h, 8301h and Store the Result in
Locations 8302h (Lower 8 bits) and 8303h (higher 8 bits)

ORG 00H
MOV DPTR, #8300H
MOVX A,@DPTR
MOV R1, A
INC DPTR
MOVX A,@DPTR
ADD A, R1
INC DPTR
MOVX @DPTR, A
INC DPTR
JC TOP1
MOV A, #00H
MOVX @DPTR, A
SJMP TOP2
TOP1: MOV A, #01H
MOVX @DPTR, A
TOP2: SJMP TOP2
END

Sample Result:
Before Execution: 8300h=45H, 8301h =56H

After Execution: 8302h=9BH, 8303h=00H


Dr.BRAGMRPW (SC), Karimnagar 22
Microcontroller Lab Department of ECE

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initialise the data pointer.ie
8001 83 MOV DPTR,8300H source address 8300h
8002 00
Data in E-RAM address 8300h
8004 E0 MOVX A,@DPTR copied in to A
8005 F9 MOV R1,A Data in A is copied in to R1
8006 A3 INC DPTR Update the pointer
Data in E-RAM address 8301h
8007 E0 MOVX A,@DPTR is copied into A
8008 29 ADD A,R1 Add the data 1 with data 2
8009 A3 INC DPTR Update the pointer
Data in A is copied in to
800A F0 MOVX @DPTR,A E-RAM address 8302h
800B A3 INC DPTR Update the pointer
800C 40 If carry=1, go to TOP1,
JC TOP1 otherwise continue.
800D 05
800E 74
MOV A,#00H Move the data 00H in to A
800F 00
Data in A is copied in to
8010 F0 MOVX @DPTR, A E-RAM address 8303h
8011 80
SJMP TOP2 Jump to TOP2
8012 03
8013 74
TOP1 MOV A,#01H Move the data 01H in to A
8014 01
Data in A is copied in to
8015 F0 MOVX @DPTR, A E-RAM address 8303h
8016 80
TOP2 SJMP TOP2 Stop execution
8017 FE
8018 00 END End of the program

Example:1 INPUT : 8300h = OUTPUT : 8302h =


8301h = 8303h =

Dr.BRAGMRPW (SC), Karimnagar 23


Microcontroller Lab Department of ECE

Example:2 INPUT : 8300h = OUTPUT : 8302h =


8301h = 8303h =

13. Experiment No.5 (a) Date:


Aim: Write an Assembly Language Program (ALP) to add two 16- bit
Numbers present in Internal RAM Locations

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let Data is taken in Internal RAM Locations 40h, 41h (first 16-bit Number )and
50H,51H(Second 16-bit Number) , Store the Result in the locations 60h(Lower 8bits) ,
61h(higher 8 bits)and 62h(if carry generated)

ORG 00H
MOV R0, #40H ; source of 1st 16-bit number
MOV R1, #50H ; source of 2nd 16-bit number
MOV A,@R0
ADD A,@R1
MOV 60H, A
INC R0
INC R1
MOV A,@R0
ADDC A,@R1
MOV 61H, A
JC TOP1
MOV A, #00H
MOV 62H, A
SJMP TOP2
TOP1: MOV A, #01H
MOV 62H, A

Dr.BRAGMRPW (SC), Karimnagar 24


Microcontroller Lab Department of ECE

TOP2: SJMP TOP2


END

Memory Hex
Label Mnemonics Comments
Address Code
8101 78 Initialise the R0 as internal memory
MOV R0,#40H pointer.ie source address 40h
8101 40
8102 79 Initialise the R1 as internal memory
MOV R1,#50H pointer.ie source address 50h
8103 50
Data in I-RAM address 40h copied
8104 E6 MOV A,@R0 in to A
8105 27 ADD A,@R1 Add the data in 40h with data in 50h
8106 F5 Data in A is copied in to I-RAM
MOV 60H,A address 60h
8107 60
8108 08 INC R0 Update the pointer R0
8109 09 INC R1 Update the pointer R1
Data in I-RAM address 41h copied
810A E6 MOV A,@R0 in to A
810B 37 ADDC A,@R1 Add the data in 41h with data in 51h
810C F5 Data in A is copied in to I-RAM
MOV 61H,A address 61h
810D 61
810E 40 If carry=1, go to TOP1, otherwise
JC TOP1 continue.
810F 06
8110 74
MOV A,#00H Move the data 00H in to A
8111 00
8112 F5 Data in A is copied in to I-RAM
MOV 62H, A address 62h
8113 62
8114 80
SJMP TOP2 Jump to TOP2
8115 04
8116 74
TOP1 MOV A,#01H Move the data 01H in to A
8117 01
8118 F5 Data in A is copied in to I-RAM
MOV 62H, A address 62h
8119 62
811A 80
TOP2 SJMP TOP2 Stop execution
811B FE
811C 00 END End of the program

Dr.BRAGMRPW (SC), Karimnagar 25


Microcontroller Lab Department of ECE

Sample Result:

Memory address 40h 41h 50h 51h 60h 61h 62h


Before Execution 12 34 56 78 00 00 00
(Data)
After Execution
12 34 56 78 68 AC 00
(Data)

1st 16 –bit Number: 3412H

2nd 16-bit Number: 7856H

Sum: AC68H

Example:

Memory address 40h 41h 50h 51h 60h 61h 62h


Before Execution
(Data)
After Execution
(Data)

1st 16 –bit Number:

2nd 16-bit Number:

Sum:

Dr.BRAGMRPW (SC), Karimnagar 26


Microcontroller Lab Department of ECE

14. Experiment No.5 (b) Date:


Aim: Write an Assembly Language Program (ALP) to add two 16- bit
Numbers present in External RAM Locations

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let Data is taken in External RAM Locations 8300h, 8301h (first 16-bit Number )and
8302H,8303H(Second 16-bit Number), Store the result in the locations 8304h(Lower
8bits) , 8305h(higher 8 bits)and 8306h(if carry generated)
ORG 00H
MOV DPTR, #8300H ; OFF-CHIP RAM INITIALIAZATION
MOV R0, #40H
MOV R1, #50H
MOV R2, #02H
TOP1: MOVX A,@DPTR
MOV @R0, A
INC DPTR
INC R0
DJNZ R2, TOP1
MOV R2, #02H
TOP2: MOVX A,@DPTR
MOV @R1, A
INC DPTR
INC R1
DJNZ R2, TOP2
MOV R0, #40H ; source of 1st 16-bit number
MOV R1, #50H ; source of 2nd 16-bit number
MOV A,@R0
ADD A,@R1
MOVX @DPTR, A
INC DPTR
INC R0
INC R1
MOV A,@R0
ADDC A,@R1
MOVX @DPTR, A
Dr.BRAGMRPW (SC), Karimnagar 27
Microcontroller Lab Department of ECE

INC DPTR
JC TOP3
MOV A, #00H
MOVX @DPTR, A
SJMP TOP4
TOP3: MOV A, #01H
MOVX @DPTR, A
TOP4: SJMP TOP4
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initialise the data pointer.ie source
8001 83 MOV DPTR,8300H address 8300h
8002 00
8003 78 Initialise the R0 as internal memory
MOV R0,#40H pointer.ie for 1st 16-bit number
8004 40
8005 79 Initialise the R1 as internal memory
MOV R1,#50H pointer.ie for 2nd 16-bit number
8006 50
8007 7A
MOV R2,#02H Counter initialization. as 02
8008 02
Data in E-RAM address 8300h
8009 E0 TOP1 MOVX A,@DPTR copied in to A
Data in A is copied in to I-RAM
800A F6 MOV @R0,A address 40h onwards.
800B A3 INC DPTR Update the data pointer
800C 08 INC R0 Update the pointer R0
800D DA Decrement count, if count=0,stop
DJNZ R2,TOP1 otherwise go to TOP1
800E FA
800F 7A
MOV R2,#02H Counter re- initialization.
8010 02
Data in E-RAM address 8302h
8011 E0 TOP2 MOVX A,@DPTR copied in to A
Data in A is copied in to I-RAM
8012 F7 MOV @R1,A address 50h onwards
8013 A3 INC DPTR Update the data pointer
8014 09 INC R1 Update the pointer R1
8015 DA Decrement count, if count=0,stop
DJNZ R2,TOP2 otherwise go to TOP2
8016 FA
8017 78 Re-initialise the R0 as internal
MOV R0,#40H memory pointer.ie for 1st 16-bit
8018 40 number
8019 79 Re- Initialise the R1 as internal
MOV R1,#50H memory pointer.ie for 2nd 16-bit
801A 50 number
Data in I-RAM address 40h is
801B E6 MOV A,@R0 copied in to A
Add the data in 40h with the data
801C 27 ADD A,@R1 in 50h
Dr.BRAGMRPW (SC), Karimnagar 28
Microcontroller Lab Department of ECE

Data in A is copied in to E-RAM


801D F0 MOVX @DPTR,A address 8304h
801E A3 INC DPTR Update the data pointer
801F 08 INC R0 Update the pointer R0
8020 09 INC R1 Update the pointer R1
Data in I-RAM address 41h copied
8021 E6 MOV A,@R0 in to A
Add the data in 41h with the data in
8022 37 ADDC A,@R1 51h along with carry
Data in A is copied in to E-RAM
8023 F0 MOVX @DPTR,A address 8305h
8024 A3 INC DPTR Update the data pointer
8025 40 If carry=1, go to TOP3, otherwise
JC TOP3 continue.
8026 05
8027 74
MOV A,#00H Move the data 00H in to A
8028 00
Data in A is copied in to E-RAM
8029 F0 MOVX @DPTR, A address 8306h
802A 80
SJMP TOP4 Jump to TOP2
802B 03
802C 74
TOP3 MOV A,#01H Move the data 01H in to A
802D 01
Data in A is copied in to E-RAM
802E F0 MOVX @DPTR, A address 8306h
802F 80
TOP4 SJMP TOP4 Stop execution
8030 FE
8031 00 END End of the program

Sample Result:
Memory address 8300h 8301h 8302h 8303h 8304h 8305h 8306h
Before Execution 34 12 56 78 00 00 00
(Data)
After Execution 34 12 56 78 8A 8A 00
(Data)

1st 16 –bit Number: 1234H


2nd 16-bit Number: 7856H
Sum: 8A8AH

Example:

Memory address 8300h 8301h 8302h 8303h 8304h 8305h 8306h


Before Execution
(Data)
After Execution
(Data)

Dr.BRAGMRPW (SC), Karimnagar 29


Microcontroller Lab Department of ECE

1st 16 –bit Number:


2nd 16-bit Number:
Sum:

15. Experiment No.6 (a) Date:


Aim: Write an Assembly Language Program (ALP) to Subtract two 8- bit
Numbers present in registers and store the difference Internal RAM Location.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the Registers R1, R2 and Store the Result (Difference) in
the I-RAM Location 40h

ORG 00H
MOV R1, #34H
MOV R2, #28H
MOV A, R1
SUBB A, R2
MOV 40h, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 79 Move the 1st Number 34H in the
MOV R1,#34H Register R1
8001 34
8002 7A Move the 2nd Number 28H in the
MOV R2, #28H Register R2
8003 28
8004 E9 MOV A,R1 Data in R1 is copied in to A
8006 9A SUBB A,R2 Subtract the data in R2 from A
8008 F5 Difference in A is copied in to
MOV 40h,A I-RAM address 40h
8009 40
800A 00 END End of the Program

Sample Result:
Dr.BRAGMRPW (SC), Karimnagar 30
Microcontroller Lab Department of ECE

Before Execution: R1=34H, R2=28H


After Execution: 42h=0CH (Difference) =12D

Example: INPUT: R1= OUTPUT: 40h=

R2=

16. Experiment No.6 (b) Date:


Aim: Write an Assembly Language Program (ALP) to Subtract two 8- bit
Numbers present in Internal RAM and External RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program1:

Let the Data is given in the I-RAM Locations 50h and 51h and store the Result
(Difference) in the 52h location.

ORG 00H
MOV R0, #50H ; I-RAM address initialization.
MOV A,@R0
INC R0
SUBB A,@R0
INC R0
MOV @R0, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8101 78 Initialise the R0 as internal
MOV R0,#50H memory pointer.ie source address
8102 50 50h
Data in I-RAM address 50h
8103 E6 MOV A,@R0 copied in to A
8104 08 INC R0 Update the pointer R0
8105 26 SUBB A,@R0 Add the data 1 with data 2
8106 08 INC R0 Update the pointer R0
Data in A is copied in to I-RAM
8107 F6 MOV@R0,A address 52h
8108 00 END End of the Program

Dr.BRAGMRPW (SC), Karimnagar 31


Microcontroller Lab Department of ECE

Sample Result:
Before Execution: 50h= 45H, 51h= 20H
After Execution: 52h = 25H (Difference) =32D

Example:
INPUT: 50h= OUTPUT: 52h=

51h=
Program 2:
Let the Data is taken in the External RAM Locations 8300h, 8301h and the
Store the Result in External RAM location 8302h.

ORG 00H
MOV DPTR, #8300H
MOVX A,@DPTR
MOV R1, A
INC DPTR
MOVX A,@DPTR
SUBB A, R1
INC DPTR
MOVX @DPTR, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initialise the data pointer.ie
8001 83 MOV DPTR,8300H source address 8300h
8002 00
Data in E-RAM address 8300h
8004 E0 MOVX A,@DPTR copied in to A
8005 F9 MOV R1,A Data in A is copied in to R1
8006 A3 INC DPTR Update the pointer
Data in E-RAM address 8301h
8007 E0 MOVX A,@DPTR is copied into A
8008 99 SUBB A,R1 Subtract the data 1 from data 2
8009 A3 INC DPTR Update the pointer
Data in A is copied in to
800A F0 MOVX @DPTR,A E-RAM address 8302h
800B 00 END End of the program

Dr.BRAGMRPW (SC), Karimnagar 32


Microcontroller Lab Department of ECE

Sample Result:
Before Execution: 8300h=45H, 8301h =56H
After Execution: 8302h=11H (Difference) =17D
Example: INPUT: 8300h = OUTPUT: 8302h =
8301h =

17. Experiment No.7 (a) Date:


Aim: Write an Assembly Language Program (ALP) to multiply two 8- bit
Numbers present in Internal RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is given in the I-RAM Locations 50h and 51h, Store the Result (i.e.
Product) in the locations 52h (lower 8-bit),53h (higher 8-bit)

ORG 00H
MOV R0, #50H
MOV A,@R0
MOV 0F0H, A
INC R0
MOV A,@R0
MUL AB
INC R0
MOV @R0, A
INC R0
MOV A, 0F0H
MOV @R0, A
END

Sample Result:
Before Execution: 50h= 45H, 51h= 20H
After Execution: 52h = 88h, 53h=02H (Result=0288H (648 in Decimal))

Dr.BRAGMRPW (SC), Karimnagar 33


Microcontroller Lab Department of ECE

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal
MOV R0,#50H memory pointer.ie source
8001 50 address 50h onwards.
Data-1 in I-RAM address 50h
8002 E6 MOV A,@R0 copied in to A
8003 F5 Data-1 in A is copied in to
MOV 0F0H,A Reg.B ie it’s I-RAM address as
F0 0F0h
8004 08 INC R0 Update the pointer R0=51h
Data-2 in I-RAM address 51h
8005 E6 MOV A,@R0 is copied into A
Multiplying the data-2 and
8006 A4 MUL AB data-1
8007 08 INC R0 Update the pointer R0=52h
Data in A ie Lower order byte
8008 F6 MOV @R0,A of the result is copied in to
I-RAM address 52h
8009 08 INC R0 Update the pointer R0=53h
800A E5 Data in Reg.B ie Higher order
MOV A,0F0H byte of the result is copied into
800B F0 A
Data in A ie Higher order byte
800C F6 MOV @R0,A of the result is copied in to
I-RAM address 53h
800D 00 END End of the program

Example:

INPUT: 50h= OUTPUT: 52h=


51h= 53h=

Dr.BRAGMRPW (SC), Karimnagar 34


Microcontroller Lab Department of ECE

18. Experiment No.7 (b) Date:


Aim: Write an Assembly Language Program (ALP) to multiply two 8- bit
Numbers present in External RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let the Data is taken in External- RAM Locations 8300h, 8301h and Store
the Result (i.e. Product) in locations 8302h (Lower 8 bit), 8303H (Higher 8 bit)

ORG 00H
MOV DPTR, #8300H
MOVX A,@DPTR
MOV 0F0H, A
INC DPTR
MOVX A,@DPTR
MUL AB
INC DPTR
MOVX @DPTR, A
INC DPTR
MOV A, 0F0H
MOVX @DPTR, A
END

Sample Result:
Before Execution: 8300h= 25H, 8301h= 13H

Dr.BRAGMRPW (SC), Karimnagar 35


Microcontroller Lab Department of ECE

After Execution: 8302h = BFH, 8303h=02H

(Result=02BFH (703 in Decimal))

Memory Hex
Label Mnemonics Comments
Address Code
8000 90 Initializing the data pointer.ie
8001 83 MOV DPTR,#8300H source address 83000h
00 onwards.
Data-1 in E-RAM address
8002 E0 MOVX A,@DPTR 8300h copied in to A
8003 F5 Data-1 in A is copied in to
MOV 0F0H,A Reg.B ie it’s I-RAM address as
F0 0F0h
8004 A3 INC DPTR Update the pointer to 8301h
Data-2 in E-RAM address
8005 E0 MOVX A,@DPTR 8301h is copied into A
8006 A4 MUL AB Multiply data-2 with data-1
8007 A3 INC DPTR Update the pointer to 8302h
Data in A ie Lower order byte
8008 F0 MOVX @DPTR,A of the result is copied in to
E-RAM address 8302h
8009 A3 INC DPTR Update the pointer to 8303h
800A E5 Data in Reg.B ie Higher
MOV A,0F0H order byte of the result is
800B F0 copied into A
Data in A ie Higher order byte
800C F0 MOVX @DPTR,A of the result is copied in to
E-RAM address 8303h
800D 00 END End of the program

Example:1

INPUT: 8300h = OUTPUT: 8302h =


8301h = 8303h =

Example:2
Dr.BRAGMRPW (SC), Karimnagar 36
Microcontroller Lab Department of ECE

INPUT: 8300h = OUTPUT: 8302h =


8301h = 8303h =

19. Experiment No.7 (c) Date:


Aim: Write an Assembly Language Program (ALP) to perform division operation
Between two 8- bit numbers present in Internal RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM Locations 50h (Divisor) and 51h (Dividend)
and Store the Result in the locations 52h (Quotient) and 53h (Remainder)

ORG 00H
MOV R0, #50H
MOV A,@R0
MOV 0F0H, A
INC R0
MOV A,@R0
DIV AB
INC R0
MOV @R0, A
INC R0
MOV A, 0F0H
MOV @R0, A
END

Sample Result:
Before Execution: 50h= 12H (Divisor),
51h= 25H (Dividend)

After Execution: 52h = 02H (Quotient),

Dr.BRAGMRPW (SC), Karimnagar 37


Microcontroller Lab Department of ECE

53h=01H (Remainder)

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal
MOV R0,#50H memory pointer.ie source
8001 50 address 50h onwards.
Data-1 in I-RAM address 50h
8002 E6 MOV A,@R0 copied in to A
8003 F5 Data-1 in A is copied in to
MOV 0F0H,A Reg.B i.e. it’s I-RAM address
F0 as 0F0h
8004 08 INC R0 Update the pointer R0=51h
Data-2 in I-RAM address 51h
8005 E6 MOV A,@R0 is copied into A
8006 84 DIV AB Divide the data-2 by data-1
8007 08 INC R0 Update the pointer R0=52h
Data in A ie Quotient is
8008 F6 MOV @R0,A Copied in to I-RAM address
52h
8009 08 INC R0 Update the pointer R0=53h
800A E5 Data in Reg.B i.e. Remainder
MOV A,0F0H is copied into A
800B F0
Data in A i.e. Remainder
800C F6 MOV @R0,A copied in to I-RAM address
53h
800D 00 END End of the program

Example:

INPUT: 50h (Divisor) = OUTPUT: 52h (Quotient) =


51h (Dividend) = 53h (Remainder) =

Dr.BRAGMRPW (SC), Karimnagar 38


Microcontroller Lab Department of ECE

20. Experiment No.7 (d) Date:


Aim: Write an Assembly Language Program (ALP) to perform division operation
Between two 8- bit numbers present in External RAM Locations.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let the Data is taken in External- RAM Locations 8300h, (Divisor) 8301h
(Dividend), Store the Result in locations 8302h (Quotient), 8303H (Remainder)

ORG 00H
MOV DPTR, #8300H
MOVX A,@DPTR
MOV 0F0H, A
INC DPTR
MOVX A,@DPTR
DIV AB
INC DPTR
MOVX @DPTR, A
INC DPTR
MOV A, 0F0H
MOVX @DPTR, A
END

Sample Result:

Dr.BRAGMRPW (SC), Karimnagar 39


Microcontroller Lab Department of ECE

Before Execution: 8300h= 15H (Divisor),


8301h= 45H (Dividend)

After Execution: 8302h = 03H (Quotient),

8303h=06H (Remainder)

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initializing the data pointer.ie
8001 83 MOV DPTR,#8300H source address 8300h onwards.
00
Data-1 in E-RAM address
8002 E0 MOVX A,@DPTR 8300h copied in to A
8003 F5 Data-1 in A is copied in to
MOV 0F0H,A Reg.B ie it’s I-RAM address as
F0 0F0h
8004 A3 INC DPTR Update the pointer to 8301h
Data-2 in E-RAM address
8005 E0 MOVX A,@DPTR 8301h is copied into A
8006 84 DIV AB Divide data-2 by data-1
8007 A3 INC DPTR Update the pointer to 8302h
Data in A ie Quotient is
8008 F0 MOVX @DPTR,A copied in to E-RAM address
8302h
8009 A3 INC DPTR Update the pointer to 8303h
800A E5 Data in Reg.B ie Remainder
MOV A,0F0H is copied into A
800B F0
Data in A ie Remainder is
800C F0 MOVX @DPTR,A copied in to E-RAM address
8303h
800D 00 END End of the program

Example:1

INPUT: 8300h (Divisor) = OUTPUT: 8302h (Quotient) =


8301h (Dividend) = 8303h (Remainder) =

Dr.BRAGMRPW (SC), Karimnagar 40


Microcontroller Lab Department of ECE

Example:2

INPUT: 8300h (Divisor) = OUTPUT: 8302h (Quotient) =


8301h (Dividend) = 8303h (Remainder) =

III.Programs on
Logical
Instructions.
Dr.BRAGMRPW (SC), Karimnagar 41
Microcontroller Lab Department of ECE

21. Experiment No.8 (a) Date:


Aim: Write an assembly language program (ALP) to mask the higher order nibble of
the given 8-bit number.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

To mask Nibble in the give 8-bit number use logical AND operation
Let the Data is taken in (Accumulator) A=54H, after masking the higher order
nibble in the given 8-bit number, Store the Result in the Register R4 (Masked data of
Higher order nibble)

ORG 00H
MOV A, #54H
ANL A, #0FH ; for masking Higher order nibble
MOV R4, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#54H Move the data 54H in to A
8001 54
8002 54 Perform Logical AND operation
with the contents of A with the
ANL A, #0FH data 0FH to mask higher order
8003 0F
nibble of the number.
Masked number of higher order
8004 FC MOV R4,A nibble is copied in to R4.
8005 00 END End of the Program
Dr.BRAGMRPW (SC), Karimnagar 42
Microcontroller Lab Department of ECE

Sample Result:
Before Execution: A=54H;
After Execution: R4 = 04H,
Example:
INPUT: A=
OUTPUT: R4=

22. Experiment No.8 (b) Date:


Aim: Write an assembly language program (ALP) to mask the Lower order nibble of
the given 8-bit number.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

To mask Nibble in the give 8-bit number use logical AND operation
Let the Data is taken in (Accumulator) A=54H, after masking the Lower order
nibble in the given 8-bit number, Store the Result in the Register R5 (Masked data of
Lower order nibble)

ORG 00H
MOV A, #54H
ANL A, #0F0H ; for masking Lower order nibble
MOV R5, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#54H Move the data 54H in to A
8001 54
8002 54 Perform Logical AND operation
with the contents of A with the
ANL A, #0F0H data F0H to mask lower order
8003 F0
nibble of the number.
Masked number of lower order
8004 FD MOV R5,A nibble is copied in to R5.
8005 00 END End of the Program

Dr.BRAGMRPW (SC), Karimnagar 43


Microcontroller Lab Department of ECE

Sample Result:
Before Execution: A=54H;
After Execution: R5 =50H
Example:

INPUT: A=

OUTPUT: R5=

23. Experiment No.9 (a) Date:


Aim: Write an assembly language program (ALP) to mask any one bit only of the given
8-bit number.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
To mask any one bit only in the give 8-bit number use logical AND operation with
the number
For example to mask 7th bit in the given 8-bit number to perform the logical AND
operation with the number consisting of 7th bit position as ZERO and remaining bit
positions as ONE,i.e.for the above example the number is=BF H

Let the given 8-bit number is taken in Accumulator (A) =C7H and the Result
i.e. masked number is stored in register R6.

ORG 00H
MOV A, #0C7H
ANL A, #0BFH
MOV R6, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#0C7H Move the data C7H in to A
8001 C7
8002 54 Perform Logical AND operation with
ANL A, #0BFH the contents of A with the data BFH to
8003 BF mask one bit only of the given number.
Result i.e. Masked number is copied in
8004 FE MOV R6,A to R6.

Dr.BRAGMRPW (SC), Karimnagar 44


Microcontroller Lab Department of ECE

8005 00 END End of the program

Sample Result:
Before Execution: A=C7H after Execution: R6=87H

Bit’s Position b8 b7 b6 b5 b4 b3 b2 b1
A 37h 1 1 0 0 0 1 1 1
th
Data to mask 7 bit is BFH BFh 1 0 1 1 1 1 1 1
Result:R6=87H
After Perform Logical AND operation 87h 1 0 0 0 0 1 1 1
between above two data’s
Example: INPUT= A= OUTPUT= R6=

24. Experiment No.9 (b) Date:


Aim: Write an assembly language program (ALP) to rotate the given 8-bit
number left twice with carry set.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let the data i.e. 8-bit number is taken in to accumulator (A) =54h, Store the
result in the register R5.
ORG 00H
MOV A, #54H
SETB C
RLC A
RLC A
MOV R5, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#54H Move the data 54H in to A
8001 54
8002 D3 SETB C Set the carry flag bit as 1
Rotate left the A contents through
8003 33 RLC carry by one time (LSB=Carry &
Carry=MSB)
Rotate left the A contents through
8004 33 RLC carry by one time (LSB=Carry &
Carry=MSB)
8005 FD MOV R5,A Result is copied in to R5.
8006 00 END End of the program
Dr.BRAGMRPW (SC), Karimnagar 45
Microcontroller Lab Department of ECE

Sample Result:
Before Execution: A=54H; After Execution: R5=52H

Bit’s Position Carry B7 B6 B5 B4 B3 B2 B1 B0


1
A=54H, Carry=1 0 1 0 1 0 1 0 0
After Execution of RLC 1st RLC 0
instruction every time the data 1 0 1 0 1 0 0 1
A=A9H
in A i.e. all bits move towards
left side by one time through 2nd RLC
1 0 1 0 1 0 0 1 0
carry A=52H
Result=A=R5=52h 0 1 0 1 0 0 1 0

Example: INPUT: A= OUTPUT: R5=


25. Experiment No.10 (a) Date:
Aim: Write an assembly language program (ALP) to rotate the given 8-bit
number rotate right twice with carry reset.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let the data i.e. 8-bit number is taken in to accumulator (A) =54h, Store the
result in the register R5.

ORG 00H
MOV A, #54H
CLR C
RRC A
RRC A
MOV R5, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#54H Move the data 54H in to A
8001 54
8002 C3 CLR C Set the carry flag bit as 0
Rotate right the A contents
8003 13 RRC A through carry by one time
(Carry=LSB & MSB=Carry)
Rotate right the A contents
8004 13 RRC A through carry by one time
(Carry=LSB & MSB=Carry)
8005 FD MOV R5,A Result is copied in to R5.
8006 00 END End of the program

Dr.BRAGMRPW (SC), Karimnagar 46


Microcontroller Lab Department of ECE

Sample Result:
Before Execution: A=54H; After Execution: R5=15H

Bit’s Position Carry B7 B6 B5 B4 B3 B2 B1 B0


A=54H, Carry=0 0 0 1 0 1 0 1 0 0
After Execution of RRC 1st RRC
instruction every time the data 0 0 0 1 0 1 0 1 0
A=2AH
in A i.e. all bits move towards
right side by one time through 2nd RRC
0 0 0 0 1 0 1 0 1
carry A=15H
Result=A=R5=15h 0 0 0 1 0 1 0 1

Example: INPUT: A= OUTPUT: R5=


26. Experiment No.10 (b) Date:
Aim: Write an assembly language program (ALP) to find the
a) 1’s complement b) 2’s complement of a given 8-bit number.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the data is taken in to accumulator (A) =64h and Store the result in the register
R5 (1’s complement) and R6 (2’s complement)

ORG 00H
MOV A, #64H
CPL A ; 1’s Complement
MOV R5, A
INC A ; 2’s Complement
MOV R6, A
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 74
MOV A,#64H Move the data 64H in to A
8001 64
8002 F4 CPL A Complement the A contents.
Result 1’s complement is copied
8003 FD MOV R5,A in to R5.
8004 04 INC A Increment the A contents by one
Result 2’s complement is copied
8005 FE MOV R6,A in to R6.

Dr.BRAGMRPW (SC), Karimnagar 47


Microcontroller Lab Department of ECE

8006 00 END End of the program

Sample Result:
Before Execution: A=64H;
After Execution: 1’s Complement: R5=9BH,
2’s Complement: R6=9CH

Example:
INPUT: A= OUTPUT: R5= R6=

IV.Programs to
perform
Dr.BRAGMRPW (SC), Karimnagar 48
Microcontroller Lab Department of ECE

Data Transfer

27. Experiment No.11 (a) Date:


Aim: Write an assembly language program (ALP) to transfer a block of data
from Internal RAM to External RAM.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM locations i.e. 40h onwards and this date is to be
transferred to E-RAM locations i.e. 8500h onwards, assume that no. of data’s are 10 i.e.
Counter is initialization as 0AH(10 data’s)

ORG 00H
MOV DPTR, #8500H
MOV R0, #40H
MOV R5, #0AH
TOP: MOV A,@R0
MOVX @DPTR, A
INC R0
INC DPTR
DJNZ R2, TOP
END

Memory Hex Label Mnemonics Comments


Dr.BRAGMRPW (SC), Karimnagar 49
Microcontroller Lab Department of ECE

Address Code
8000 90 Initializing the data pointer.ie
8001 85 MOV DPTR,#8500H Destination address 8500h
8002 00 onwards.
8003 78 Initializing R0 as internal
MOV R0,#40H memory pointer.ie source
8004 40 address 40h onwards.
8005 7D Initialise R5 has counter with
MOV R5,#0AH number of data as 10
8006 0A
Data in I-RAM address 40h
8007 E6 TOP MOV A,@R0 onwards are copied in to A
Data in A is copied in to
8008 F0 MOVX @DPTR,A E-RAM address 8500h onwards.
8009 08 INC R0 Update the pointer R0
800A A3 INC DPTR Update the pointer
800B DA Decrement R5, if R5=0,stop
DJNZ R5,TOP otherwise go to TOP
800C FA
800D 00 END End of the program

Sample Result:

Before &
Before After
After
I-RAM Address E- RAM Address Execution Execution
S.No Execution
locations locations
Data Data Data

1 40h 34 8500h 00 34
2 41h 25 8501h 00 25
3 42h 1F 8502h 00 1F
4 43h 3D 8503h 00 3D
5 44h 56 8504h 00 56
6 45h 75 8505h 00 75
7 46h 87 8506h 00 87
8 47h 95 8507h 00 95
9 48h 17 8508h 00 17
10 49h 83 8509h 00 83

Example:

INPUT OUTPUT
I-RAM Address E- RAM Address
Data Data
locations locations
40h 8500h
Dr.BRAGMRPW (SC), Karimnagar 50
Microcontroller Lab Department of ECE

41h 8501h
42h 8502h
43h 8503h
44h 8504h
45h 8505h
46h 8506h
47h 8507h
48h 8508h
49h 8509h

28. Experiment No.11 (b) Date:


Aim: Write an assembly language program (ALP) to transfer a block of data
from External RAM to Internal RAM
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the E-RAM locations i.e. 8500h onwards, and this date is to be
transferred to I-RAM locations i.e. 40h onwards, assume that the no. of data’s are 10 i.e.
Counter is initialization as 0AH(10 data’s)

ORG 00H
MOV DPTR, #8500H
MOV R0, #40H
MOV R5, #0AH
TOP: MOVX A,@DPTR
MOV @R0, A
INC R0
INC DPTR
DJNZ R5, TOP
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 90 MOV DPTR,#8500H Initializing the data pointer.ie
Dr.BRAGMRPW (SC), Karimnagar 51
Microcontroller Lab Department of ECE

8001 85
source address 8500h onwards.
8002 00
8003 78 Initializing R0 as internal
MOV R0,#40H memory pointer.ie destination
8004 40 address 40h onwards.
8005 7D Initialise R5 has counter with
MOV R5,#0AH number of data as 10
8006 0A
Data in E-RAM address 8500h
8007 E0 TOP MOVX A,@DPTR onwards are copied in to A
Data in A is copied in to
8008 F6 MOV @R0,A I-RAM address 40h onwards.
8009 08 INC R0 Update the pointer R0
800A A3 INC DPTR Update the pointer
800B DA Decrement R5, if R5=0,stop
DJNZ R5,TOP otherwise go to TOP
800C FA
800D 00 END End of the program

Sample Result:

Before &
Before After
After I- RAM
E-RAM Address Execution Execution
S.No Execution Address
locations
locations
Data Data Data

1 8500h 34 40h 00 34
2 8501h 25 41h 00 25
3 8502h 1F 42h 00 1F
4 8503h 3D 43h 00 3D
5 8504h 56 44h 00 56
6 8505h 75 45h 00 75
7 8506h 87 46h 00 87
8 8507h 95 47h 00 95
9 8508h 17 48h 00 17
10 8509h 83 49h 00 83

Example:

INPUT OUTPUT
E-RAM Address I- RAM Address
Data Data
locations locations
8500h 40h
8501h 41h
Dr.BRAGMRPW (SC), Karimnagar 52
Microcontroller Lab Department of ECE

8502h 42h
8503h 43h
8504h 44h
8505h 45h
8506h 46h
8507h 47h
8508h 48h
8509h 49h

29. Experiment No.12 Date:


Aim: Write an assembly language program (ALP) to add block of data
Present in Internal RAM , Store the Sum and Carry in registers.

Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM locations ie 50h onwards, assume that the no.
of data’s are 5 i.e. counter is initialization as 05H (5 data’s).

Store the Result in Registers R5 (SUM) and Register R6 (CARRY).

ORG 00H
MOV R0, #50H
MOV R4, #05H ; COUNTER
MOV R5, #00H
MOV R6, #00H
CLR C
TOP2: MOV A,@R0
ADD A, R5
MOV R5, A
JNC TOP1
Dr.BRAGMRPW (SC), Karimnagar 53
Microcontroller Lab Department of ECE

INC R6
TOP1: INC R0
DJNZ R4, TOP2
HERE: SJMP HERE
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#50H pointer.ie source address 50h
8001 50 onwards.
8002 7C Initialise R4 has counter with
MOV R4, #05H number of data as 05
8003 05
8004 7D
MOV R5,#00H Move the data 00H in R5
8005 00
8006 7E
MOV R6,#00H Move the data 00H in R6
8007 00
8008 C3 CLR C Set the carry flag bit position as ‘0’
Data in I-RAM address 50h
8009 E6 TOP2 MOV A,@R0 onwards are copied in to A
800A 2D ADD A,R5 Add the data in A with data in R5
800B 50 If carry=0, go to TOP1, otherwise
JNC TOP1 proceed.
01
800C 0E INC R6 Increment the contents of R6
800D 08 TOP1 INC R0 Update the pointer R0
800E DC Decrement R4,if R4=0 stops,
DJNZ R4,TOP2 otherwise go to TOP2
800F F7
8010 80
HERE SJMP HERE Stops execution
8011 FE
8012 00 END End of the Program

Sample Result:
I-RAM Locations 50h 51 52h 53h 54h
INPUT h
Data 23 45 36 64 58
Dr.BRAGMRPW (SC), Karimnagar 54
SUM: R5 5A
OUTPUT
CARRY: R6 01
Microcontroller Lab Department of ECE

Example:

I-RAM Locations 50h 51 52h 53h 54h


INPUT h
Data
SUM: R5
OUTPUT
CARRY: R6

30. Experiment No.13 Date:


Aim: Write an assembly language program (ALP) to test if a given number is Present in
the block of data.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM locations i.e. 50h onwards, assume that no. of
data’s are 8 i.e. counter is initialization as 08H (8 data’s).

One of the number must be present in the given block of data is taken in to
Accumulator (A) say 12H to test,

Register R5 is used to store the count number (i.e. the assumed number present in
the given block of data)

ORG 00H
MOV R0, #50H
MOV R7, #08H ; Counter
MOV R5, #00H
MOV A, #12H
MOV 0F0H, A
UP: MOV A,@R0

Dr.BRAGMRPW (SC), Karimnagar 55


Microcontroller Lab Department of ECE

CJNE A, B, L1
INC R5
L1: INC R0
DJNZ R7, UP
END

Memory Hex
Label Mnemonics Comments
Address Code
8001 78 Initializing R0 as internal memory
MOV R0,#50H pointer.ie source address 50h
8002 50 onwards.
8003 7F Initialise R7 has counter with
MOV R7, #08H number of data as 08
8004 08
8005 7D
MOV R5,#00H Move the data 00H in R5
8006 00
8007 74
MOV A,#12H Move the data 12H in A
8008 12
8009 F5 Data in A is copied in to Reg.B ie
MOV 0F0H,A it’s I-RAM address as 0F0h
800A F0
Data in I-RAM address 50h
800B E6 UP MOV A,@R0 onwards are copied in to A
800C B5 Compare the data in A with Data in
800D F0 CJNE A,B,L1 B, if they are not equal go to L1,
800E 01 otherwise proceed.
800F 0D INC R5 Increment the contents of R5
8010 08 L1 INC R0 Update the pointer R0
8011 DF Decrement R7,if R7=0 stops,
DJNZ R7,UP otherwise go to UP
8012 F8
8013 00 END End of the Program

Sample Result:
I-RAM Locations 50h 51h 52 53h 54h 55 56h 57h
INPUT h h
Data 12 25 12 45 53 35 12 85
Dr.BRAGMRPW (SC), Karimnagar 56
Microcontroller Lab Department of ECE

R5(Count Number)
Assumed Number
OUTPUT
appearing times in the 03
block of data

Example:
I-RAM Locations 50h 51h 52 53h 54h 55 56h 57h
INPUT h h
Data
R5(Count Number)
Assumed Number
OUTPUT
appearing times in the
block of data

31. Experiment No.14 (a) Date:


Aim: Write an assembly language program (ALP) to arrange a block of data
In ascending order.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM locations i.e. 41h onwards, assume that no. of data’s
are 8 i.e. counter is initialization as 08H (8 data’s).

store the result(i.e. data in ascending order) in the same I-RAM locations i.e. 41h
onwards.

ORG 00H
MOV R5, #08H ; COUNTER
L4: MOV R0, #41H ; I-RAM ADDRESS
MOV R7, #07H ; NO.OF COMPARISIONS
L3: MOV A,@R0
INC R0
MOV 0F0H,@R0
CJNE A, B, L1
SJMP L2
L1: JC L2
MOV @R0, A
DEC R0
MOV @R0, B
Dr.BRAGMRPW (SC), Karimnagar 57
Microcontroller Lab Department of ECE

INC R0
L2: DJNZ R7, L3
DJNZ R5, L4
HERE: SJMP HERE
END

Sample Result:
I-RAM Locations 41h 42h 43 44h 45h 46 47h 48h
INPUT h h
Data (Before Execution) 23 AF 45 12 87 94 65 39
Data in Ascending order
OUTPUT 12 23 39 45 65 87 94 AF
(After Execution)

Memory Hex
Label Mnemonics Comments
Address Code
8000 7D Initialise R5 has counter with
MOV R5,#08H number of data as 08
8001 08
8002 78 Initializing R0 as internal memory
L4 MOV R0, #41H pointer.ie source address 41h
8003 41 onwards.
8004 7F Move the data 07H in R7 ie it
MOV R7,#07H indicates number of comparisons
8005 07
Data in I-RAM address is copied in
8006 E6 L3 MOV A,@R0 to A
8007 08 INC R0 Update the pointer R0
8008 86 Data in I-RAM address is copied in
MOV 0F0H,@R0 to Reg.B
8009 F0
800A B5 Compare the data in A with Data in
800B F0 CJNE A,B,L1 B, if they are not equal go to L1,
800C 02 otherwise proceed.
800D 80
SJMP L2 Jump to L2
800E 07
800F 40 If carry=1(if A data < next data) go
L1 JC L2 to L2, otherwise proceed.
8010 05
Data in I-RAM address is copied in
8011 F6 MOV @R0,A to A(bring small data in to A)
Decrement the pointer R0
8012 18 DEC R0 (previous location)
8013 A6 Data in Reg.B is copied in to
MOV @R0,B I-RAM address (bring small data
8014 F0 in to memory location )
8015 08 INC R0 Update the pointer R0
8016 DF L2 DJNZ R7,L3 Decrement R7,if R7=0 stops,
Dr.BRAGMRPW (SC), Karimnagar 58
Microcontroller Lab Department of ECE

otherwise go to L3 (ie check


8017 FE weather all comparisons are over)
8018 DD Decrement R5,if R5=0 stops,
DJNZ R5,L4 otherwise go to L4 (ie check
8019 E8 weather all data are compared)
801A 80 HER
SJMP HERE Stops execution
801B FE E
801C 00 END End of the Program

Example:
I-RAM Locations 41h 42h 43 44h 45h 46 47h 48h
INPUT h h
Data (Before Execution)
Data in Ascending order
OUTPUT
(After Execution)
32. Experiment No.14 (b) Date:
Aim: Write an assembly language program (ALP) to arrange a block of data
In descending order.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I-RAM locations i.e. 41h onwards, assume that no. of data’s
are 8 i.e. counter is initialization as 08H (8 data’s).

store the result(i.e. data in descending order) in the same I-RAM locations i.e. 41h
onwards.
ORG 00H
MOV R5, #08H ; COUNTER
L4: MOV R0, #41H ; I-RAM ADDRESS
MOV R7, #07H ; NO.OF COMPARISIONS
L3: MOV A,@R0
INC R0
MOV 0F0H,@R0
CJNE A, B, L1
SJMP L2
L1: JNC L2
MOV @R0, A
DEC R0
MOV @R0, B
INC R0
L2: DJNZ R7, L3
DJNZ R5, L4
Dr.BRAGMRPW (SC), Karimnagar 59
Microcontroller Lab Department of ECE

HERE: SJMP HERE


END

Sample Result:
I-RAM Locations 41h 42h 43 44h 45h 46 47h 48h
INPUT h h
Data (Before Execution) 23 AF 45 12 87 94 65 39
Data in descending order
OUTPUT AF 94 87 65 45 39 23 12
(After Execution)

Memory Hex
Label Mnemonics Comments
Address Code
8000 7D Initialise R5 has counter with
MOV R5,#08H number of data as 08
8001 08
8002 78 Initializing R0 as internal memory
L4 MOV R0, #41H pointer.ie source address 41h
8003 41 onwards.
8004 7F Move the data 07H in R7 ie it
MOV R7,#07H indicates number of comparisons
8005 07
Data in I-RAM address is copied in
8006 E6 L3 MOV A,@R0 to A
8007 08 INC R0 Update the pointer R0
8008 86 Data in I-RAM address is copied in
MOV 0F0H,@R0 to Reg.B
8009 F0
800A B5 Compare the data in A with Data in
800B F0 CJNE A,B,L1 B, if they are not equal go to L1,
800C 02 otherwise proceed.
800D 80
SJMP L2 Jump to L2
800E 07
800F 50 If carry=0(if A data > next data) go
L1 JNC L2 to L2, otherwise proceed.
8010 05
Data in I-RAM address is copied in
8011 F6 MOV @R0,A to A(bring BIG data in to A)
Decrement the pointer R0
8012 18 DEC R0 (previous location)
8013 A6 Data in Reg.B is copied in to
MOV @R0,B I-RAM address (bring BIG data in
8014 F0 to memory location )
8015 08 INC R0 Update the pointer R0
8016 DF L2 DJNZ R7,L3 Decrement R7,if R7=0 stops,
Dr.BRAGMRPW (SC), Karimnagar 60
Microcontroller Lab Department of ECE

otherwise go to L3 (ie check


8017 FE weather all comparisons are over)
8018 DD Decrement R5,if R5=0 stops,
DJNZ R5,L4 otherwise go to L4 (ie check
8019 E8 weather all data are compared)
801A 80 HER
SJMP HERE Stops execution
801B FE E
801C 00 END End of the Program

Example:
I-RAM Locations 41h 42h 43 44h 45h 46 47h 48h
INPUT h h
Data (Before Execution)
Data in descending order
OUTPUT
(After Execution)

V.Counters &
Timers
Dr.BRAGMRPW (SC), Karimnagar 61
Microcontroller Lab Department of ECE

33. Experiment No.15 (a) Date:


Aim: Write an assembly language program (ALP) to implement an 8- bit
Up/down HEX counter on watch window.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
ORG 00H
MOV A, #00H
BACK: ACALL DELAY
INC A
JNZ BACK

BACK1: ACALL DELAY


DEC A
JNZ BACK1
HERE: SJMP HERE

DELAY: MOV R2, #0FFH


L3: MOV R3, #0FFH
L2: MOV R4, #04FH
L1: DJNZ R4, L1
DJNZ R3, L2
DJNZ R2, L3
RET
Dr.BRAGMRPW (SC), Karimnagar 62
Microcontroller Lab Department of ECE

END

RESULT: Accumulator A is incremented in the from


00, 01, 02…09,0A, 0B… 0F, 10, 11…FF
After A contents reaches FF then
Accumulator A is decremented in the from
FF, FE, FD…, 0F, 0E… 0A, 09, 08…00
Note: 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)

34. Experiment No.15b (1) Date:


Aim: Write an assembly language program (ALP) to produce required time
Delay using instructions.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Theory:

CPU executing an instruction takes a certain number of clock cycles; these are
referred as machine cycles. The length of machine cycle depends on the frequency of the
crystal oscillator connected to 8051.

One machine cycle has 12 clock periods.


One Clock period =1T state
So, One Machine cycle period =12 T States
= 12 * 1 / f
Where f = frequency of oscillator

For example, if the microcontroller clock frequency is 11.0592 MHz

One machine cycle period = 12 / 11.0592MHz = 1.085μs

Time delay = No. of Machine Cycles X One machine cycle period

Dr.BRAGMRPW (SC), Karimnagar 63


Microcontroller Lab Department of ECE

Program1:

To generate a delay of 0.5ms using 8051 with crystal frequency of 11.0592 MHz

Assume the program written as below

ORG 00H Machine Cycles


MOV R1, #N 1
HERE: DJNZ R1, HERE 2
RET 2
END

In the above program find the value of ‘N’

Solution:

1. No. of Machine cycles inside HERE loop is = 2 x N =2N


2. No. of Machine cycles (Adding the two instructions) outside loop = 1+2=3
3. Total No. of Machine cycles = 2N+3
4. Time delay is given as = 1ms
5. One machine cycle period = 12 / f (where f = 11.0592 MHz)
= 12 / 11.0592MHz = 1.085μs

Time delay = No. of Machine Cycles X One machine cycle period

0.5ms = 2N+3 * 1.085 μs


2N+3= 0.5ms / 1.085 μs
2N = 461-3
N = 458 / 2 = 229
Then in the above program N=229 decimal number i.e. equivalent Hex decimal =E5

ORG 00H
MOV R1, #0E5h
HERE: DJNZ R1, HERE

Dr.BRAGMRPW (SC), Karimnagar 64


Microcontroller Lab Department of ECE

RET
END
(OR)

ORG 00H
MOV R1, #229
HERE: DJNZ R1, HERE
RET
END
After execution of the program we observe the time delay in

35. Experiment No.15b (2) Date:


Aim: Write an assembly language program (ALP) to produce required time
Delay using Timers.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Theory:
To generate a time delay

1. Load the TMOD value register indicating which timer (timer 0 or timer 1)
is to be used and which timer mode operation (0 or 1) is selected.
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to
See if it is raised
􀂃 Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again.

Dr.BRAGMRPW (SC), Karimnagar 65


Microcontroller Lab Department of ECE

Program1:
To generate a delay of 5ms using timer1 in mode 1 with XTAL frequency of
11.0592 MHz

Solution:

Here Data to be loaded in TMOD =10H⇒Timer 1 in mode 1

Clock period (T) = 12 / 11.0592 MHz = 1.085microsec.

Number of clocks = 5millisec / 1.085microsec = 4608

Number to be loaded in to timer : 65536-4608=60,928D=EE00H.

ORG 00H
MOV TMOD, #10H ; Timer 1, mode 1(16-bit mode)
MOV TL1, #00H ; TL0=00H, the low byte
MOV TH1, #0EEH ; TH0=0EEH, the HIGH byte
SETB TR1 ; Start the timer 1
WAIT: JNB TF1, WAIT ; wait until timer 1 count up to maximum i.e.
Jump if TF1 = 0
CLR TR1 ; Stop timer 1
CLR TF1 ; Clear timer 1 flag
END

Program2:
To generate a delay of 19.94ms using timer0 in mode 1 with XTAL
frequency of 11.0592 MHz

Solution:

Here Data to be loaded in TMOD =01H⇒Timer 0 in mode 1

Clock period (T) = 12 / 11.0592 MHz = 1.085microsec.


Dr.BRAGMRPW (SC), Karimnagar 66
Microcontroller Lab Department of ECE

Number of clocks = 19.94 millisec / 1.085microsec = 18370

Number to be loaded in to timer: 65536-18370=47166D=B83Eh.

ORG 00H
MOV TMOD, #01H ; Timer 0, mode 1(16-bit mode)
MOV TL0, #3EH ; TL0=3EH, the low byte
MOV TH0, #0B8H ; TH0=0B8H, the HIGH byte
SETB TR0 ; Start the timer 0
WAIT: JNB TF0, WAIT ; wait until timer 0 count up to maximum i.e.
Jump if TF0 = 0
CLR TR0 ; Stop timer 0
CLR TF0 ; Clear timer 0 flag
END

Program3:
To generate a delay of 1Second using timer1 in mode 1 with XTAL frequency of
11.0592 MHz

Solution:

Here Data to be loaded in TMOD =10H⇒Timer 1 in mode 1

Clock period (T) = 12 / 11.0592 MHz = 1.085microsec.

Number of clocks = 1 Sec / 1.085microsec = 921659 pulses

which is greater than 65536 so calculate delay count


(ie 65536 * delay count = 921659)

Multiple delay count =921659/ 65536 = 14, BUT 65536*14= 917504 so take =15
65536*15=983040
i.e. = 921659/15 = 61,443 pulses repeated 15 times (61444*15=921645 near to 921659)

So take 15D in one register

Number to be loaded in to timer = 65536-61443=4093D=0FFDH.

Dr.BRAGMRPW (SC), Karimnagar 67


Microcontroller Lab Department of ECE

ORG 00H
MOV TMOD, #10H ; Timer 1, mode 1(16-bit mode)
MOV R1, #15 ; Delay count in decimal
AGAIN: MOV TL1, #0FDH ; TL0=0FDH, the low byte
MOV TH1, #0FH ; TH0=0FH, the HIGH byte
SETB TR1 ; Start the timer 1
WAIT: JNB TF1, WAIT ; wait until timer 1 count up to maximum i.e.
Jump if TF1 = 0
CLR TR1 ; Stop timer 1
CLR TF1 ; Clear timer 1 flag
DJNZ R1, AGAIN
END

Dr.BRAGMRPW (SC), Karimnagar 68


Microcontroller Lab Department of ECE

VI.Additional
Programs

36. Experiment No. (a) Date:


Aim: Write an assembly Language Program (ALP) to Transfer a block of data one
location to another locations in External RAM.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the E-RAM locations i.e. 8500h onwards, and this date is to be
transferred to another E-RAM locations i.e. 8600h onwards, assume that the no. of data’s
are 08 i.e. Counter is initialization as 08H(8 data’s).

ORG 00H
MOV DPTR, #8500H

Dr.BRAGMRPW (SC), Karimnagar 69


Microcontroller Lab Department of ECE

MOV R0, #40H


MOV R4, #08H
TOP1: MOVX A,@DPTR
MOV @R0, A
INC DPTR
INC R0
DJNZ R4, TOP1
MOV DPTR, #8600H
MOV R0, #40H
MOV R4, #08H
TOP2: MOV A,@R0
MOVX @DPTR, A
INC R0
INC DPTR
DJNZ R4, TOP2
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 90
Initialise the data pointer.ie source
8001 85 MOV DPTR,8500H address 8500h onwards.
8002 00
8003 78 Initialise the R0 as internal memory
MOV R0,#40H pointer.
8004 40
8005 7C Initialise R4 has counter with
MOV R4,#08H number of data as 8
8006 08
Data in E-RAM address 8500h
8007 E0 TOP1 MOVX A,@DPTR onwards are copied in to A
Data in A is copied in to I-RAM
8008 F6 MOV @R0,A address 40h onwards.
8009 A3 INC DPTR Update the data pointer
800A 08 INC R0 Update the pointer R0
Dr.BRAGMRPW (SC), Karimnagar 70
Microcontroller Lab Department of ECE

800B DC Decrement R4, if R4=0,stop


DJNZ R4,TOP1 otherwise go to TOP1
800C FA
800D 90
Initialise the data pointer.ie
800E 86 MOV DPTR,8600H destination address 8600h onwards.
800F 00
8010 78 Re-Initializing R0 as internal
MOV R0,#40H memory pointer.ie source address
8011 40 40h onwards.
8012 7C Re-Initialise R4 has counter with
MOV R4, #08H number of data as 8
8013 08
Data in I-RAM address 40h
8014 E6 TOP2 MOV A,@R0 onwards are copied in to A
Data in A is copied in to E-RAM
8015 F0 MOVX @DPTR,A address 8600h onwards.
8016 A3 INC DPTR Update the data pointer
8017 08 INC R0 Update the pointer R0
8018 DC Decrement R4, if R4=0,stop
DJNZ R4,TOP2 otherwise go to TOP2
8019 FA
801A 00 END End of the program

Sample Result:

E-RAM I-RAM E-RAM


Address Data Address Data Address Data
locations locations locations
Then
8500h Data in 40h 8600h
23 E-RAM
23 Data in 23
I-RAM
8501h 45 address 41h 45 address
8601h 45
location is
8502h AF transferred 42h AF location is 8602h AF
transferred
8503h D5 to I-RAM 43h D5 to E-RAM 8603h D5
location
8504h 44h location 8604h
E7 through E7 through
E7
Accumulator
8505h 88 45h 88 Accumulator 8605h 88
8506h 93 46h 93 8606h 93
8507h 53 47h 53 8607h 53

Dr.BRAGMRPW (SC), Karimnagar 71


Microcontroller Lab Department of ECE

Example:

INPUT OUTPUT
E-RAM Address E- RAM Address
Data Data
locations locations

8500h 8600h
8501h 8601h
8502h 8602h
8503h 8603h
8504h 8604h
8505h 8605h
8506h 8606h
8507h 8607h

37. Experiment No. (b) Date:


Aim: Write an assembly Language Program (ALP) to Exchange a block of
Data in Internal - RAM locations
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:
Let the Data is taken in the I- RAM locations i.e. 40h and 60h onwards, and the date in
these memory locations are to be Exchanged, assume that the no. of data’s are 10 i.e.
Counter is initialization as 0AH(10 data’s).

ORG 00H
MOV R0, #40H
MOV R1, #60H
MOV R7, #0AH
TOP: MOV A,@R0
XCH A,@R1
MOV @R0, A
Dr.BRAGMRPW (SC), Karimnagar 72
Microcontroller Lab Department of ECE

INC R0
INC R1
DJNZ R7, TOP
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#40H pointer.ie source address 40h
8001 40 onwards.
8002 79 Initializing R1 as internal memory
MOV R1, #60H pointer.ie destination address 60h
8003 60 onwards.
8004 7F Initialise R7 has counter with
MOV R7,#0AH number of data as 10
8005 0A
Data in I-RAM address 40h are
8006 E6 TOP MOV A,@R0 copied in to A
Exchange the contents of A with
8007 C7 XCH A,@R1 I-RAM address 60h onwards
Data in A is copied in to I-RAM
8008 F6 MOV @R0,A address 40h onwards
8009 08 INC R0 Update the pointer R0
800A 09 INC R1 Update the pointer R1
800B DF Decrement R7, if R7=0,stop
DJNZ R7,TOP otherwise go to TOP
800C F9
800D 00 END End of the program

Example:

INPUT OUTPUT
I- RAM I- RAM I- RAM I- RAM
Address Data Address Data Address Data Address Data
locations locations locations locations

40h 60h 40h 60h


41h 61h 41h 61h
42h 62h 42h 62h
43h 63h 43h 63h
44h 64h 44h 64h
45h 65h 45h 65h
46h 66h 46h 66h
47h 67h 47h 67h
48h 68h 48h 68h
49h 69h 49h 69h
Dr.BRAGMRPW (SC), Karimnagar 73
Microcontroller Lab Department of ECE

38. Experiment No. (c) Date:


Aim: Write an assembly language program (ALP) to count no. of 1’s and no. of 0’s
of a given 8-bit number.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the data i.e. 8-bit number is taken at I-RAM address 50h and Store the Result in the
registers R5 (i.e. No. of 1’s) and R6 (i.e. No. of 0’s)

ORG 00H
MOV R0, #50H
MOV A, @R0

Dr.BRAGMRPW (SC), Karimnagar 74


Microcontroller Lab Department of ECE

MOV R7, #08H ; COUNTER


MOV R5, #00H ; RESULT No. of 1’s
MOV R6, #00H ; RESULT No. of 0’s
TOP3: RRC A
JNC TOP1
INC R5
SJMP TOP2
TOP1: INC R6
TOP2: DJNZ R7, TOP3
END

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#50H pointer.ie source address 50h
8001 50
Move the data in I-RAM address
8002 E6 MOV A,@R0 50h in to A
8003 7F Initialise R7 has counter with
MOV R7, #08H number as 08(ie it indicates no. of
8004 08 bits in the given 8bit number)
8005 7D Initialise R5 as 00 to store Result
MOV R5,#00H i.e. No. of 1’s
8006 00
8007 7E Initialise R6 as 00 to store Result
MOV R6,#00H i.e. No. of 0’s
8008 00
Rotate Right the accumulator
8009 13 TOP3 RRC A contents by one bit along with carry
800A 50 Jump to TOP1 if no carry ie Carry
JNC TOP1 flag bit=0, otherwise proceed.
800B 03
800C 0D INC R5 Update the R5 contents
Dr.BRAGMRPW (SC), Karimnagar 75
Microcontroller Lab Department of ECE

800D 80
SJMP TOP2 Jump to TOP2
800E 01
800F 0E TOP1 INC R6 Update the R6 contents
8010 DF Decrement R7, if R7=0,stop
TOP2 DJNZ R7,TOP3 otherwise go to TOP2
8011 FA
8012 00 END End of the program

Sample Result:
I-RAM Address Location 50h
INPUT
Data = 62H in binary ( 0110 0010 ) 62H
No. of 1’s in the given Number :R5 03
OUTPUT
No. of 0’s in the given Number:R6 05

Example:
I-RAM Address Location 50h
INPUT
Data
No. of 1’s in the given Number :R5
OUTPUT
No. of 0’s in the given Number:R6

39. Experiment No. (d) Date:


Aim: Write an assembly language program (ALP) to find the Largest Number in the
given block of Data.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I- RAM locations 50h onwards, assume the no. of data’s are
10 i.e. Counter is initialization as 0AH(10 data’s)and Store the Result i.e. the Largest
Number among the block of data in the I-RAM Address Location 70h.

ORG 00H;
MOV R0, #50H ; MEMORY ADDRESS OF DATA
MOV R7, #0AH ; CONTER i.e. NO.OF DATAS
MOV A,@R0
Dr.BRAGMRPW (SC), Karimnagar 76
Microcontroller Lab Department of ECE

MOV 0F0H, A
DEC R7
BACK: INC R0
MOV A,@R0
CJNE A, B, L1
SJMP L2
L1: JC L2
MOV B, A
L2: DJNZ R7, BACK
MOV 70H, B
HERE: SJMP HERE
END

Sample Result:

INPUT: 50 53 57
I-RAM Address 51h 52h 54h 55h 56h 58h 59h
h h h
Data 45 67 12 36 80 93 A5 F3 78 29
OUTPUT: at 70h
Largest Number
F3

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#50H pointer.ie source address 50h
8001 50 onwards.
8002 7F Initialise R7 has counter with
MOV R7,#0AH number of data as 10
8003 0A
Data in I-RAM address 50h are
8004 E6 MOV A,@R0 copied in to A
8005 F5 Data in A is copied in to Reg.B ie
MOV 0F0H,A it’s I-RAM address as 0F0h.
8006 F0
8007 1F DEC R7 Decrement the contents of R7 by 1
BAC
8008 08 INC R0 Update the pointer R0
K
Data in I-RAM address are copied
8009 E6 MOV A, @R0 in to A
800A B5 Compare the Data-1 and Data-2 if
800B F0 CJNE A,B,L1 they are not equal go to L1,
800C 02 otherwise proceed.
800D 80
SJMP L2 Jump to L2
800E 04
800F 40 L1 JC L2 If carry=1 ie (A < B) go to L2,
Dr.BRAGMRPW (SC), Karimnagar 77
Microcontroller Lab Department of ECE

8010 02 otherwise proceed.


8011 F5
MOV B,A Big number is copied in to Reg.B
8012 F0
8013 DF Decrement R7, if R7=0,stop
L2 DJNZ R7,BACK otherwise go to BACK
8014 F3
8015 85
Big number in Reg.B is copied in to
8016 F0 MOV 70H,B I-RAM address 70h i.e. Result
8017 70
8018 80
HERE SJMP HERE Stop execution
8019 FE
801A 00 END End of the program

Example:

INPUT: 52 55
I-RAM Address 50h 51h 53h 54h 56h 57h 58h 59h
h h
Data
OUTPUT: at 70h
Largest Number

40. Experiment No. (e) Date:


Aim: Write an assembly language program (ALP) to find the Smallest Number in the
given block of Data.
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the Data is taken in the I- RAM locations i.e. 40h onwards, assume that
the no. of data’s are 10 so Counter is initialization as 0AH(10 data’s)and Store the
Result i.e. Smallest Number in the I-RAM Address Location 60h.

ORG 00H;
MOV R0, #40H ; MEMORY ADDRESS OF DATA
MOV R7, #0AH ; CONTER ie NO.OF DATAS
MOV A,@R0
MOV 0F0H, A
DEC R7
BACK: INC R0
MOV A,@R0
Dr.BRAGMRPW (SC), Karimnagar 78
Microcontroller Lab Department of ECE

CJNE A, B, L1
SJMP L2
L1: JNC L2
MOV B, A
L2: DJNZ R7, BACK
MOV 60H, B
HERE: SJMP HERE
END

Sample Result:

INPUT: 40 43 47
I-RAM Address 41h 42h 44h 45h 46h 48h 49h
h h h
Data 45 67 12 36 80 93 A5 F3 78 29
OUTPUT: at 60h
Smallest Number
12

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#40H pointer.ie source address 40h
8001 40 onwards.
8002 7F Initialise R7 has counter with
MOV R7,#0AH number of data as 10
8003 0A
Data in I-RAM address 40h are
8004 E6 MOV A,@R0 copied in to A
8005 F5 Data in A is copied in to Reg.B ie
MOV 0F0H,A it’s I-RAM address as 0F0h.
8006 F0
8007 1F DEC R7 Decrement the contents of R7 by 1
BAC
8008 08 INC R0 Update the pointer R0
K
Data in I-RAM address are copied
8009 E6 MOV A, @R0 in to A
800A B5 Compare the Data-1 and Data-2 if
800B F0 CJNE A,B,L1 they are not equal go to L1,
800C 02 otherwise proceed.
800D 80
SJMP L2 Jump to L2
800E 04
800F 50 If carry=0 ie (A > B) go to L2,
L1 JNC L2 otherwise proceed.
8010 02
8011 F5 Small number is copied in to
MOV B,A Reg.B
8012 F0
8013 DF L2 DJNZ R7,BACK Decrement R7, if R7=0,stop

Dr.BRAGMRPW (SC), Karimnagar 79


Microcontroller Lab Department of ECE

8014 F3 otherwise go to BACK


8015 85
Small number in Reg.B is copied
8016 F0 MOV 60H,B in to I-RAM address 60h i.e. Result
8017 60
8018 80
HERE SJMP HERE Stop execution
8019 FE
801A 00 END End of the program

Example:

INPUT: 42 45
I-RAM Address 40h 41h 43h 44h 46h 47h 48h 49h
h h
Data
OUTPUT: at 60h
Smallest Number
41. Experiment No. (f) Date:
Aim: Write an assembly language program (ALP) to find the sum of first N- natural
numbers
Apparatus: PC, Keil software, 8051 Trainer Kit.

Program:

Let the data i.e. N value is taken in to the I-RAM Location 40h and store the
Result in the I-RAM Location 45h.

ORG 00H
MOV R0, #40H ; data input
MOV A,@R0
MOV R1, A
MOV A, #00H
CLR C
UP: ADDC A, R1
DJNZ R1, UP
MOV 45H, A ; Result
HERE: SJMP HERE
END

Dr.BRAGMRPW (SC), Karimnagar 80


Microcontroller Lab Department of ECE

Sample Result:

If N=10H (Hexadecimal Number) then Result is also in Hex decimal only


Result=10+F+E+D+C+B+A+9+8+7+6+5+4+3+2+1=88H

Memory Hex
Label Mnemonics Comments
Address Code
8000 78 Initializing R0 as internal memory
MOV R0,#44H pointer.ie source address 44h.
8001 44
Data in I-RAM address 44h is
8002 E6 MOV A,@R0 copied in to A
8003 F9 MOV R1,A Data in A is copied in to Reg.R1
8004 74
MOV A,#00H Move data 00h is copied in to A
8004 00
8006 C3 CLR C Set the carry flag bit as 0
Add the contents of A with R1
8007 39 UP ADDC A,R1 along carry flag bit
8008 D9 Decrement R1, if R1=0,stop
DJNZ R1,UP otherwise go to UP
8009 FD
800A F4 Data in A is copied in to I-RAM
MOV 40H,A address 40h ie Result
800B 40
800C 80
HERE SJMP HERE Stops execution
800D FE
800E 00 END End of the program

Example:

Dr.BRAGMRPW (SC), Karimnagar 81


Microcontroller Lab Department of ECE

I-RAM Address location 40h


INPUT
Data (N)
I-RAM Address location 45h
OUTPUT
Result

42. Experiment No. (g) Date:


Aim: Write an assembly Language Program (ALP) to perform Multi-byte
Addition (32-bit Addition)

Apparatus: PC, Keil software, 8051 Trainer Kit.


Program:
Let the Data is taken in External-RAM Locations 8300h to 8303h (first 32-bit
Number), 8304H to 8307H (Second 32-bit Number) and Store the Result in the locations
8308h to 830Ch.
ORG 00H
MOV DPTR, #8300H ; E-RAM Initialization
MOV R0, #40H ; I-RAM location to store 1st 32-bit No.
MOV R1, #40H ; I-RAM location to store 2nd 32-bit No.
MOV R2, #04H ; Counter initialization as=04 indicates No. of
TOP1: MOVX A,@DPTR bytes in the given number.
MOV @R0, A
INC DPTR
INC R0
DJNZ R2, TOP1
MOV R2, #04H
TOP2: MOVX A,@DPTR
MOV @R1, A
INC DPTR
INC R1
DJNZ R2, TOP2
MOV R0, #40H
Dr.BRAGMRPW (SC), Karimnagar 82
Microcontroller Lab Department of ECE

MOV R1, #40H


MOV R2, #04H
CLR C
TOP3: MOV A,@R0
ADDC A,@R1
MOVX @DPTR, A
INC R0
INC R1
INC DPTR
DJNZ R2, TOP3
JC TOP4
MOV A, #00H
MOVX @DPTR, A
SJMP TOP4
TOP4: MOV A, #01H
MOVX @DPTR, A
TOP4: SJMP TOP4
END
Memor
Hex
y Label Mnemonics Comments
Code
Address
8000 90
Initialise the data pointer.ie source
8001 83 MOV DPTR,8300H address 8300h
8002 00
8003 78 Initialise the R0 as internal memory
MOV R0,#40H pointer.ie for 1st 16-bit number
8004 40
8004 79 Initialise the R1 as internal memory
MOV R1,#40H pointer.ie for 2nd 16-bit number
8006 40
8007 7A
MOV R2,#04H Counter initialization. as 04
8008 04
Data in E-RAM address 8300h
8009 E0 TOP1 MOVX A,@DPTR copied in to A
Data in A is copied in to I-RAM
800A F6 MOV @R0,A address 40h onwards.
800B A3 INC DPTR Update the data pointer
800C 08 INC R0 Update the pointer R0
800D DA Decrement count, if count=0,stop
DJNZ R2,TOP1 otherwise go to TOP1
800E FA
800F 7A
MOV R2,#04H Counter re- initialization.
8010 02
Data in E-RAM address 8304h
8011 E0 TOP2 MOVX A,@DPTR copied in to A
Data in A is copied in to I-RAM
8012 F7 MOV @R1,A address 40h onwards
8013 A3 INC DPTR Update the data pointer
8014 09 INC R1 Update the pointer R1
8014 DA DJNZ R2,TOP2 Decrement count, if count=0,stop

Dr.BRAGMRPW (SC), Karimnagar 83


Microcontroller Lab Department of ECE

8016 FA otherwise go to TOP2


8017 78 Re-initialise the R0 as internal
MOV R0,#40H memory pointer.ie for 1st 16-bit
8018 40 number
8019 79 Re- Initialise the R1 as internal
MOV R1,#40H memory pointer.ie for 2nd 16-bit
801A 40 number
801B 7A
MOV R2,#04H Counter initialization. as 04
801C 04
801D C3 CLR C Set the carry flag to 0
Data in I-RAM address 40h is
801E E6 TOP3 MOV A,@R0 copied in to A
Add the data in 40h with the data
801F 37 ADDC A,@R1 in 40h
Data in A is copied in to E-RAM
8020 F0 MOVX @DPTR,A address 8308h
8021 A3 INC DPTR Update the data pointer
8022 08 INC R0 Update the pointer R0
8023 09 INC R1 Update the pointer R1
8024 DA Decrement count, if count=0,stop
DJNZ R2,TOP3 otherwise go to TOP3
8024 F8
8026 40 If carry=1, go to TOP4, otherwise
JC TOP4 continue.
8027 04
8028 74
MOV A,#00H Move the data 00H in to A
8029 00
Data in A is copied in to E-RAM
802A F0 MOVX @DPTR, A address 830Ch
802B 80
SJMP TOP4 Jump to TOP4
802C 03
802D 74
TOP4 MOV A,#01H Move the data 01H in to A
802E 01
Data in A is copied in to E-RAM
802F F0 MOVX @DPTR, A address 830Ch
8030 80
TOP4 SJMP TOP4 Stop execution
8031 FE
8032 00 END End of the program

Sample Result:
1st 32 –bit Number
(8303h) (8302h) (8301h) (8300h)
(Memory Address) ---
12 34 56 78
DATA
INPUT
2nd 32 –bit Number
(8307h) (8306h) (8305h) (8304h)
(Memory Address) ---
87 65 43 21
DATA
(Memory Address) (830Ch) (830Bh) (830Ah) (8309h) (8308h)
OUTPUT
Result 00 99 99 99 99

Dr.BRAGMRPW (SC), Karimnagar 84


Microcontroller Lab Department of ECE

Example:

1st 32 –bit Number (8303h) (8302h) (8301h) (8300h)


(Memory Address) ---
DATA
INPUT
2nd 32 –bit Number (8307h) (8306h) (8305h) (8304h)
(Memory Address) ---
DATA

OUTPUT (Memory Address) (830Ch) (830Bh) (830Ah) (8309h) (8308h)


Result

Viva Questions
1. What is Microcontroller?

2. List out the differences between Microcontroller and Microprocessor.

3. How are Microcontrollers more suitable than Microprocessor for Real Time

Applications?

4. What are the General Features of Microcontroller?

4. Explain the general features of 8041 Microcontroller.

6. How many pin the 8041 has?

7. Differentiate between Program Memory and Data Memory.

8. What is the size of the Program and Data memory?

9. Write a note on internal RAM. What is the necessity of register banks? Explain.

10. How many address lines are required to address 4K of memory? Show the necessary

calculations.

11. What is the function of accumulator?

12. What are SFR’s? Explain briefly.


Dr.BRAGMRPW (SC), Karimnagar 85
Microcontroller Lab Department of ECE

13. What is the program counter? What is its use?

14. What is the size of the PC?

14. What is a stack pointer (SP)?

16. What is the size of SP?

17. What is the PSW? And briefly describe the function of its fields.

18. What is the difference between PC and DPTR?

19. What is the difference between PC and SP?

20. What is ALE? Explain the functions of the ALE in 8041.

21. Describe the 8041 oscillator and clock.

22. What is the Data Pointer register? What is its use in the 8041?

23. Describe in detail how the register banks are organized.

24. What are the bit addressable registers and what is the need?

24. What is the need for the general purpose RAM area?

26. Write a note on the Stack and the Stack Pointer.

27. Why should the stack be placed high in internal RAM?

28. Explain briefly how internal and external ROM gets accessed.

29. What are the different addressing modes supported by 8041 Microcontroller ?

30. Explain the Immediate Addressing Mode.

31. Explain the Register Addressing Mode.

32. Explain the Direct Addressing Mode.

34. Explain the Indirect Addressing Mode.

34. Explain the Code Addressing Mode.

36. Explain in detail the Functional Classification of 8041 Instruction set

37. What are the instructions used to operate stack?


Dr.BRAGMRPW (SC), Karimnagar 86
Microcontroller Lab Department of ECE

38. What are Accumulator specific transfer instructions?

39. What is the difference between INC and ADD instructions?

40. What is the difference between DEC and SUBB instructions?

41. What is the use of OV flag in MUL and DIV instructions?

42. What are single and two operand instructions?

43. Explain Unconditional and Conditional JMP and CALL instructions.

44. Explain the different types of RETURN instructions.

44 What is the difference between Timer and Counter operation?

46. How many Timers are there in 8041?

47. What are the three functions of Timers?

48. What are the different modes of operation of timer/counter?

49. What is the difference between mode 0 and mode 1?

40. How do you differentiate between Timers and Counters?

41. Explain the function of the TMOD register and its various fields?

42. How do you control the timer/counter operation?

43. What is the function of TF0/TF1 bit

44. Explain the function of the TCON register and its various fields?

44. What are the various registers and pins in the 8041 required for Serial

communication

46. What are interrupts and how are they useful in Real Time Programming?

47. Briefly describe the Interrupt structure in the 8041.

48. What are the five interrupts provided in the 8041?

49. What are the three registers that control and operate the interrupts in 8041?

60. Describe the Interrupt Enable (IE) special function register and its various bits.
Dr.BRAGMRPW (SC), Karimnagar 87
Microcontroller Lab Department of ECE

61. Describe the Interrupt Priority (IP) special function register and its need.

62. Explain in detail how the Timer Flag interrupts are generated.

63. Explain in detail how the Serial Flag interrupt is generated.

64. Explain in detail how the External Flag interrupts are generated.

64. What happens when a high logic is applied on the Reset pin?

66. Why the Reset interrupt is called a non-maskable interrupt?

67. Why do we require a reset pin?

68. How can you enable/disable some or all the interrupts?

69. Explain how interrupt priorities are set? And how interrupts that occur

simultaneously are handled.

70. What are the actions taken when an Interrupt Occurs?

71. What are Software generated interrupts and how are they generated?

72. What is the significance of DA instruction?

73. What is packed and unpacked BCD?

74. What is the difference between CY and OV flag?

74. When will the OV flag be set?

76. What is an ASCII code?

Dr.BRAGMRPW (SC), Karimnagar 88


Microcontroller Lab Department of ECE

Dr.BRAGMRPW (SC), Karimnagar 89


Microcontroller Lab Department of ECE

Dr.BRAGMRPW (SC), Karimnagar 90


Microcontroller Lab Department of ECE

Dr.BRAGMRPW (SC), Karimnagar 91


Microcontroller Lab Department of ECE

Dr.BRAGMRPW (SC), Karimnagar 92

You might also like