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

Unit-4 8051 Assembly Language Programming Technical

The document provides an overview of the programming model and addressing modes of the 8051 microcontroller, including details on stack operations, subroutines, and various addressing techniques such as immediate, register, direct, and indirect addressing. It outlines the structure of internal memory, the use of the stack pointer, and the concept of modular programming through subroutines. Additionally, it explains different addressing modes with examples, emphasizing their significance in assembly language programming for the 8051 architecture.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit-4 8051 Assembly Language Programming Technical

The document provides an overview of the programming model and addressing modes of the 8051 microcontroller, including details on stack operations, subroutines, and various addressing techniques such as immediate, register, direct, and indirect addressing. It outlines the structure of internal memory, the use of the stack pointer, and the concept of modular programming through subroutines. Additionally, it explains different addressing modes with examples, emphasizing their significance in assembly language programming for the 8051 architecture.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Questions

Short
Directives
Assembler
7.19 7.18 7.15
16 Examples.
.17 7.Programming 7.13 7.12
7.Instruction
14 Timings Arithmetic
7.9
7.11 7.10 Instructions 7.8 7.7 7.6 Addressing
7.5 Modes7.4 7.3 7.2Contents
7.1 generating
programming.
IDE.Programming
arithmetic
Syllabus
VO/OUse Time Jump Rotate Bit ByteClassification Concept
Concept
Programming
Data
Level
Bit Port of Delay Level Transfer time and
Manipulation
Programming Look-up and and
Logical of of Concept
delay, logicalmodel
7
for CALL Swap Logical SubroutineStack
Instructions of Model
Table 8051 Instruction code group, of
and Instructions Instructions
Instructions. Instructions 8051,
Language
Assembly
8051
of
Programming of conversionsstack,
Answers branching
Addressing
8051
Set
subroutine
of
8051 in
instructions,
Programming
(7- 1) assembly modes,
and
programming
related
language
Summerr12,
Winter-18,
Summer-12,
Winter-15,
Winter-15, Winter-09,
Winter-06 Summer-17,
Summer-16,
Winter-10,
Winter-11, 18Winter-10,
12, Winter-13,
16,Summerr11,
18,19, Winter-13,19,
Summer-17 16,17
Winter-10,
19,16,Summer-13,
16,13, Summer-11, bit
manipulation
instructions,
of
8051 of
Summer-18, 11, 13, 11, 8051
13, 12, and
12, 18, 16, instructions
17, 16, 17, writing based
13, 19, testing
18, 17, 18,
15, on
19 19 the
programs
17, " and data
Marks 7" 7 Marks 4 Marks 7 Marks 7 Marks 7 same
Marks 7 Marks 18, Marks 3 Marks 7 Marks 4 transfer,
IO
19, using
for Port
MicroprOcessors and Microcontrollers 7-2 8051 ASsemoy Language

7.1 Programming Model of 8051


Programming
Fig. 7.1.1 shows the programming model of 8051. It includes all the
internal memory locations accessible to the programmer. registers and

Register Register Register Register


bank 0 bank 1 bank 2 bank 3
00 08 10 18
01 09 11 19
02 OA 12 1A
03 0B 13 1B
04 0C 14 1C
05 OD 15 1D
06 OE 16 1E
07 OF 17 1F

Bit
addressed Special function registers Internal
RAM ROM
20 80 PO 90 P1 000
2F 81 SP 98 SCON
82 DPL 99 SBUF
83 DPH A0 P2
87 PCON A8 IE
General
purpose 88 TCON BO P3
RAM
89 TMOD B8 IP
30
8A TLO DO PSW
8B TL1 E0 ACC
8C THO FO
7F 8D TH1 FFF

Fig. 7.1.1 Programming model of 8051


Review Question

1. Draw the programming model of 8051.


Microprocessors and MicrOcontrollers 7-3 8051 Assembly Language Programming

7.2 Concept of Stack


The stack refers to an area of internal RAM that is used to store and retrieve data
quickly. 1he stack pointer register is used by the 8051 to hold an internal RAM address
that is called top of stack. The stack pointer register is 8-bit wide. It is increased before
data is stored during PUSH and CALL instructions and decremented after data is
restored during POP and RET instructions.
The stack array can reside anywhere in on-chip RAM. The stack pointer is initialized
to 07H after a reset. This causes the stack to begin at location 08H. The operation of
stack and stack pointer is illustrated in Fig. 7.2.1.
On-chip RAM On-chip RAM On-chip RAM

08
SP 07 08 809
06 SP
Data 08
07
Stack pointer SP SP+1 07

(a) Status of stack and (b) Store operation


stack pointer of reset

Data 1 09 09
SP Data 2 08
Data 3 07 SP SP-1 - 07
Stack pointer

(c) Read operation


Fig. 7.2.1
The stack may overwrite data in the register banks, bit-addressable RAM and
scratch-pad RAM. Thus to avoid conflict with the register, bit-addressable RAM and
scratch-pad RAM data, the stack is initialized at a higher location in the internal RAM.

Review Questions

1. Explain the operation of stack in 8051.


2. Define SP.

7.3 Concept of Subroutine


The modular programming is a go0d programming concept. n which, large
programs are divided into small subroutines. A subroutine is a sequential group of
Instructions stored in the memory at specific address for performing repeatedly needed
TECHNICAL PUBLICATIONS -An up thrust for knowledge
7-4 8051 Assembly Language
Microprocessors and Microcontrollers
Programming
task. Whenever, it is necessary to perform that particular task, subroutine is called 6
the main program.
The 8051 microcontroller has two instructions to implement subroutines : CALI. an
RET. CALL instruction is used to call a subroutine in the main program and RET
instruction is the last instruction in the subroutine to return it back to the main program.
The CALL instruction saves the address of the instruction following it on stack and then
transfers the program control to the first instruction in the subroutine. When subroutine
execution is completed the RET instruction reads the return address from the stack and
transfers control back to the instruction following the CALL. This is illustrated in the
Fig. 7.3.1.
Main program
Subroutine
0080H LCALL 2000H
0083H 2000H

Subroutine
body
RET

Fig. 7.3.1 Subroutine processing


Review Questions

1. What is subroutine ?
2. Explain the subroutine processing.

7.4 Addressing Modes GTU : Winter-10,11,16,17,18,19, Summer-13,16,1


The way, using which the data sources or destination addresses are
specified n u
instruction mnemonic for moving the data, is called 'addressing mode'. This secu
explains addressing modes used in 8051 with examples.
7.4.1 Immediate Addressing
In this addressing mode source operand is a constant rather than a
constant can be incorporated in the instruction. Sign "#" indicates it variable.
is a immediate
addressing mode.
Destination register
Data specified
in the instruction

Example: Add the constant 52


MOV A, #52 decimal in accumnulator.
Microprocessors and Microcontrollers 7-5 8051 Assembly Language Programming
7.4.2 Register Addressing
The 805l can access eight "working registers" (R0-R7).
Three bit code within the
instruction selects one of the eight registers from the selected register
register addressing mode, the data to be operated is within the bank. In the
registers.
Example:
MOV A, R3 Copies content of R3 to A
R AA R4

Note The advantage of register addressing is that the


instruction length is small; it is
asinglebyte.

7.4.3 Direct Addressing


Memory

Destination register
Address of memory
Data from within the instruction
selected memory
location

Direct addressing can access any on-chip variable or hardware register. ie. on-chip
RAM and special function register. The most significant bit of the address decides
whether it is a location within on-chip RAM (MSB = 0) or in special furnction register
(MSB = 1).
Example: Add the contents of locations 50H and 51H
MOV A50Hload byte from address 50H into A
ADD A51H Add the contents of A and the ontentsatmemory location 51

74.4 Indirect or Register Indirect Addressing


In this addressing mode RO and R1 of each register bank can be used as an index or
pointer register. RO and R1 point to the contents in the RAM. The instruction with
ndirect addressing uses the '@ sign.
Indirect addressing accesses data in dynamic marner rather than static manner.
Looping is not possible in direct addressIng mode. In indirect addressing we can
successive locations.
ncrement the index or pointer register to access

TECHNICAL PUBLICATIONS -An up thrust for knowledge


7-6 8051 Assembly Language
Microprocessors and Microcontrollers Programminn
Memory

Register

Destination register
Contents of register are
used to point memory
Data from
selected memory
location

RO, R1 and DPTR are the only registers that can be used for pointers in register
indirect addressing mode.
Example: ADD the contents of memory location addressed by register 1 to the
contents of RAM location pointed by register 0.
MOV A, @RO ; load the contents pointed by RO in A
ADD A, @RI ;Add the contents of A and the contents pointed by R1
MOVX A, @ DPTR ;Copy the contents of extemal data memory
Pointed by DPTR intothe accumulator
7.4.5 Relative Addressing
The JMP JUMP) and CALL instructions in 8051 use the relative addressing. We
know that, a JMP and CALL instructions replace the contents of the program counter
with anew program address. In relative addressing, a new program address is not
directed specified. It is specified as a difference, in bytes, of the new address from the
address in the program counter called offset. Thus, PC + offset gives the new program
address. The relative offset is a signed &-bit value gives an address range of + 127 to
-128 locations.

Program Code
memory
SJMP NEXT
1000 H 80 Opcode of SJMP
1001 H 03 Offset
1002 H
NEXT: 1003 H
1004 H 1002H + 3 = 1005H
1005 H
NEXT:
1006 H

Flg. 7.4.1 Relative addressing mode


TECHNICAL PUBLICATIONS- An up thrust for
MicroproceSsors and Microcontrollers 7-7 8051 Assembly Language Programming

7.4.6 Absolute Addressing


This addressing mode is similar to that of relative addressing except the offset length.
Here, the offset is 11-bit instead of 8-bit incase of relative addressing. Here, instruction
is two-bytes and the lowest 11-bits of instruction gives offset. In 8051, AJMP and
ACALL (Absolute jump and absolute call) ses absolute addressing. The 11-bit offset
gives an address range of from 0000H to 07FFH.
Example :
AJMP NEXT Executes the instruction at label NEXT after
execution of the cuent instruction
ACALL SUB1 Calls the subrOutines that is started at the label SUB

7.4.7 Long Addressing


In the long addressing mode, the new address for JMP or CALL instruction (LJMP or
LCALL) is specified as the entire new 16-bit address. Since address is 16-bit, a jump or a
call can be made to alocation within a 64-kbyte (0000- FFFFH) code memory space.

Example :
LJMP 2000H the tntIuction at adcress 2000
after execut instruCion
LCALL 3000H Cals the subroutine that started at the address 3000H

7.4.8 Indexed Addressing


addressing. Either the DPTR or
Only program memory can be accessed in the index
PC can be used as an index register.
DPTR Register

Program memory

Contents of DPTR register


A Register
Address of
Data from
memorY
selected memory

Contents of register A

TECHNICAL PUBLICATIONS- An up thrust for knowledge


Example : Read data from the program memory.
contente of memory locati
MOVC A, @A+ DPTR:This instruction copies the
: pointed by sum of the accumulator A and the register
;DPTR into the accumulator A.
of
MOV A, @A+ PC This instruction copies the contents
memory location pointed by the sum of
;the aCcumulator A and the prograrn counter
;PO) in the acoumulator A.

7.4.9 Inherent Addressing


Inherent in the instruction, these refer to a specific register such as accumulator
DPTR.

Example:
SWAP A Swap nibbles within the Accumulator

7.4.10 Bit Inherent Addressing


In this addressing, the address of the flag which contains the operand, is implied in
the opcode of the instruction.
Example :
CLR C Clears the carry lag to

7.4.11 Bit Direct Addressing


The RAM address space from 20H to 2FH and some Special Function Registers (SFRS)
are bit addressable.

Example:
SETB O6H Sets the bit 6 of 20H RANM space
CLR P1.2 Cears the bit-2 of port

|7.4.12 Stack Addressing Mode


It is subtype of direct addressing mode in which stack
are used. Instruction such as 'PUSH A' is invalid.
instructions (PUSH and O)
Here, we have to specify the adae
of register A. Thus, PUSH 0EOHH is a valid instruction; it
accumulator on the stack. pushes/stores the contents
Examples:
PUSH 04 : Push R4 onto stack
PUSH 06 Push R6 onto stack
POP 02 Pop top of stack into R2
POP OFOH Pop top of stack into register B
MicroprOceSSors and Microcontrollers 7-9 8051 Assembly Language Programming

Review Questions

1. Explain follotving addressing modes of 8031 microcontroller GTU: Winter-10, Marks 7


1) MOV A, # 20h 2) MOV A, 30h 3) MOV A, @R0
4) MOVX A, @DPTR 5) MOVC A, @A+DPTR
2. i) Explain indirect addressing, external data moves and code memory read only data moves with
instructions.
i) Explain relative range, short absolute range and long absolute range with necessary
instructions. GTU : Winter-11, Marks 7

3. Explain register direct and register indirect addressing mode of 8051 with an example.
GTU: Summer-13, Marks 3
4. List the addressing modes of 8051 and explain register indirect and indexed addressing mode in
detail with assembly language example. GTU : Summer-13, Marks 7
5. What are the addresing modes for 8051?Explain in brief giving suitable example.
GTU : Summer-16, Marks 7
6. Explain addressing modes of 8051 with example. GTU : Winter-16, Marks 4
7. Explain the direct and register indirect addressing modes of 8051 with suitable example.
GTU : Summer-17, Marks 4

8. Write addressing modes of 8051 microcontroller with one instruction of each addressing mode.
GTU : Winter-17, Marks 3

9. Define addressing mode. List different addressing modes of 8051 with example.
GTU: Winter-18, Marks 4
10. Explain various addressing modes of 8051 instructions with suitable example.
GTU : Winter-19, Marks 7

7.5 Classification of Instruction Set of 8051


An instruction is a single operation of a processor defined by an instruction set
architecture. According to type of operations, the instruction set of 8051 is classified as,
Data Transfer Instructions

Byte Level Logical Instructions


Bit Level Logical Instructions
Arithmetic Instructions
Jump and CALL Instructions
Review Questions

1. List the diferent types of 8051 instructions.


instruction set.
******.
Z. Give the classification of 8051 ******

TECHNICAL PUBLICATIONS- An up thrust for knowledge


Microprocessors and Microcontrollers
Dyramming
GTU: Summer-11,12, 16,17, 19
7.6 Data Transfer Instructions
An immnediate, direct, register and indirect addressing modes are used
Winter-13,16,19
different
MOVE instructions. Table 7.6.1 lists all types of data moving (data transfer) instructions.

8-bit/16-bit Bytes : 12/3 Cycles : 12


MOV <dest>, <src> : Move
indicated by 'src-byte' into the 'dest-bvte'
Copy the byte variableaffected.
Description : location. Flags are not
of selected register bank to A
Copy the contents of register Rn
MOVA, Rn
MOV A, RO: This instruction copies the contents of the register RO f
Example : accumulator.
selected register bank to the
specified with instruction to A.
Copy the contents of address
MOV A, irect
MOV A, 30H : This instruction copies the contents of memory location
Example whose address is 30H to the
accumulator.
Ri to A.
MOV A, QRi Copy the contents of the address in
contents of memory location
MOV A, @R1 : This instruction copies the from selected register
Example : whose address is specified in the register Ri
bank.
A.
MOV A, #data Load data given in theinstruction to
given within instruction
Example : MOV A, #30H : This instruction copies data
(30H) into the accum1dator.
register bank.
MOV Rn, A Copy the contents of A to register Rn of selected
contents of accumulator in R2
Example: MOV R2, A: Thisinstruction copies the
register of selected register bank.
of selected register bank.
MOV Rn, direct Copy the contents of address to register Rn
at memory addresS
Example : MOV R1, 40H : This instruction copies the contents
40H into the R1 register of the selected register bank.
.

selected register
MOV Rn, #data Load data given in the instruction to register Rn of
bank.
Example :
MOV R2, #20H : This instruction loads 20H in the register R2 of
selected register bank.
MOV direct, A Copy the contents A to the address specified within instruction.
accumulatorto
Example : MOV 20H, A: This instruction copies the contents of
the direct memory address specified in the instruction (20H)
the address
......

MOV direct, Rn Copy the contents of register Rn of selected bank register


specified within instruction. R2 of
MOV 30H, R2 : This instruction copies the contents of register
Example : specified inthe
selected register bank to the direct memory address
instruction (30H).

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Programming
is bank address
address instruction location in
the to selected
selected
the
selected address
specified
by (DPL)the address
register accumulator bank. into
to memory selectedmemory within into
memory The A
to
Language the memory byte 1234H
instruction whose of of of indicated.
within Ri R1 to (30H) register 8-bit DPTR
selectedspecified R2 third 34H. memory.
of of instruction.
of register instruction
contents
of register
location 30H. register the value the in
Assembly contents Ri contents given of instruction selected the
hold A. address know
within register
of address is
contents into constant
the to from
20H.
address
is by by barnk. the while Will data Ri
memory R3 dataaddress within
by 3OH data
within given givern the specifiedto loads
8051 specified
the
by theregister copies the
copies
the
register
of
instruction byte,
16-bit
DPL
Memory
in
external
address
16-bit for
copies copies the R2
loads copies
given specifiedwithin instruction
and thru
the specified instruction
to is
whoseaddresscopies register the
instruction. by address bank. thehigh-order
instruction selected is
instruction instruction 12H instruction
external up
addressinstruction
to addressinstruction address from
given
whose
location location given with Data access An
40H the instruction within
register
by hold 7.6.1
Table PU
address whose address loaded This
specified
data
is to of is the
7-11 thewithin is the This A
mnemory whose This
specified
byte. will to the This copies
External
Thisaddress of address
This within Ri address
30H:
This selected is :
#1234H instruction
of
of the 112H:
of Thislocationof register #30H: is low-order
(DPH) DPH
contents
contents 40H: contents
OR3: contents contents location is pointer :RO
specified to whose memory
into
the data
given :A instruction
whose the bank.
register whosebank.
register
data address :
DPTR, A,@
bank bank.
registermemory by of @R2, byte Pointer Access
20H, @R1, @R2,
MOV
thespecifiedmemory theRi
20H, 30H,into
instruction. TEC
the location
the
Copy
location the
location register data
the
second the theMOVX A.
to
RO
address register whose MOV
MicrOcontrollers
and
Microprocessors
Copy MOV MOV Load MOV (12H)Copy MOV Copy Load MOV The holds Data Copy
explains This
20H. the
to
Instructions
7.6.2 MOVX
@DPTR
A,
direct
direct,
MOV @Ri
direct,
MOV
#data
direct,
MOV @Ri,
direct
MOV @Ri,
#data
MOV MOVX
A,@Ri
MOV
DPTR, Table
eieweeivccceecccccw
@Ri,
MOV
A Example
:
Example: :Example :Example Example
: :Example Example
: #data16
Example:
7.6.1 The
Microprocessors and Microcontrollers 7-12 ASSEII
nguage Programming
MOVX @Ri, A Copy data from A to the external address in Ri.
Example : MOVX @RI, A: This instruction copies data from A to the 8-bit
address in R1.

MOVX @DPTR, A This instruction copies data from A to the 16-bit address in DPTp
Table 7.6.2

Important Points to Remember in Accessing External Data Memory


All external data moves with external RAM involve the A register.
While accessing external RAM, Rp can address 256 bytes and DPTR can addre.
64 kbytes.
MOVX instruction is used to access external RAM or I/0 addresses.

7.6.2 Instructions to Access External ROMIProgram Memory


The Table 7.6.3 explains the instructions to access external ROM/program memory.
MOVC A, @A + DPTR Copy the contents of the external ROM address formed by adding
A and the DPTR, to A.
MOVC A, 0A + PC Copy the contents of the external ROM address formed by adding
A and the PC, to A.
Table 7.6.3
Important Points to Remember in Accessing External Read Only Memory
When PC is used to access external ROM, it is incremented by 1(to point to the
next instruction) before it is added to A to form the physical address of
ROM.
extermal
All external data moves with external ROM
involve the A register.
" MOVC is used with internal or
external ROM and can address 4 K of internal
code or 64 K of external code.
" The DPTR and the PCare not
changed.
7.6.3 Data Transfer with Stack (PUSH and POP)
Instructions
PUSH direct : Push onto stack
Bytes : 2 Cycles :2
Description : The Stack Pointer is
is then copied into theincremented
by one. The contents of the indicated variable
internal
Otherwise no flags are affected. RAM location addressed by the Stack Ponte.
Example : PUSH B:This instruction increments the
contents of register B to the internal RAM stack pointer by one and storer
location
pointer (SP). addressed by the stack
Microprocessors and Microcontrollers 7- 13 8051 Assembly Language Programming

POP direct : Pop from stack Bytes : 2 Cycles : 2

Description : The contents of the internal RAM location addressed by the Stack Ponter 19
read, and the Stack Pointer is decremented by one. The value read is then
transferred to the directly addressed byte indicated. No flags are affected.
Example : POP ACC: This instruction copies the contents of the internal RAM location
addressed by the stack pointer to the accumulator. Then the stack pointer is
decremented by one.
Important Points to Remember during PUSH and POP
When the SP contents become FFH, for the next PUSH, the SP rolls over to 001.
The top of the internal RAM, i.e. it's end address is 7FH. So next PUSHes after
7FH result in errors.
Generally the SP is set at address above the register banks.
The PUSH and POP operations may be applied to the stack pointer (SP).
When PUSH and POP operations are used for the registers from the register banks
(bank 0 - bank 3), specify direct addresses within the instructions. Do not use
register name from register bank since the register name does not specify the bank
in use.

7.6.4 Data Exchange Instructions


When 8051 executes MOV, PUSH or POP instruction, the 'copy operation' takes place.
The data from the source address is copied to the destination address. The data at the
source address remains unchanged. The Exchange instructions move data fromn source
address to destination address and vice versa. Table 7.6.4 lists all types of exchange
instructions in 8051.

XCH A, Rn Exchange data bytes between register Rn and A.


Example : XCH A, RO: This instruction exchanges contents of accumulator with the
contents of register RO of selected register bank.

XCH A, direct Exchange data bytes between address directly given within instruction and
A.
Example:
XCH A, 20H : This instruction exchanges contents of accumulator with the
contents of memory whose address is given within the instruction (20H)

XCH A, @Ri Exchange data bytes between A and address in Ri.


Example : MOV A., @R2: This instruction exchanges the contents of accumulator with
the contents of memory location whose address is given by the contents of
register R2 of selected register bank.

TECHNICAL PUBLICATIONS-An up thrust for knowledge


Microprocessors and Microcontrollers 7-14 8051 Assembly Language
Programming
XCHD exchanges the low-order nibble of the Accumulator (bits 3-0), with
XCHD A, @Ri the specifed
that of the internal RAM location indirectly addressed by are not affort
register. The high-order nibbles (bits 7-4) of each register
the value 36H
Example: RO contains the address 20H. The Accumulator holds 75H (01110101
(0011011OB), Internal RAM location 20H holds the value
The instruction, XCHD A, @RO Will leave RAM location 20FH holding the
Accumulator.
value 76H (01110110B) and 35H (00110101B) in the

Table 7.6.4

Important Points to Remember in Exchange Instructions


All exchanges involve the A register.
All exchanges take place internally within 8051.
When XCHD A, @Ri instruction is executed, the upper nibble of A and the upper
nibble of the address in Ri do not change.
Immediate addressing mode cannot be used in the exchange instructions.
Review Questions

1. Explain following instructions


1) XCHD A, RO 2) MOV A, @RO GTU : Summer-11, Marks 2
2. What is the diference between MOVC and MOVX instructions ? GTU : Summer-12, Mark 1
3. Explain following 8051 instruction with an example : MOVX. GTU : Winter-13, Mark 1
4. Differentiate between SJMP and LJMP, RET and RETI, MOVX and
MOVC.
GTU: Summer-16. Marks 7
5. Differentiate between MOVX and MOVC instruction. GTU :Winter-16, Marks 3
6. Differentiate between following instructions of 8051.
i) SWAP and XCHG ii) MOVX and MOVC GTU : Summer-17, Marks 3
7. How stacks are accessed in 8051? Explain PUSH and POP
instructions.
GTU : Summer-19, Marks 4
8. Explain POP and PuSH
instructions w.r.t. 8051. GTU: Winter-19, Marks
9. Compare MOVX and MOVC.
GTU:Winter-19, Marks
7.7 Byte Level Logical
Instructions
The instructions ANL, ORL, and XRL perform the logical functions AND, OR,
and/or Exclusive-OR on the two byte variables indicated, leaving the results in the
first. No flags are affected.
Microprocessors and Microcontrollers 7- 15 8051 Assembly Language Programming
" The byte-level logical operations use all four addressing modes for the
data byte. Here, directly addressed bytes may be source of
either the accumulator or aconstant as the used as the destination witr
source. These
clearing (ANL), setting (ORL) or complementing (XRL)instructions are userul ro
one or more bits in a
RAM, output ports, or control registers.
This is illustrated in following
figures.
X X X X X X X X Unknown 8-bit binary number
1 11 1 000 0 Masking pattern
X X X >X 000 Result

Masked bits

Fig. 7.7.1 Masking using AND operation

X X X >X X X X X Unknown 8-bit binary number


+ 1 1 1 1 000 0 Setting pattern
X X X X Result

Set bits

Fig. 7.7.2 Setting bitls using OR operation

XXX X XXX X Unknown 8-bit binary number


0 0 0 0 111 1 Pattern for inverting lower 4-bits

X X X X Result
Inverted bits

operation
Fig. 7.7.3 Inversion of part of a number using XOR
R051 Assembly Language
Microprocessors and Microcontrollers 7-16 Programming
logical operations.
Table 7.7.1 gives the list of byte level
variables Bytes : 1/2/3 Cycles . 1
ANL<dest-byte>, <src-byte>: Logical-ANDfor byte
variahloe
operation between the Aaoe
Description : ANL performs the bitwise logical-AND
in the destination variable. No
indicated and stores the result
affected.
Byte 1 Cyde 1
ANL A, Rn ANDregister to Accumulator
ANL A, R2 : Logically ANDs A
and R2 and store
Example : result in A.
Byte 2 Cycle 1
ANL A, irect AND direct byte to Accumulator
ANL A, 20H : Logically ANDs contents of AÀ and
Example : 20H and stores
memory location whose address is
result in A.
AND indirect RAM to Accumulator
Byte 1 Cycle 1
ANL A, @Ri
Example : ANL A, @R2: Logically ANDs contents of Aand
memory location whose address is given by R2 and
stores result in A.
AND immediate data to Accumulator Byte 2 Cycle 1
ANL A, #data
Exanple : ANL A, #50H : Logically ANDS contents of A with
50H and stores result in A.
AND Accumulator to direct byte Byte 2 Cycle 1
ANL direct, A
Example : ANL 20H, A : Logically ANDs contents of A with the
Contents of menory location 20H and stores result at
memory location 20H.
ANL direct, #data AND immediate data to direct byte Byte 3 Cyce 2
Example : ANL 20H, #20H : Logically ANDs the contents of
memory locatiorn 20H with data 20H and stores result
in memory location 20H.
ORL <dest-byte>, <src-byte> : Logical-OR for byte variables Bytes : 1/2/3 Cycles : 12
Description : ORL performs the bitwise logical-OR operation between the indicated
variables, storing the results in the destination byte. No flags are affected
ORL A, Rn OR register to Accumulator Byte 1 Cycle 1
Example : ORL A, R2 : Logically ORs the contents of A and R2
and stores result in A.
Byte 2 Cycle1
ORL A, direct OR direct byte to Accumulator
Example : ORL A, 20H : Logically ORs the contents of A and
memory location 20H and stores result in A.
OR indirect RAM to Accumulator Byte 1 Cycle1
ORL A, @Ri
Example: ORL A, @R2 : Logically ORs the contents of A and
memory location whose address is given by register
R2 and stores result in A.
Cycle1
ORL A, #data OR immnediate data to Accumulator Byte 2
Example : ORL A, #32H: Logically ORs the contents of Awith
32H and stores result in A.
W*wWwwwwiwvewwwwwiw.ww

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Questions
Review DescriptionDescription
: CPL #data
CLR Exampledirect,
: XRL Exampledirect, A
: XRL Example #data Exampledirect
: XRL A, Example : XRL@Ri A, :XRL A, Rn
A, <dest-byte>,
<src-byte>:
Example :XRLDescription #data Microprocessors
Microcontrollers
direct,
:XRL Exampledirect,
:ORL Example :ORL A and
C. 1.
A A
Explain What : Clear :
is
Complement
the the
Accumulator
operation
instruction wiwweitwwwewwwwww.eweowv.w
a
(one's Each The and XRL Excusive-OR
Exclusive-OR data XRL
and 20H,XRLA:Exclusive-OR Exclusive-OR
and the XRL memory
Exclusive-OR performs
indicated
and XRL affected.
XRL ExcBusive-OR XRL memoryat ORL OR result the ORL OR
LS zero
Accumulator memory
location
201.
of Accumulator data the 40H A, stores stores A, immediate contents Accumulator
set the bit 30H memory A, A, 20H, at 30H,
complement).and Logical
of A #40H @R2 location20H: location memory
ofgiven
40H and and result R2variables,
: #30H A
wwwi8051wwwwwwwwwwwwwwwwwwwwwwwwww
vice-versa. the #40H result of
immediate immediate location : Logically :
and Accumulator
Logically
stores Logically stores : Logically indirectLogicallydirect register Exclusive-OR the Logically
data memory
8051 Accumulator : in in 20H : to
icrocontroller. is Logicaly 20H Logically location
Table
7.7.1 stores byte A. bitwise
storing to direct 7-17
microcontroller Bits cleared the result A. to
No whose RAM and XOR and direct
XORs XORs Accumulator location ORs
which the data resultXORs XORs data to logical data 30FH. byte
flags to in stores
to the
-An (all XORs the for ORs byte
the
is result to thdirect e A toaddress the Accumulator
are
logically
previously bits at the A
A the contents results byte contents 30H theof 30H
contents
up direct result Exclusive-OR
the contents contents contents and
thrust instructions affected. set at 30H. byte contents is variables and
301H.
contents in 8051
on givern in of the stores
A. stores of
for complemented zero). A
contained at of of of destination. A Assembly
ledge : at 20H by A A and with
XRL R2 and with operation Bytes result
No 30}-H with A R2
A, a Bytes flags
Bytes Language
1/2/3 :
direct one Byte 2 Byte No Byte 3 Byte 2
Byte 3 Byte 2 Byte 1 Byte 1between
are :1 are :
? 1 2 flags Cycles
changed toCyclesaffected.Cycles
Programming
Cycle 1 Cycle 1 Cycle i Cycle Cycle 1 are the Cycle 2 Cycde 1
Cycie 2 1/2 :
:1 1
1:
decrement
increment
and amultiplication, Microprocessors
Microcontrollers
and
DEC A DPTR
INC INC INC A 7.8.1 Instructions
Arithmetic 7.8
Example :DEC@Ri Exampledirect
:DECExample :DEC RnDescription :Example
Example : INC@Ri Example direct : INCRnDescription given
: INC Incrementing
Decrement
<byte>:
: DEC The
<byte>
number.
Incrementing arithmetic

Increment :
division
Decrement
whose indirect
DEC RAM whose
Decrement
DECdirect register
Accunulator
DECDecrenent
byteDecrement underflow The indirect
INC RAMaddress
IncrementaddressIncremernt
is Increment
INC direct overflow INC
INC Increnent These and
byte ncrement
register operations
variable @R2 20H: R2 increments decrementing
L @R2: 20H R3: instructions and and
address is address is : instructions.
Decrenments : Incremernts to
Decrements Decrements : to a given
Datincremnent Increments
given Accumulator 00H. decimal
indicated
OFFH. Decrementing of
is Pointer 8051
given 20H by within Nothe
No register indicated
flags instructions
not operations.
by contents bycontents contents contents include
Table
7.8.1 bythe the flags is the by 7-18
1. decremented affect
register contents 1 1 are
contents are
R2instruction
of variable
affected. increment,
of by of of
- affected. memory R2 C,
allow
An R R3 1.
memory
of by AC GTU
up by of by by by
memory (20H) 1
st memory 1 1. 1. addition
and :
1. location location
An by An decrement,Winter-10, 8051
for OV
location location original Bytes1/2 : 1. original Bytes1/2 :
e whose Assembly
whose flags. and
13,
value value subtraction
Table addition, 16, Language
Byte 1 Byte 2 Byte 1Byte of Byte 1 Byte Byte 2 Byte Byte 1 of Summer-11,
1
00H Cycles: 1 0FFH Cycles12
: 7.8.1
1 subtracthon, Programming
will wil
Cycle 1 Cycle I Cycle 1Cyce Cyce 2 Cyck Cycle I Cyce 1 liste of
1 ,
1 1 f 121
MicroproceSsors
Microcontrollers
Addition
7.8.2 and
ExampleADDC
#data
: A, ExampleADDC ExampleADDC
: A,
:@Ri A, ExampleADDC Rn
A, <src-byte>
: Description ADDC A, #data
: Example: ADD A, Example :ADD@Ri A, directADD A, Description : ADD Table
Example : Example: ADD

A, A,
shows
direct Rn <src-byte>

the
e
thWhen
Add Accumulator. isAdds list
ADDC
and 0ocation
carry ADDC Add stores
Add location ADDC20H A,AddADDCA, the
flag, Add cleared ADDC ADD Add whoseAADD AddaddressADD Add ADD operands.
a
sumcarry-out of
20H Accumulator, immediate A, direct register the Add :
immediate flag indirect result direct and register Add : A,datatoaddressindirect A, A adding addition
A, whose otherwise.
simultaneously W20H: is R2
of byte
and A, whose stores @R2 2011 20H
and @R2 with byte two
#20: in byte R2 RAM Adds to signed The
stores RAM to is :
Adds : fromvariable
stores resut Adds given andAdds Accumulator
to positive
data addressAdds : address A. : to resultAdds
Accumulatorin
A. carry instructions
Adds Adds respectively, Carry Accumulator
store
to bit
to A contents contents
result toresult with th e the Accumulator
by the integers, 7
indicated
A the adds Accumulator operands, orand 7-
19
the A is the is register result
contents conternts bit
in with conternts given with 20F1 contents auxiliary-carry
contents in contentscarry bytethe of
A, the carry in of A OV3,
and with if and tosupported
carry by flag there R2 A. A and indicates
A. of of of or the
register of flag the carry and and cleared,
a
offlag of A, variable A A
A, A, is and memory
R2 positiveAccumulator,
A carry R2 flag and stores and
memory memory a 8051
and and carry-out fromn 20H memory a flags by
R2 flag store negative
otherwise.
indicated,the result sum 8051. Assembly
carry
flag and carry whose are
and result
the Bytes1/2 : in from set, Bytes1/2 :
leaving
number
respectively, Language
bit twO
Byte 2 Byte 1 Byte 2 Byte 1 carry7 Byte 2 Byte 1 Byte 2 Byte 1 the
negative
produced
or result
and
bit flag Cycles :1 Programming
Cycles: 1 if
Cycle 1 Cycle 1 Cycle 1 3, Cycie 1 Cycle 1 Cyce 1 Cycle 1 in
Cycle 1 and there
as the
Example : Description : MUL 7.8.4 A,#data
Example SUBB
: SUBB
ExampleA,@R0
: A,direct
SUBB A,Precaution : MicroprOcessors
MicroControllers
7.8.3 and
Example : Example SUBB
: Rn Description : SUBB Subtraction
A,Table
AB
Multiplication shows
Multiply : <src-byte>
TheOriginally
(3200), value Accumulator, the
A. instruction.
cleared.(FFH)register MUL Subtract
SUBB
A.
in together Subtract location
locationSUBB inSUBB SUBB
Subtracttogether A,
Subtract
multiple-precision is if SUBB
(borrow)
IfAccumulator,
overflow the needed a list
160 the AB borrow :
so B. and A, state subtracts
Subtract of
(0A0H). the overflow multiplies The immediate whose indirect
#20H: A, from @R2
20H direct
20H
A, from register
R3: subtraction
B into flag
flag
Accumulator is and Division of is
changed low-order A : andcarry : Subtracts A the needed
bit if
leaving
is The Subtracts address
Subtracts Subtracts byte a the with
flag the the and RAM and from carry 6,
borrow
set, data subtraction,
instruction, high-order stores from stores but indicated instructions
carry to A for the Borrow
is byte
set; unsigned is from together
contents with is not 7-20
321H holds 20FH from the the A not is
bit result
result given results into needed
is otherwise of from A contents A conternts with borroW known 3,variable
(00110010B)
cleared. the with it and in
MUL the byteeight-bit with from borrOW of should bit
in by in R3 the
value sixteen-bit A R2borrow cleared 7, for supported
in and
borrow A. of A. before and
AB it B. and of A carry
and Accunulator.
or
memory stores
memory be into bit
is integers stores the 8051
and will 80cleared. the If carry explicitly
startingotherwise. and 7,
(50H), product Bytes 1: result bit carry
the give product result 7,clears
Bytes1/2 : by Assembly
in 8051.
AccumulatorRegister the The the clearedsingle a but OVSUBB flag
is not
otherwise. together Language
product carryAccumulatorisleft is
greater Byte 2 Byte 1 Byte 2 Byte 1 sets
B in byor bit set
holds flag the Cycles :4 a 6. if the
is CLR ACis
a carryfromCycles: 1 Programming
othe tnais borrow
cleared. always and Cycle 1 Cycie 1 CycBe 1 Cycle 1 C the
sot
s
Questions
Review Example : Decimal
DA7.8.5
Description : Arithmetic Example : Microprocessors
Microcontrolersand
Description :Divide
AB:DIV
*. 3. k. 1.
B 1)
ain What ADDA,txplain 2) 1) :A
Differentiate
program.
Liduring
st the Decimal-adjust
recaution the
ing following
JUMP ThusSince Sincecleared. will ADDC DA
AR3 A
nibble.
A,accumulator
upperthe packed-BCD If Adjusts
addition
the result. 0001001OB).
00001101B) The
Accumulator
The Exception OV
unsigned
part DIV
(10111110) the
Carry
=
ADDC first result Accumulator carry flag is flags of AB
8051 weDIV AB
2) e lower
thupper 55H lower arnd
instructions and the divides
should final perform B by ofthe
Accumulator OV will
eight-bit
CALL and in = two and The quotiernt;
If:
tion nibblenibble the 68IH
isnibble eight-bit
adding and B be
take SUBB result greater will the
instruction, had the
instruction. Accumulator. a variables contairnscleared
B-register cleared. integer
before > and of bothvalue
= > 9,standard 06 the value originally registerunsigned
with MUL AB
3) instruction. 124, Lower 9, CF than in for any in 7-21
Upper accumulator the (each in
be16
250 in
anusing which 1. cleared. will B
register
Describe or9 thaddition
e V
(10H DI(0FBH
ample SUBB binary Then lower in contained
case. be
receiveseight-bit
List nibblenibble The CF
Accumulator orAB
is packed-BCD
n the carry instrucion is 00010000B) will undefined
or B.
:
instruction conditional valid = addition, nibble. is
set, the integer
The
p DIV. BEH EH greater 11111010B) 00H,
precaution and it leave integer
BCD + Accumulator 8051
+6H 6M resulting corrects the If
resulting and the in
? JUMP sum. auxiliary sequence thanformat), in 13 Assembly
Summer-l1,
GTU : to + = upper B,inand values
theremainder.
the Bytes :1
GTU
GTU: be AF(1) 4H the 9
GTU instruction. or
since the overflow Accumulator
taken with carry in from Bytes B returned receives
: the resultnibble AF to
produce Accumulator
250contains Language
: Winter-10,
ummer-12,
ter-13, for CF=1 AF=1
with2H flagsvalue the The
by ofset,is :1 = flag Cycles :4
both earlier (13 in carry the by
will BEH adding the 18
it Cycles x18)(ODH (12H or wiset.l bethe integer the Programming
Marks Marks
3 instructions be corrects and
Mark
Mark 06 +
in :1 16. or
1 1 7
e
thshows arebit interpolate
26H bit From internalaraddresses
e addresses.
Like Byteaddresses
particular
through 7.9 Microprocessors
Microcontrollersand
corresponding
addresses.bit 6 As
as of 00H necessary
Bit 6. 5.
addressable.
theTable
The 31H Table shown Address
internal internal level Bit ExplainExplain
bit RAM 26 25 24 23 20 2FH
of are bit
addressable
andaddresses 7.9.1 and
in
Level
byte
bit in indifferent. is
manipulations DA MUL
RAM, 36H, RAM 07H the
0 the Hex both A
we Table Logical
and internal instruction AB
SL respectively. of address
byterespectively. Table
some can Bit The byte instruction
SFR bit bit
address 7.9.1
1 Address Table RAM are
easily 20H 7.9.1,
7 addressableInstructions of
and 7.9.2 SFRs and Bit 38-3F 30-37 28-2F 20-27 18-1F 10-17 08-0F 00-07 8051 of
of
and very a
or 8051.
in 7.9.1 with
byte Hex SFRs.
convenient
Table shows example. 7-22
SCON TCON addresses and
PSW P3 P2 P1 SFR The
PO IP IE A Byte
- 7.9.2 the bit
An internal
up Address when
t Bit of 2F 2E 2D 2C 2B 2A 29 28 *******
addressable.
correspondence
Address
Direct
and internal
Hex in RAM it GTU 8051
or in reset or
is
byte 98 88 DO BO A0 90 80 B8 A8 FO E0
Hex : Language
Assembly
Winter-13, GTU: Programming
of
RAM GTU
dresses However, 8051
Bit between Winter-16, :
Winter-16,
AddressBitin Address from set to
19,
98-9F 88-8F DO-DZ AOA7
BO
B 90-97 80-87 F
B8-BA&AF FO-F7 E0
B7Hex 78-7F 70-77 68-6F 60-67 58-5F 50-57 48-4F 40-47
of e
byte bytaddrees Summer-17
SFRs in Marks Marks
Hev and and
k hi 3 2
MicroprOcessors
Microcontrollersand
ExampleExample :
:ORLbit C, :ORLbitC, Description Example: Example
ANLbit C, :ANLbitC, Description : CPL CPL
Description : ANL Example: Example
Description
: :SETB SETBExample : CLR CLR Table
ORL Description :
C bit
C, C, bit: bit C:7.9.3
C: :
Complement : : Set Clear Clear
<src-bit>: <src-bit>: Complement Set
gives
direct Carry directCarry
the
Logical-OR ******** P1.2
CLR Port The
CPL
Port The (00110101B).
35Hleave P1.0SETB Cdirectly
(0011010OB).
will SETB The anySETB bit flag
ORL
ORL COR OR
other bit
current Set
assembly Onlysource
preceding
ANL AND ANL ANDcomplement the flag; If
Logical-AND ******************.********.*******.*
bit flag list
CAL the directCarry ****.****.*
is the P1.1 carry indicated
1 of
direct C direct Boolean 1bit sets has
complement C, flags used Complement C, direct biotherwise
t has
variable
JOV carry
state 1OV ACC.7 leave
will
the bit
itself will bit flag carrythe is the previously
ACC.7: bit language
are as addressing
previously addressable
cleared.
instruction, level
NS- fo r bit of ****p
The ndicated bit
to otherwise
the flag : operand leave ****
:Or affected. And : to isthe leavevalue for specified ********
bit And is
of Carry source if not **2*
flagsetto cleared operations. 7-
23
direct Or indicates variables the with of Carry addressed the of
bit the Output
Carry direct Carry affected. written
been beern
Carry tlag is the
invariables port bit. bit port
slash
value, Boolean inverse flag
allowed carry
the is
********** to
bit source complemented. Port No
written(reset
set
with with bit with set one.
to that("/") bit
assembly
No flag and 1
An e but to to other to
up
thCarry
the the value of
Accumulator to
1 SETB FBH with
Carry for flags is inbit
used FDH with has
thrust inverseACC.bit7 the
preceding Overtlow changethe zero).
logical is the its is 8051
source a
language
indicates
are current a been flags can (11111011B) FFHH
for logical source as logical(11111101B) FFH No
of affected. the No written are
operate Assembly
ledge OV complement
bit the flag (11111111B). (11111111B).other
t source other output
dataon affected.
bioperand. state. 0
itself
operand 1;Bytes 7 then Bytes Bytes Bytes
leave flags
Bytes Bytes with on flagsBytes Bytes Language
is value, A the
Byte 2 Byte 2 not of the :2 Byte 2 Byte 2 slashclear :2 the
in logical
thatthe The are :2 1: carry
: :1 The are :1 :1
the but the vaBue
2 affected.
e Cycles
affected.No thcarry affected.
("/") Cyclesinstruction Cycles instruction,
addressed Cycles:1
Cycles:1 Port to1 Cycles:1 CycleCycles
s Programming
Cycle 2 Cycle 2 Cyce 2 Cycde 2 the carry iag
in 34H
Or
its :2 :2 :1 :1 :1
MicroprOcessors
Microcontrollers
and
7.10 Questions
Review MOVbit, C MOVbitC,Description :
Example:
RL Example : MOV
Example : Description : RLC Example Description : The 1
A 6. 5. 4. 3. 2.
A Table Explain WhatExplain What What Name <dest-bit>,
: Rotate
: Rotate
Rotate are are are any
7.10.1 any following
the the the four
Accumulator two
and advantages
A differentvarious <src-bit>
gives bit
bit MOV
P1.3,C: variable
Boolean
location
registercarry The
(10001010B) The The
NICAL
IONs
Left leaves The
carry one The unaffected. rotated The Swap level 8051 manipulation Move MOV Move
the operations
operations
Accumulator
instruction, bithrough
t bits Accumulator eight instruction direct flag;
tlag eight the instruction of Carry C,
Carry
flag to into list bit P3.3 or
specified
Left
bits Instructions the Move :
moves the Accumulator wise performed flag bit flag
with the of performedinstructions :
left. the in the 7 in rotate with Copies Copies to is
other
bit the programming
with
the RLCholds into Carry holds Table
7.9.3
to Carry affected. by bit 7-24
7 Bit 6 0 an direct may the
indicated data
Accumulator
position. example. by the the
carry A Accumulator
the 7 and example by
leaves the holding the boolean in flag first
be
6 moves
bit flag 5 boolean status bit status
value swap microcontroller any
-
set. value in operand.
0 www..ww theby
An 5
position. 4
No 8051 ANL : variable
the into the variable of of directly
up C5H flags areoperations GTU P3.3
Accumulator C5H andthe carry
thrust 4 the value ? C, second
3rotated One 8051
(11000101B), carry (11000101B). are /Bit. intoaddressable
No Summer-17, : instructions 8051.
instructions into
for 3 8BH 2
affected. of Programmirng
Language
Assembly
other carry GTU P1.3. carry. operand
the is
ledge flag; **.e**.*****..***.................
one supported GTU
2 (10001011B) GTU: Bytes
holding bit : operands
flags the flag Bytes Summer-17, bit.
and Bytes The to Winter-19, of8051 of
1 are 18, Winter-13, 8051 Byte 2 Byte :2
are
original instruction the No copied
intohe
the the together by
affected.
: with
1 left. 1: Winter-11, ? ? 2 othermust Cycles
valuecarry 8051.
state Cycles BiCycles
t is7 Cycle 2 Cycle be
the Marks Marks Mark
o rotated tha :1/2
of carry RL 16 4 i
the :1 A 4 1
1:
Microprocessors and Microcontrollers 7-25 8051 Assembly Language Programming

RR A: Rotate Accumulator Right Bytes : 1 Cycles : 1


Description : The eight bits in the Accumulator are rotated one bit to the right. Bit Ois
rotated into the bit 7 position. No flags are affected.

7 5 4 3 2 1

Example : The Accumulator holds the value C5H (11000101B). The instruction, RR
A leaves the Accumulator holding the value E2H (11100010B) with the
carry unaffected.
RRC A : Rotate A Right through Carry flag Bytes : 1 Cycles:1
Description : The eight bits in the Accumuiator and the carry flag are together rotated
one bit to the right. Bit 0 moves into the carry lag; the original value of
the carry flag moves into the bit 7 position. No other flags are affected.

7 54 2 0 C

-EE-O Carry
flag

Example : The Accumulator holds the value CSH (11000101B), the carry is zero. The
instruction RRC Aleaves the Accumulator holding the value 62
(01100010B) with the carry set.
SWAP A : Swap nibbles within the Accumulator Bytes : 1 Cycles : 1
Swap A interchanges the low and high-order nibbles (four-bit fields) of
Description : the Accumulator (bits 3-0 and bits 7-4). The operation can also be thought
of as a four-bit rotate instruction. No flags are affected.

4 3
Higher nitbble| Lower nibble

The Accumulator holds the value C5H (11000101B). The instruction


Example : sWAP A leaves the Accumnulator holding the value 5CH (01011100B)
.ww.w.*wiww.

Table 7.10.1

Review Questions
GTU : Winter-11, Marks 2
Explain following instructions : 1) SWAP A 2) RL A.
1. GTU : Winter-16, Marks 2
of 8051.
2. Explain SWAPAinstruction

TECHNICAL PUBLICATIONS - An up thrust for khowledge


Jumpcausesofaddress specifying
Forcontentsis counter 7.11.1 equal.
magnitude ForThere contentscall 7.11 MicroproceSsors
MicrOControllers and

Absolute The 4. 3.
Long Relative arndexample, subroutine.
Return CallDecrement
We unconditionally. Jump
a Compare Jump bion t example, temporarily Jump
the know
and following are ExplainDifferentiatei)
CALL
from or of Jump SWAP
range program the with Jump of jump and
by from program
range(short) address he byte the CJNE the
iftdifference
specifying athat byte
new and a conditions. types instructions changes CALL and instructions and
0000H : instructions jump a subroutine. and first XCHGbetwveen
:
0000H range counter a and
program
address.The jump Call (compare instructions counter. CALL
to jump of tw o following
:+ instruction the in between
the
jump instructions the
FFFFH Program DÊNZ i)
to may to operands, which program MOVX
07FFH 127 become entire if not if and
Instructions
A
counter call zero. jump and
instructions
tohave program
- is new the equal. jumpchange change SWAP and
instructions Range change flow MOVC
128 one located and permanently
0230H, new 7-26
(+7FH address. changes if the to the wi th of
of address GTU: 8051.
the not reference
the then at new programallow flow
address
0200H,
tothreeprogram program equal).
- the called The addressreplace program another changes Winter-10,
80H) and of to
ranges.rangethe differernce, flow the 8051. 8051
the the flow This
the
specifiedcanbe flow if
part the 13,18,19, GTU GTU: Assembly
range current instruction program
contents : certain program
if of Summer-18, :
Summer-17,
in their the Languago
jump of
bytes, Summer-16,18.19
the progrâm condition program by
of values
the compares flow
changing
isand jump or Prograrnming
30H the Marks Marke
Oru ceitherprogram are exists whereas to
bytes. jump cal . not the run. the 4 3
by
Microprocessors and Microcontrollers 7-27 8051 Assembly Language Programming

7.11.2 Jump
The Table 7.11.1 shows the list of jump instructions supported by 8051.
AIMP addr11 : Absolute Jump Bytes : 2 Cycles : 2
Description : AJMP transfers program execution to the indicated address. Since address is
11-bit the destination must therefore be within the same 2 K block of program
memory as the first byte of the instruction following AJMP. No flags are
affected.

LJMP addri6 : Long Jump Bytes : 3 Cycles :2


Deseription : LJMP causes an unconditional branch to the indicated address by loading the
high-order and low-order bytes of the PC(respectively) with the second and
third instruction bytes. The destination may therefore be anywhere in the full
64 K program memory address space. No flags are affected.
sJMP rel: Short Jump (relative addr) Bytes : 2 Cydes :2
Description : Program control branches unconditionally to the address indicated. The branch
destination is computed by adding the signed displacement in the second
instruction byte to the PC, after incrementing the PC twice. Therefore, the range
of destinations allowed is from 128 bytes preceding this instruction to 127 bytes
following it.
JMP @ADPTR : Jump indirect relative to the DPTR Bytes : 1 Cycles :2
Description : Add the eight-bit unsigned contents of the Accumulator with the sixteen-bit
data pointer, and load the resulting sum to the program counter. This will be
the address for subsequent instruction fetches.
Neither the accumulator nor the data pointer is altered. No flags are affected..
JZ rel : Jump if Accumulator is zero Bytes :2 Cycles : 2
If all bits of the Accumulator are zero, branch to the address indicated;
Description :
otherwise proceed with the next instruction. The accumulator is not modified.
No flags are affected.
JNZ rel: Jump if Accumulator is not zero Bytes : 2 Cycles : 2
indicated address:
Description : If any bit of the Accumulator is a one, branch to the modified.
otherwise proceed with the next instruction. The accumulator is not
No flags are affected.
Bytes : 2 Cycles: 2
JC rel :Jump if Carry flag is set
Description : If the carry flag is set branch to the address indicated; otherwise proceed with
next instruction. No flags are affected.
the
Bytes : 2 Cycles :2
JNC rel: Jump if no Carry flag
Description : the address indicated; otherwise proceed
If the carry flag is a zero, branch to flag
with the next instruction. The carry i is not modified.
wwwwwwwwswwwwsw
Microprocessors and MIcrocóhtOners
amming
JB bit, rel : Jump if direct Bit set
Bytes : 3 Cycles:2
indicated
the the jump to the address indicated; otherwise
bit is one, The bit tested is not modified. No flags areproceed
Description : Ifwith next instruction. affected.

JNB bit, rel : Jump if direct Bit not set Bytes : 3 Cycles:2
Ifthe indicated bit is a zero, branch to the
indicated address; otherwise
Description: proceed
affected
with the next instruction. The bit tested is not moditied. No flags are
*'**********... *************

JBC bit, rel : Jump if direct Bit is set and Clear


bit Bytes : 3 Cycles :2
If the indicated bit is one, branch to the address indicated; otherwise proceed
Description:
with the next instruction. The bit will not be cleared if it is already a zero
....... .

CJNE <dest-byte>,<scr-byte>, rel : Comnpare and Jump if Not Equal


Bytes : 3 Cycles: 2
Description : CNE compares the magnitudes of the first two operands, and branches if their
values are not equal. The carry flag is set if the unsigned integer value of
<dest-byte> isless than the unsigned integer value of <sre-byte>; otherwise, the
carry is cleared. Neither operand is affected.

DJNZ <byte>, <rel-addr> : Decrement and Jump if Not Zero


Bytes : 2/3 Cycles :2
Description: DJNZ decrements the location indicated by 1, and branches to the address
indicated by the second operand if the resulting value is not zero. An original
value of 00H will underflow to OFFH. No flags are afected.
NOP Function :No Operation
Bytes : 1 Cycles :1
Description : Execution continues at the following instruction. Other than the PC, no registerS
or flags are affected.
Table 7.11.1

7.11.3 CALL and Subroutines


There are two subroutine-call instructions. LCALL (Long Call) and ACALL
(Absolute Call). Each increments the PC to the first byte of the following instruction,
then pushes it onto the stack (low byte first). Saving both bytes increment the stack
pointer by two. The subroutine's starting address is encoded in the same ways as LJMP
and AJMP. The generic form of the call operation is the mnemonic CALL, which 8051
will translate into LCALL or
ACALL as appropriate.
The return instruction RET pops the high and low-order bytes of the program
counter successively from the stack, decrementing the stack pointer by two. Program
execution continues at the address previously pushed: The first byte of the instruction
immediately following the call.

TECHNICAL PUBLICATIC
Microprocessors and Microcontrollers

Explain the instructions DJNZ and SWAP with reference to 8051.


Programming
5. GTU : Summer-18, Winter-18, Marks 4
8051.
Differentiate between RET and RETI, SJMP and LJMP instruction in
6. GTU : Summer-18, Marks 3
DJNZ and JBC in 8051. GTU : Summer-19, Marks A
7. Explain the instruction
different from RET instruction ?
8. What is RETI instruction? How it is GTU : Summer-19, Marks 3
9. What is jump range ? Explain short absolute and long absolute jump ustng appropriate
GTU : Winter-19, Marks 3
instruction.

7.12 Instruction Timings GTU: Winter-06


In 8051, the internal operations and external read/write operations are controlled by
termns : Instruction vla
the oscillator clock input signal. Let us define the three
machine-cycle and T-cycle used in the instruction timings.

Instruction cycle : The time required for fetching, decoding and execution for an
instruction is called instruction cycle.
Machine cycle : Each instruction cycle consists of one to four read or write operatins
between CPU and memory or input/output ports. Each memory or I/0 operation
requires a particular time period, called a machine cycle.
In 8051, the machine cycle consists of a sequence of 6 states numbered S1 to S
(12 oscillator periods). Each state is divided into two phases : Phase 1 and Phase
During Phase 1, Phase 1 clock is active, and a Phase 2 clock is active during Phase
Thus, amachine cycle consists of 12 oscillator periods.
T-state : The time for one clock cycle, ie. one oscillator period is known as Tstate.
Example 7.12.1 For 8051 microcontroller operated with 12 MHz crystal Oscillator, find the
execution time for following instructions :
1 MOV A, # 52H 2. ADD A, 50H
3. MOV DPTR, # 2000H 4. DIV AB
1 1
Solution: The clock period
Crystal frequency 12x106
= 0.0833 us
.. Time for one machine cycle = 0.0833 x 12 = 1
us
1. MOV A, # 52H 1Machine cycle Execution time:1 us
2. ADD A, 50H 1Machine cycle Execution time:1us

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Microprocessors and Microcontrollers 7-31 8051 Assembly Language Programming

3. MOV DPTR, # 2000H 2 Machine cycles Execution time : 2 us


4 DIV AB Execution time :4 us
4 Machine cycles

Fxample 7.12.2 Calculate time required to PuSH DPTR, B, A, PSW and PC on to the stack.
Assume f= 11.0592 MHz. GTU : Winter-06

Solution : Execution time for any instruction can be found out by


Cx12d
T=
Crystal frequency
Now we calculate time required for each.
1) PUSH DPTR
PUSH DPTR required two 8-bit register to store in stack pointer.
PUSH DPH, PUSH DPL

i.e. push data pointer higher and push data pointer lower consequently.
so each push require 2 cycles.
4x12
PUSH DPTR = 4 cycles = 0.3616 us
11.0592

PUSH B =2 cycles 2x12


8-bit register PUSH A =2 cycles = 0.1808 us
11.0592
PUSH PSW = 2 cycles
of PCL and PCH.
PC 1.e. program counter. It consist
PUSH PC= PUSH PCL = 2 cycles
PUSH PCH = 2cycles
4x12
so PUSH PC = 4 cycles = = 0.36168 us
11.0592

Review Questions

and instruction cycle.


1. Define T-state, machine cycle
frequencu of
2. Define machine cycle and calculate time per M/C, as$uMing standard operating
11.0592 MHz.
3. For 8051 microcontroller operated with 12 MHZ crystal osc1llator, find the execution time for
BACK 2. MUL AB
jollowing instructions : 1. DJNZ RI,
8051 Assembly Language
Microprocessors and Microcontrollers 7-32
Programming
GTU : Summer-12, Winter-18
7.13 Time Delay for 8051
Many times, it is necessary to generate time delays. For example, to generate a square
generate a delay of T/2 period so that we can make output 1 for T
wave we have to see how to calculate exact delave Eo
period and make output 0 for T/2 period. Let us
8051.
(1/12) of the crystal frequency. Therefore,
For 8051 operating frequency is one-twelfth
periods.
one machine cycle lasts for 12 oscillator
12
Machine cycle period =
Crystal frequency
For crystal frequerncy 11.0592 MHz,
12
Machine cycle period = 11.0592x 106 = 1.085 us

For 8051, we know that how much machine cycle/s are required to execute the
execution of that
particular instruction. Therefore, we can calculate the exact time for
instruction, as shown below.

Instruction Machine cycle Time to execute

1 1x1.085 11s = 1.085 us


MOV R2, *40
DJNZ R1, SKIP 2 2 x 1.085 us = 2.17 us
MUL AB 4 4 x 1.085 us =434 us

Example 7.13.1 Calculate the time delay produced by the following subroutine.
Delay : MOV R1, #30
HERE : DJNZ R1, HERE
NOP

NOP
RET

Solution: Let us assume the crystal frequency of 8051 is 11.0592 MHz. Therefore,
period of the machine cycle willbe
12
T= = 1.085 usec
11.0592>x106
Instruction
Machine cycle
Delay MOVR1, #30 1
HERE : DJNZ R1, HERE 2
MicroprocesSors and Microcontrolers 7-33 8051 Assembly Language Programming
NOP 1
NOP 1
RET 1

Time delay = [1 + (2 x30) + 1 + 1+ 1]× 1.085 usec = 69.44 us

Here, (2 x 30) indicates that the instruction DJNZ R1, HERE is executed 30 times.
Example 7.13.2 Find the time delay generated by the folloving routine if the
XTAL = 22 MHz. GTU : Summer-12, Marks 3
HERE :MOV RO,#200
AGAIN:DJNZ RO,AGAN
RET

Solution : For crystal frequency of 22 MHz, one machine cycle will be 0.546 us
The AGAIN loop takes (2x 200x 0.546 us) = 218.4 us
MOV RO,#200 takes (1x 0.546 us) = 0.546 us
RET takes (2x 0.546 us) = 1.092 us
:. Routine generates total delay of 220.038 us.
Example 7.13.3 For a 8051 system of 11.0592 MHz, Find how long it takes to execute
followoing instructions : DEC R3, LJMP, MUL AB. GTU : Winter-18, Marks 3

Solution:
12
T= = 1.085 us
11.0592x106
DEC R3 requires 1cycle and hence it takes 1.085 us
LJMP requires 2 cycles and hence it takes 2.17 us
MUL AB requires 4 cycles and hence it takes 4.34 us
7.14 Programming Examples
GTU : Winter-09,11, 12,13,15,17, 18,19, Summer-12, 13, 17, 18

Program 7.14.1 : Program toto load accumulator A, DPH and DPL with 30H
MOV A, #30H : Loads 301H in A register
MOV DPH, A : (DPH)¬- (A)
MOV DPL, A :(DPL) -(A)
Program 7.14.2 : Copy byte in SCON to register R3.

Method1: Using direct address for SCON (98H)


MOVR3, 98H ; Copy SCON to R3
TECHNICAL PUBLICATIONS -An up thrust for knowledge
Microprocessors and Microcontrollers 7-34 8051 Assembly Language

Method 2: Using direct address for SCON (98H) and R3 (03H)


Programming
MOV03H, 98H ;Copy SCON to R3
Method3: Using indirect address for R3
MOVR1, # 03H Initialize pointer to R3
MOV@ R1, 98H ;Copy SCON to R3
Method 4: Using PUSH instruction
MOV81H, #02H :Set the SP to address 02H in RAM
PUSH 98H ;Push SCON (98H) to address (03H)
Program 7.14.3 Put the number 9OH in R2 and R3
***8****************** *****8*E8

Method 1: Use immediate addressing mode


MOV R2, #90H
MOV R3, #90H
Method 2: Use immediate and register addressing
MOV R2, #90H
MOV R3, R2

Program 7.14.4: Add two 8-bit numbers.


MOV A, #30H :(A)- 30
ADD A, #50H : (A)- (A) + 50H
Programn 7.44.5 Add two 16-bit numbers.
MOV DPTR, #2040H ;(DPTR) 2040H(16-bit number)
MOV A, #2BH ;(A) - 2BH (l0wer byte of second 16-bit
MOV B, #20H number)
:(B)- 20H (Higher byte of
ADD A, DPL
second 16-bit number)
;Add lower bytes
MOVDPL, A ;Save result of lower byte
MOV A, B addition
;Get higher byte of second
ADDC A, DPH number in A
;Add higher bytes with any carry from
MOV DPH, A lower byte addii01
;Save result of higher byte
Program 714.6 : Find the 2's complement of a addition
MOV A, RO
number in R0.
:(A) - (RO)
CPL A
;1's complement A
ADD A, #01
;Add 1 to it to get 2's
Program 7.14.7 : Unpack the packed BCD complement save
the result in R0 and R1 nunber
such that (R0)- LSB and (R1)stored in the accumulator and
MOVB, A :Save the packed BCD MSB.
ANL A, #0FH : Mask
number
upper nibble of BCD
number
TEC
MicroproceSSors and Microcontrollers 7-35 8051 Assembly Language Programming
MOV R0, A ;Save the lower digit
MOV A, B ;Get the packed BCD number
ANL A, #OFOH ;Mask lower nibble of BCD number
SWAP A Exchange the lower and upper nibbles
MOV R1,A ;Save the upper digit.
Programn .14.8 : Subtract two 8-bit numbers and exchange digits.
MOV A, #9F ;Get the first number in A
MOV RO, #40 ;Get the second number in RO
CLR C ;Clear Carry
SUBB A, RO ;A+A-(RO)
SWAP A ;Exchange digits
Program 714.9 ts of R0 of
Subtract the conterts of RI of Bank0 from the contents
Bank2.
MOV PSW, #10 ;Select Bank2
MOV A, RO ; (A) - (RO) from Bank2
MOV PSW, #00 ;Select Bank0
CLR C :Clear carry
SUBB AR1 ;A+A-(R1) from Bank0
Program 7.44.10 : Division oftwo &-bit numbers.
MOV A, #90 :Get the first number in A

MOV B,#20 ;Get the second number in B


DIV AB :A + B, Remainder in B and Quotient in A

Proaram 11411 Vtoy two 8-bit


O numbers.
MOV A, #8F :Get the first number in A

MOV B, #79 ;Get the second number in B


MUL AB :Ax B, Higher byte of result in B and lower byte of result in A
Program 7.14,12 : Progranm to convert 8-bit binary number to its equivalent BCD.
GTU : Summer-13, Winter-15, Marks 7

Program Logic :
Step 1: Divide number with 100 decimal and save
number.
quotient i.e. save hundred's digit.
Step 2 : Make remainder as a new
Step 3 : Divide number with 10 decimal and save quotient i.e. save tens digit.
Step 4: Save remainder as ones digit.
8051 Assembly
Microprocessors and Microcontrollers 7-36
Language
Sample Example : Flowchart :
Start
Programming
Quotient Remainder
76H + 100 1 12H
12H + 10 1 8 Get the number

76H (118) 10
Program : Number + 100
MOV A, #76H; Load the binary number in A
MOV B, #100 ;Load Bwith 100decimal
Number Renainder
DIV AB ;Divide number with 100 Hund digitQuotient
MOV RO A :Save the hundreds of the number
:(Quotient of the previous division) Number+ 10
MOVA, B ;Get the remainder
MOV B, #10 ;Load B with 10 decimal Ten digit Quotient
DIV AB ;Divide number with 10 [One digitRemainder
MOV R1, A ;Save the tens of the number
MOV R3, B ;Save the ones of the number Stop

Program 7.1413: To add two 16-bit BCDnumbers.


Program :
MOVDPTR, #12341H ;Load first number Flowchart :
MOVR0, #20H Start
;Load lower byte
;of second number Load the first 16-bit BCD number
MOV R1, #30H ;Load higher byte
;of second number Load the second 16-bt BCO number
MOVA, RO ;Get the lower
; byte of Add two lower-digits
second number
ADD A, DPL ;Add two lower bytes
DA A Acjust result to valid BCD umber
;Adjust result to valid BCD
MOVDPL, A :Store the sum of lower bytes Store lower byte of result
MOV A, R1 ;Get the higher byte of
second considering
;number Add two higher digits
byte addition
ADDC A, DPH ;Add two higher cary oflower
bytes considering
carry of lower byte addition Adjust resutt to vaid
BCD number
DA A
;Adjust result to valid BCD
MOVDPH, A ;Store the sum of Store higher byte of
resu
higher bytes
Stop
Microprocessors and Microcontrollers 7-37 8051 Assembly Language Programming

Program 7.14.14 : mlementing a BCD multiply using MUL and DIV


MULBCD: UNPACK TWO BCD DIGITS RECEIVED IN ACC, FIND THEIR
PRODUCT, AND RETURN PRODUCT IN PACKED BCD FORMAT IN ACC
MULBCD: MOV B, #10H ;DIVIDE INPUT BY 16
DIV AB ;A AND BHOLDSEPARATED DIGITS
: (EACH RIGHT JUSTIFIED IN REGISTER)
MUL AB ;A HOLDS PRODUCT IN BINARY FORMAT
i (0- 99(DECIMAL)= 0- 63H)
MOV B, #10 ;DIVIDE PRODUCT BY 10
DIV AB ;A HOLDS # OF TENS, BHOLDS REMAINDER
SWAP A
ORL A, B ;PACK DIGITS
RET

Program 7.14.15 Subtract two 16-bit numbers.


MOV DPTR, #9080 (DPTR) 9080H (16-bit number)
MOV B, #50H (B) - 50H Higher byte of second number
MOV A, #40H (A) 40H Lower byte of second number
CLR C Clear Carry
SUBB A, DPL Subtract lower bytes
MOVDPL, A Save result of lower byte subtraction
MOV AB Get the higher byte
SUBB A, DPH Subtract higher byte with borrow

MOV DPH, A Save result of higher bytes subtraction


7.14.16 Generate BCD 1up COUnter ana Serd each cont to p0rt A
**********************E**

MOV A, #00 : Initialize counter


BACK: MOV P1, A : Send count to port A

ADD A, #01 ;Increment counter

DA A :Decimal adjust the counter


AJMP BACK ; Jump BACK

Program 714 Fnd the maximum number


**********
ter numbers
from a given 8 bite*****
***********%

Flowchart : (See on next page)


Program :
are stored
MOVDPTR, #2000 ; Initialize pointer to memory where numbers
MOV R0, #0A :Initialize counter

MOV R3, #00 ; Maximum = 0

TECHNICAL PUBLICATIONS -An up thrust for knowledge


8051 Assembly Language
Microprocessors and Microcontrollers 7-38
Prrogramming
AGAIN: MOVX A, @DPTR ;Get the number from memory
maximumn number
CJNE A, R3, NE ;Compare number with
AJMP SKIP :If equal go to SKIP
SKIP
NE: JCSKIP :If not equal check for carry, if carry go to
number
MOV R3, A ;Otherwise maximum =
SKIP: INC DPTR ;Increment memory pointer
DJNZ RO, AGAIN :Decrement count, if count =0stop otherwise go to
;AGAIN

Start

Initialize pointer
to memory

Initiaizecounter

Maximum number 0

Get the number

Number Yes
Max. number

No Max. numberNumber

Increment memory pointer

Decremnent counter

No
COunter0

Yes
Stop
TECHNICAL - An up
Microprocessors and Microcontrollers 7-39 8051 Assembly Language Programming

Progrm 7.14.18 : Arrange the given ten 8-bit numbers in the ascending order.
Program:
MOVR0, #09 Initialize counter1
AGAIN:MOV DPTR, #2000H ; Initialize memory
pointer
Flowchart
MOV R1, #09 ;Initialize counter2
BACK: MOVR2, DPL ;Save lower byte of
;memory address Start
MOVX A, @DPTR ;Get the number
Initialize counter 1
MOV B, A ;Save the number
INC DPTR ; Increment memory Initialize memory pointer
pointer
initialize counter 2
MOVX A, @DPTR ;Get the next
; number
Get the number
CJNE A,B,NE ;If not equal check
Increrment
;for greater or less memory pointer
AJMP SKIP ;Otherwise go to Get the number
;skip
NE: JNC SKIP ;If
Yes
(polnter) >
MOV DPL,R2 ;| Exchange (pointer 1)
;the contents interchange contents of
MOVX @DPTR, A No
compared memory locations
INC DPTR ; of two

MOV A,B ;memory Decremsnt counter 2

MOVX @DPTR, A :locations ]

SKIP : DJNZ R1, BACK :If R1 not equal to No


counter 2=0
; 0go to BACK
;If ROnot equal to
DJNZ R0, AGAIN Yes

;0 goto AGAIN Decrement counter 1

No
counter 1 0

Yes
Stop
8051 Assembly
Microprocessors and MicrOcontrollers 7- 40
Language
Program 7.14.19 : Count number of one's in a number.
Programming
Program:
MOVR2, #0 ;Initialize one's Flowchart
;counter = 0
Start
MOV R1, #08 ;Initialize iteration
;Count Initlalize cOunt 0
MOV RO, #56 ;Load number Initialize counter=8
MOV A, RO ;Get the number in
Get the contents of
;accumulator RO register in the
;Rotate A and CY LSB accumulator
BACK: RRC A
JNC SKIP ;If carryis not zero go to Rotate contents of
; skip accumulator so that
LSB will go in carry
INC R2 ;Otherwise increment
; one's counter
SKP : DJNZ R1, BACK No Is
;Decrement iteration Carry =
; count and if not
Yes
;zero repeat
Increment count

Decrementcounter

No
COunter =0)

Yes

Stop

Program 7.14.20: To find the sum of 10 numbers STOted1


in the array.
Statement : Calculate the sum of series of numbers. The is in
series
memory location 2200H and the series itself begins from memorylength of the
a. location
Assume the sum to be 8-bit number so you can ignore carries. 220111.
Store the Sumat

memory location 2300H.


b. Assume the sum to be 16-bit number. Store the sum at memory locations 2300H
and 2301H.
Microprocessors and Microcontrollers 7-41 8051 Assembly Language Programming
a. Sample problem
2200H = 04H
2201H = 20H
2202H = 15H
2203H = 13H
2204H 22H
Result = 20 + 15 + 13 + 22= 6AH
2300H 6AH
Flowchart :
Program:
a) MOVDPTR, #2200H :Initialize memory pointer Start

MOVX A, @DPTR ;Get the count


MOV R0, #10 ;Initialize the iteration Sum0
Pointer 2201H
;counter Count (2200H)
INC DPTR ; Initialize pointer to array of
;numbers Sum = Sum + (Pointer)

MOV R1, #00 ;Result = 0


Pointer a Pointer +1
BACK: MOVX A, @DPTR ; Get the number Count Count 1
ADD A, R1 ;A+Result + A
MOVR1, A ;Result -A No
Count 0
INC DPTR ; Increment the array pointer
DJNZ RO, BACK ;Decrement iteration count Yes
:if not zero repeat
(2300H) = Sum
MOV DPTR, #2300H ;Initialize memory pointer
MOV A, R1 ; Get the result End

MOVX @DPTR, A ;Store the result


b. Sample problem
2200H = 04H 2201H = 9AH

2202H = 52H 2203H= 89H 2204H = 3EH

Result 9AH + 52H + 89H + 3EH = 1B3H

2300H B3H Lower byte 2301H = 01H Higher byte


Flowchart : (See on next page)
Program:
b) :Initialize memory pointer
MOVDPTR, #2200H
MOVX A, @DPTR ;Get the count

MOV R0, #10 :Initialize the iteration counter

INC DPTR :Initialize pointer to aray of numbers


TECHNICAL PUBLICATIONS- An up thrust for knowledge
8051 Assembly
7- 42 Language
MicroprOcesSors and Microcontrollers

MOV R2, #00 : (Make


Flowchart : Programming
; result=00H] Start
MOV R1, #00
BACK: MOVX A, @DPTR ;Get the number
;A+Result + A Sum high 0
ADD A, R1 Sum low 0
A Pointer 2201H
MOVR1, A ;Result Count (2200H)
ADDC R2, #00 ;If carry exists, add it
;to MSD Sum low =Sumow + (Pointer)
INC DPTR ;increnent the array
pointer No
DJNZ RO, BACK ;Decrement iteration cary 1
count if not zero repeat
Yes
MOV DPTR, #2300H; Initialize memory
Sum high Sum high + 1
ipointer
MOV A, R1 :Get the lower byte of
Pointer Pointer +1
;result Count =Count1
MOVX @DPTR, A :Store the lower byte of
;result
No
INC DPTR :Increment memory Count 0
pointer
MOVA, R2 ;Get the higher byte of Yes

;result (2300H) Sum low


(2301H) Sum high
MOVX @DPTR, A ;Store the higher byte of
;result
End

Program 7.14.21: Data transfer from etON DIock Bi to nemory block B2.
Statement : Assume two blocks are
non-overlapped.
Flowchart: (See on next page)
Program :
MOVR2, #1 :Initialize iteration counter
MOVR1, #20H :Initialize source
memnory pointer
MOV R0, #30H :Initialize
BACK: MOV A, @R1
destination memory pointer
; Get data
MOV @RO, A ;Store data
INC R1
INC RO
:Increment source memory pointer
: Increment
destination memory pointer
DJNZ R2, BACK :Decrement iteration count and if not zero repeat
TECHNICAL PUBLICATIONS -An up thrust for
Microprocessors and Microcontrollers 7-43 8051 Assembly Language Programming

Start

Initlalize counter 10

initialize source memory pointer

Initialize destination memory pointer

Get the byte fromn source memory block

Store byte in the destination memory biock

Increment source memory pointer, increment


destination memory pointer and decrement counter

No Is
Count = 0

Yes
End

Program 714.22 : Write a program to load accumulator with values 55H and
complement 70 tlmes.
MOVRO, #70 Initialize iteration count

MOV A, # 55H : load 55H in accumulator

HERE: CPL A Complement accumulator


DJNZ RO, HERE Repeat tillRO = 0

Program 7.14.23 : Write an 8051 assembly language program to copy the value 55H
to RAM memory locations 40H to 45H using register indirect addressing with a loop.
GTU : Winter-12

MOVRO, #06H Initialize iteration count

MOVR1, #40H Initialize memory pointer

MOV A, #55H Load data byte in A


location
BACK: MOV @R1, A Copy data byte into RAM memory
INC R1
Increment memory pointer
Decrement iteration count and if not zero repeat
DJNZ R0, BACK
TECHNICAL PUBLICA TIONS - An up thrust for knowledge
Microprocessors and MIcróohtroNels

Program 7.14.24 : Write an 8051 assembly language program to clear the


Programming
and add 3to the accumulator 10 times.
CLR A Clears the accumulator (A +0)
ac umulatox
MOV R0, #0AH Set the iteration count

BACK: ADD A, #03H Add 3 to accumulator

DJNZ RO, BACK Decrement iteration count and if not zero repeat

Program 7.14.25 : Write an assembly language program to convert the


of packed BCD numbers to ASCIL. Assume that packed BCD is following
located in internal series
as shown below :
ROM
800 H: 55 H
801 H: 78 H
802 H: 54 H
803 H : 34 H GTU : Summer-13, Marks 7
MOV R0, #04 :Initialize iteration counter
MOV DPTR, #800H :Initialize internal ROM memory pointer
MOV R1, #30H ;Initialize memory pointer to store ASCII numbers
NEXT: CLR A
MOVC A, @ A+DPTR
MOV B, A ;Save the packed BCD number
ANL A, #0FH ; Mask upper nibble of BCD number
ADD A, #30H :Add 30H to convert it to ASCII
MOV @R1, A :Save the ASCII number
INC R1 ;Increment memory pointer
MOV A, B ; Get the packed BCD
ANL A, #OFOH
number
;Mask lower nibble of BCD
SWAP A number
ADD A, #30H
;Exchange the lower and upper nibbles
;Add 30H to convert it to ASCIT
MOV@R1, A ;Save the ASCII number
INC DPTR
;Point to next
DJNZ RO, NEXT number
; If
iteration counter 0, repeat
Program 7.14.26 : Write an assembly language Program using 8051 mmemonics to
convert 2digit BCD toO DInary, GTU : Winter-09
MOVA,#15 ; Get 2 digit BCD number in Accumulator
PUSH ACC ;Save value
ANL A,#OFOH :Keep high digit
SWAP A
Get it to low order byte
MOV B,#10 :10 decimal
MUL AB ;Multiply
MOV B,A ;Store result in B

TECHNICAL PUBLICATIONS
Microprocessors and Microcontrollere 7-45 8051 Assembly Language Programming
POP ACC ;Get Accumulator value i.e. 15 number
ANL A,#0FH ; keep low digit
ADD A,B ;A contain result in binary.
Program 7.14.27 Write an
assembly language program that find the position of the
Arst high in an 8-bit data item. The data is scanned
from D7 to D0.
Assume that 8-bit data item is in **accumnulator. GTU: Summer-13, Marks 7
MOV R0, #08H ;Initialize iteration
counter
MOVR1, #00H ; Initialize position
counter
BACK: RLC A :Move MSB in carry
JC NEXT ;Check for high
INC R1 ;Increment position counter
DJNZ RO, BACK :If iteration counter 0, repeat
NEXT : ; R1 contains the position of the first high.
Program 14228 Write a program to interchange the contents of PC and DPTR.
GTU: Winter-13, Marks 3
PUSH DPL ; Store the contents of DPTR in stack
PUSH DPH
LCALL NEXT;Load the PC+3 in stack
POP DPH ; Store PC+3 in DPTR
POP DPL
DEC DPTR ;Store PC in DPTR
DEC DPTR
DEC DPTR
RET Store the contents of DPTR in PC

Program 714.29 Assuming that ROMspace starting at 350h contains "BEST LUCK",
Write a program to transfer the bytes into RAMlocation at 50h.
GTU : Winter-1 1, Marks 7
ROMSP EQU 350H
RAMLOC EQU 50H
COUNT EQU 9
MOV DPTR,#ROMSP ; Initiating pointer to external ROM
;used for data
MOVR1,#COUNT ; Initializing iteration count
MOV R0,#RAMLOC ; Initializing pointer to RAM
Back : MOVX A,@DPTR :Get data
MOV@RO,A ; Save data
INC DPTR : Increment ROM location
INC RO : Increment RAM location
DJNZ R1,BACK ; Repeat untiltransfer complete

TECHNICAL PUBLICATIONS- An up thrust for knowledge


7-46 8051 Assembly Language
Microprocessors and Microcontrollers

the character string of


Programming
Program 7.14.30: Write a program to check if
palindrome. If it is, output Y to P1. Hint
length7, stored
40H onwards is a
RAM location
inpalindrome is a string in which the characters are sanme whether the string is read in
GTU: Summer-12, Marks
forward or reverse direction. counter value
MOVR3,#03 Taking half the string length as
MOV R0,#40H Initializing starting pointer to string
MOV R1,#46H Initialize end pointer to string
in A
MOV A, @RO Copy the character pointed by R0
BACK : in B
MOVB,@R1 Copy the character pointed by R1
Compare two characters
CJNE A,B,NEXT
Increment starting pointer
INC RO
Decrement end pointer
DEC R1
DJNZ R3,BACK Repeat until all characters are compared
MOVP1,#Y Since string is palindrome send Y to port P1
NEXT: NOP If not equal do nothing
END End of file

Progranm 7.14.31 : Sixteen random numbers are stored in an array, starting from
in
location 40H. Write an assembly program to count the number of non-zero elements
this array and store it in location 30H. GTU: Summer-12, Marks 7

ORG O

MOV R2,#16 Initialize iteration counter


MOVR1,#00H Initialize non-zero element counter
MOV RO,#40H Initialize memory pointer
BACK: CJNE @RO,#0H, NEXT if not zero goto NEXT
INC RO Otherwise increment memnory pointer
DJNZ R2,BACK Repeat until the last number
SJMP LAST
NEXT: INC R1 Increnent non-zero element counter
SJMP BACK
LAST : MOV3OH,R1 Save number of non-zero elements
ORG 40H
ARRAY: DB 12H, 9H, 0H, 5H, 7H, 11H, 15H, 0H, 2H, 14H, 4H, 5H, OH, 3H, OH, 17H
END

Program 7.14.32 : Write a program to calculate the average of an array of unsigned


array 5
positive integers. The array starts from 41H and number of terms in the
available in location 40H. Store the calculated average in location 3FH.
Marks 7)
GTU: Summer-12,
TECHNICAL PUBLICATIONS- An up thrust for knowledge
Microprocessors and Microcontrollers 7-47 8051 Assembly Language Programming
MOVR2, #00 : (Make
MOV R3, #00 ; result = 0]
MOV RO, #40H :Initialize count
MOV R1, #41H ; Initialize memory pointer
BACK: MOVA, @R1 : Get the number
ADD A, R2 ;Add lower byte
MOV R2, A ; Save result
ADDC R3, #00 ;If carry exists add carry to higher byte
INC R1 ; Increment memory pointer
DJNZ RO, BACK ;Decrement counter and if not zerO repeat
MOV R0, #00 ; Make quotient = 0
AGAIN : CLR C ;clear carry to perform subtraction without borrOW
MOV A, R2 :Get the lower byte
SUBB A, #40H ; Sub 40H from it
MOV R2, A : Store result
SUBB R3, #00H ;If borrOW ex0st subtract 1 from MSD
INC RO : Increment quotient
CJNE R3, #00, AGAIN :if MSB not equal zero repeat
MOV A, R2 ;[otherwise check LSB
CLR C ;whether it is
SUBB A, #40H ; less than 40H]
MOV 3FH, RO Save result (average)
Program 7.14.33 : Write assembly language progran count number of 0's and l's in a
byte stored in externalmemory location C500H. Draw flowchart.
GTU : Winter-15, Marks 7
MOV DPTR, # C500H Initialize memory pointer
MOVX A, @ DPTR Get the byte
MØV RO, # 08 Initialize iteration counter
MOVR1, # 00 Initialize one's counter
MOV R2, R1 Initialize zero's counter
Rotate A and CY LSB
BACK: RRCA
JNC NEXT if no carry go to NEXT
Increment one's counter
INC R1
JMP LAST go to LAST

NEXT: INC R2 Increment zero's counter

DJNZ RO, BACK Decrement iteration count


and if not zero repeat

TECHNICAL PUBLICATIONS - An up thrust for knowledge


8051 Assembly Language
Microprocessors and Microcontrollers 7-48 Programmine
Start

Read byte

Initialize counter = 8
Initiaize one's counter 0
Initialize zero's counter 0

Rotate byte so that LSB


will go in carry

No
zero's counter
Increment Zero
cany

Yes

Increment one's couter

Decrement iteration counter

No
cOunter

Yes

Stop

Program : Write an assembly language program to find number of negative


tUmbers trom an ardy Ot 10 nunbers 7
GTU : Summer- 17, Marks
MOVR0, #0xA Load the address of array
MOV R1, #10 Initialize counter to count iterations
MOVR2, #00 Initial counter to count negative numbers
LOOP: MOV A, @RO Get the number
RLC A Rotate left that MSB goes in carry
JNC SKIP If carry = 0, number is positive so
do not count
INC R2 Increment negative count
SKIP : INC RO Increment memory pointer
DJNZ R1, LOOP Check whether iteration count is zero i
Otherwise repeat
END
Microprocessors and Microcontrollers 7- 49 8051 Assembly Language Programming

m 7.14.35
Program Write a program to convert a HEX numnber stored at memory
eation 30H into unpacked BCD and store result from memorv locations 40H onwards.
GTU : Winter- 1 7, Marks 7
MOV RO, #4OH; Load the destination address
MOV A, 30H : Load the hex number in A
MOV B, #100; Load B with 100 decimal
DIV AB : Divide number by 100
MOV @RO, A :Save the hundreds of the number
MOV A, B ; Get the remainder
MOV B, #10 ; LoadBwith 10 decimal
DIV AB : Divide number by 10
INC RO ; Increment destination pointer
MOV @RO, A : Save the tens of the number
INC RO ; Increment destination pointer
MOV ORO, B ; Save the ones of the number

Review Questions

1. Write an 8051 based assembly language program for performing four basic arithmetic operations on
two data.

2. Write an assembly language program to convert given binary number to 3 digit BCD number.
Drav flowchart. GTU Winter-15, Marks 7

3. Write an assembly program to add two 16 bit numbers.


GTU : Summer-18, Winter-18, Marks 7
4. Write an assembly language program to copy block of 8 bit data stored at 40h - 44h memory
location into 50h - 54h memory locations. GTU : Winter-18, Marks 7
5. Write an assembly language program to convert HEX number in unpacked BCD number. Store the
result into memory locations 30 H onwards. GTU : Winter-18, Marks 7
6. Write an assembly language program to convert 8 bit two digit BCD number system into
hexadecimal number systemn. GTU :Winter-19. Marks 7

7.15 Use of Look-up Table


Many times 7-segment LED display is used to display the results or parameters in
die microcontroller svstem, In such cases we have to convert the result or parameter in
egment code. This conversion can be done using look-up technique. In the look-up
Oe the codes of the digits (0-9) to be displayed are stored sequentially in the memory.
The conversion program locates the code of a digit based on its BCD digit. Let us see
the program for BCD to common cathode 7-segment code conversion.

TECHNICAL PUBLICA TIONS - An up thrust for knowledge


Program 7.15.1 Find the 7-segment code for a given number using look-up tble
stored in the internal ROM from location 700H.
*******

Lookup table

Digit Code

3F

06

2 5B

3 4F

4 66

5 6D

6 7D

7 07

8 7F

9 6F

MOVA, #05 ;Load the number (here number is 5)


MOV DPTR, #700H Initialize look up table pointer
MOV A, @A+DPTR Get the 7-segment code

Review Questions

1. What is look-up table ?


2. Explain the use of look-up table with the help of example.

7.16 /O Port Programming GTU : Winter-15

Let us see important points for programming I/Oports :


Port 0 has open drain outputs and hence to use this port as an input or an outputit
is necessary to connect external pull-up resistors (value 10 K) as
shown in the Fig. 0
Port 1, port 2 and port 3 do not require any pull-up resistors since they have internal
pull-up resistors.
On reset, all ports are configured as an input ports.
If the ports are configured as an output ports, to make them input ports again, we
have to write FFH (1 to all 8-bits) on these ports.

TECHNICAL PUBLICATIONS- An up thrust for knowledge


8051 Assembly
Microprocessor and Microcontrollers 7- 50
Language
Program 7.15.1: Find the 7-segment code for a given number using
Programming
stored in the internal ROM from location 700H. look-up table
Lookup table

Digit Code

3F

06

2 5B

4F

4 66

5 6D

6 7D

7 07

8 7F

9 6F

MOVA, #05 ;Load the number (here number is 5)


MOV DPTR, #7OOH : Initialize look up table pointer
MOV A, @ A+DPTR ;Get the 7-segment code

Review Questions

1. What is look-up table ?


2. Explain the use of look-up table with the help of
example.
7.16 /O Port Programming GTU : Winter-15

Let us see important points for


programming I/O ports :
Port 0 has open drain outputs and hence to use this port as an input or an outputit
is necessary to connect external pull-up resistors (value 10 K) as shown in the Fig. 716.1.
Port 1, port 2 and port 3 do not require any pull-up resistors since
they have ne
pull-up resistors.
On reset, all ports are
configured as an input ports.
If the ports are configured as an again, we
have to write FFH (1 to all 8-bits) on these output ports, to make them input ports
ports.

TECHNICAL PUBLICATIONS - An up thrust for


Microprocessor and Microcontrolers 7-51 8051 Assembly Language Programming
wW W W
Vcc

Po.o
Po.t
Po.2
8051 Po.3 Port 0
Po4
Po.5
Po.6
Po.7
Fig. 7.16.1 Port0with pull-up resistors
Now we will see
simple programming examples to clearly understand the I/O
concepts discussed above.
Example 7.16.1 Wite a progranm to toggle all bits of P0 continuously.

Solution:
BACK: MOV A, #0AAH :Load AAH in the (A) accumulator
MOVP0, A ;Send contents of A to port 0
A CALL Delay ;Wait for some time
MOV A, #55H ;Load 55H in the accumulator
MOVP0, A ;Send contents of A to port 0
A CALL Delay :Wait for some time
SJMP BACK ;Repeat
The same action can be implemented using following program code.
BACK: MOV PO, A ;Send contents of Aon port 0
CPL A ;Complement contents of port 0
A CALL Delay ;Wait for somne time
SJMP Back ;Repeat
Note Like port 0, we can toggle all bits of P1, P2 or P3 by replacing the
corresponding port instead of PO in the above programs.
Example 7.16.2 Write a program to read the content of Pl and save it in R6 und also
send it to P2.
Solution :
MOV A, # OFFH ;A+FFH

MOV P1, A :Make P1 as an input port by writing all 1's to it


MOV A, P1 ;Read data from P1
MOVR6, A ;Save it in R6
MOV P2, A ; Send it to P2
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Microprocessor and Microcontrollers 7-52 8051 Assembly Language
Programmino
Example 7.16.3 Write an assembly language program to accept 10 numbers from port 1ovy
store themn in RAM locations starting from 50 H. Drawflowchart.
GTU: Winter-15, Marks 7
Solution :
MOV RO, # 50 H Initialize memory pointer
MOV R1, # 10 Initalize iteration counter
MOV A, # OFFH Load FFH in A
MOV P1, A Make P1 as an input port
BACK: MOV A, P1 Read data fromn P1
MOVR0, A Save in memory
INC RO Increment memnory pointer
DJNZ R1, BACK Decrement iteration counter
and if not zero repeat

Stert

Memory potea50
Iteratiorncounter 0

nitialize port Aas an nptut

Read bytefrom port A

Save data

Increment mernory pointer

Decrementiteration counter

No
Counter 0

Yes
Stop
MicroproCessor and Microcontrollers 7- 53 8051 Assembly Language Programming

717 VO Bit Manipulation Programming


Many times it is necessary to access only 1 or 2 bits of the port instead of all8-bits.
The 8051 I/Oports allow to access individual bits of the ports without altering the rest
of the bits in that port.
To write or change the value of particular bit we use SETB bit, CLR bit or CPL bit
instructions.

To read the status and to act accordingly we use JB bit, target, JNB bit, target or
JBC bit, target instructions.
Example 7.17.1 Write a program to make PO.0 =1, P1.2 =0 and complement P2.3.
Solution:
SETB P0.0 : Set P0.0 = 1
CLR P1.2 ;Clear P1.2 = 0
CPL P2.3 ;Complement P2.3
Example 7.17.2 Write a program to generate a square wave of 50 % duty cycle on bit 3 of
port 1.
Solution :
HERE: SETB P1.3 : Set P1.3 = 1
CALL Delay ; Wait for some time
CLR P1.3 ; Clear P1.3 = 0
LCALL Delay ;Wait for some time
SJMP HERE ;Repeat
Example 7.17.3 Write a program tooutput 56H on P2 when P1.2 =1.

Solution :
SETB P1.2 : Make P1.2 as an input
HERE : P1.2, HERE : Wait if P1.2 = 0
JNB
MOV P2, #56HH ; Send 56H on P2

port 1.
Example 7.17.4 Write a program to output a square wave of 1O kHz on bit 2 of
Assume XTAL =12 MHz.
Solution: Timer for one machine cycle,
12
T = =1 us
12x106
Machine cycles
Program 1
BACK: CPL P1.2
1
AGAIN : MOV RO, # COUNT
2
DJNZ RO, AGAIN
2
SJMP BACK
up thrust for knowledge
Microprocessor and Microcontrollers 7-54 8051 Assembly LLanguage

Required T = =0.1 msec


Programming
10 kHz

T = TON + Tor Since ToN = TOFF


T
TOFF = ToN = 2 = 0.05 ms

Thus total delay should be 0.05 ms.


From program we have,
Total delay = [1 + (1 + 2) x COUNT + 2] x 1us
:[1 + (1 + 2) x COUNT + 2] x 1 us should be equal to 0.05 ms
0.05 ms -3 us x10° = 16
COUNT =
3

Example 7.17.5 Port 1 of 8051 isto be connected to two on-off switches and two LED. It is
required to sense the status of the suitches and indicate it through the LEDs. Write a
program to accomplish this task andalso give the necessaryinterfacing details.
+5 V
Solution: Fig. 7.17.1 shows the
interfacing diagram. P1.0 and 330 2 330 Q
P1.1 are used to connect LEDs
P1.0
while P1.2 and P1.3 are used LED
to connect switches. The status P1.1 Driver
of the switches are sensed by +5V
BIT TEST instruction and LEDs 8051
are driven by BIT SET 1K
P1.2
instructions. SW

JP1.3

Program : Fig. 7.17.1


MOVP1, # 0Ch ;configure P1.2 and P1.3
; lines of part 1 as input
BACK: JNB P1.2, GLOW L1
SETB P1.0
JNB P1.3,CGLOW_ L2
SETB P1.1
JMP BACK ;keep polling
GLOW L1: CLR P1.0

JMP BACK ; keep polling


MicroprOcessor and MicrOcontrollers 7-55 8051 Assembly Language Programming

GLOW L2: CLR P1.1

JMP BACK ;keep polling


Note By clearing bit LED will glow.

Example 7.17.6 Configure P1 to read switches of P1.0 and Pl.1. If Pl.0 is high, turn on a
relay connected to P2.5 by sending logic high output, If P1.0 is low clear P2.5. If the
status of the switch at P1.1 is high, turnoff the relay connected to P2.6 y sending logic
low output. f Pi.1 is low, set P2.6 to high state.
used to
Solution: Here P1.0 and P1.1 are used to connect relays while P2.5 and P2.6 are
instruction and
connect switches. The status of switches are sensed by BIT TEST
ON-OFF contact of relays is done according to BIT SET instruction.

P2.5
Relay o Relay 1
P2.6 driving
circuit o Relay 2

+5 V
8051

1K 1K
P1.0
SW, sW2
P1.1

Fig. 7.17.2
Program :
MOV P1, #03H ;Configure P1.0 and P1.1
;lines of port 1 as inputs

BACK: JNB P1.0,ON Relay1


SETB P2.5
JNB P1.1,ON Relay2
SETB P2.6
JMP BACK ; keep poling

CLR P2.5
ON Relay1 :
JMP BACK ;keep polling
ON Relay2: CLRP2.6
JMP BACK ; keep polling

TECHNICAL PUBLICATIONS- An up thrust for knowledge


8051 Assembly Language
MicroproceSSor and Microcontrollers 7-56

GTU : Winter-15,
Programming
7.18 Assembler Directives
There are some instructions in the assembly language program which are not a part
Summer-18
of processor instruction set. These instructions are instructions to the assembler, linker,
or as assembler
and loader. These are referred to as pseudo-operations directives.
The
control the way in which a program assembles and
assembler directives enable us to and do not gernerate any
program executable
They act during the assembly of a
lists.
machine code.
commonly used assembler directives:
Let us study the E
used to define a constant without occupying a memory location
EQU: Equate : It is
example, COUNT EQU 10.
used to indicate the beginning of the address. The address comee
ORG : Origin : It is tho
ORG can be specified either in hex or decimal. To specify address in hex,
after the
address should be followed by H. For example,
ORG 0100H
byte : It is used to define the 8-bit data. The data value comes after the DB
DB : Define after the
specified in decimal, binary, hex or ASCII formats. For decimal, the D'
can be after the hex
number and for hex H
decimal number, for binary 'B after the binary
the decimal number is optional.
number should be specified. For decimal, the 'D' after
marks. For example,
To indicate ASCII, the characters are placed in quotation
NUM 1 DB 32 Num 1 equal to 32 in decimal
NUM 2 DB 32 H Num 2 equal to 32 in hex, i.e. 50 in decimal
NUM 3 DB 10010001B Num 3 equal to 10010001 in binary
CHAR DB "A" CHAR equal to ASCII A.
tell tne
END :The END directive is put after the last statement of a 8051 program to
assembler that this is the end of the program.
CODE : It assigns a name to the specified memory location in the program memoy
(Range 0 - 65535). For example,
LIST CODE 1020H : Memory location 1020H in the programn memory is noW
referred to as LIST
of 8051.
DATA : It assigns a name to the specified location in the internal RAM
(Range 0 - 255). For example,
TEMP DATA 52 H ; Register at address 52H is now
named as TEMP

IDATA: It assigns a name to the memory location whose address is located


specified register. For example,
MARKS DATA 80 Register whose address is in register at
address 80 is named as MARKS
TECHNICAL PUBLICATIONS® - An up thrust for knowledge
MicroprocesSor and Microcontrollers 7-57 8051 Assembly Language Programming

XDATA : It assigns name to the specified memory location in the external RAM
memory (Range 0- 65535). For example,
RESULT XDATA 1000H Memory location 1000H in the
extermal RAM memory is now referred
to as RESULT
USING : This directive is used to define which register bank (Bank0 - Barnk3) will be
used in the following program. For example,
USING 1 Bank 1 will be used.

Review Questions

1. What are assembler directives ? Explain.


2. Describe DB, ORG, EQu, END directive of IC 8051 microcontroller.
3. Explain the function of following irectives.
i) DB ii) EQU iii) ORG iv) DATA v) END vi) CODE
4. Explain editor, assembler, compiler and linker. Also explain assembler directives.
i) ORG ii) EQU iii) END. GTU : Winter-15. Marks 7

5. Explain the assembly directives supported by 8051 assembler. GTU : Summer-18, Marks 4

7.19 Short Questions and Answers


.

Q.1 What is the time taken to execute MUL instruction in 8031 ?


(Refer section 7.8.4)
Q.2 Give the PSW setting for making register bank 2 as default register bank in
8051 microcontroller ? (Refer program 7.14.9)
Q.3 What is the operation carried out when 8051 executes the instruction
MOVC A, @A+ DPTR ? (Refer section 7.6.2)
Q.4 Name any four bit manipulation instructions in microcontroller 8051.
(Refer section 7.9)
Q.5 How can you perform multiplication using 8051 microcontroller ?
(Refer section 7.9)
Q.6 Write the /O related instructions in microcontroller 8051.
(MOV P1, A)
Q.7 What is output of the program ?
MOVRO, A
XRL A, #3FH
XRL A, RO

TECHNICAL PUBLICATIONS -An up thrust for knowisdge


Microprocessor and Microcontrollers 7-58 R051 Assembly Language Programmine
Ans. : The contents of Àregister will be 3FH and contents of RO will be the initial
contents of A.
section 7.11.2)
Q.8 Explain about the instruction DJNZ. (Refer
(Refer program 7.14.10)
Q.9 Write an 8051 program to divide two 8-bit numbers.
LJUMP instructions of 8051 ?
Q.10 What are the uses of LCALL and
(Refer sections 7.11.2 and 7.11.3)
Q.11 List the different types of 8051
instructions.
(Refer sections 7.6, 7.7, 7.8, 7.9, 7.10 and 7.11)
boolean variable instructions of
Q.12 What are the various operations performed by
8051 ? (Refer section 7.9)
microcontroller ?
Q.13 Explain DJNZ instructions of Intel 8051
Rn and jump if not zero.
Ans. : 1. DÊNZ Rn, rel : Decrement the content of the register
address and jump if not zero.
2. DJNZ direct, rel : Decrement the content of direct 8-bit
change the data 55H stored
Q.14 Write a program using 8051 assembly language to
using rotate instruction.
in the lower byte of the data pointer register to AAH
Ans. :
MOV DPL, #55H
MOVA, DPL
RL A
register
Q.15 Specify the single instruction, which clears the most significant bit of B
of 8051, without affecting the remaining bits.
Ans. : Single instruction, which clears the most significant bit of B register of 8051,
without affecting the remaining bit is CLR B.7.
Q.16 Explain the contents of the accumulator after the execution of the following
program segments :
MOV A, #3CH
MOV R4, #66H
ANL A, R4
Ans : A = 3C
R4 = 66
A = 24

Q.17 Write a program to load accumulator A, DPH and DPL with 30H.
Ans. :
MOV A, #30
MOV DPH, A
MOV DPL, A
TECHNICAL PUBLICATIONS - An up thrust for knowledge
MicrogrocOSSOT and Micro0Ontrollert 051Assembty L.arnguage Progrgrig

a.18 Wrtte a program to subtract the contents of R1 of Bank0 from the contents of
RO of Bank2.
Ans. :
MOV PSw, #10
MOV A, RO
MOVPSw, #00
sUBB A, R1
a.19 List the 8051 instructions that affect the overow ag.
Ans. : ADD, ADDC, DIV,MUL, SUBB
Q20 List the 8051 instructions that always clear the carry fag.
Ans. : CLR C, DIV, MUL
Q21 List the 8051 instrucions that affect allthe flags.
Ans. : ADD, ADDC and SUBB
a.2 What are the addressing modes supported by 8051 ?
Ans. : The addressing modes supported by 8051 are :
1. Register addressing 2. Direct byte addresstng 3. Register indirect
4. Immediate 5. Register specific 6. Index
Q23 What does the mnemonics "LCALL" and "ACALL" stand for ?
(Refer section 7.11.3)
Q24 Give an example for DA instruction of 8051 microcontroller.
(Refer section 7.8.5)
Q25 State the functions peformed by JBC and CJNE instructions in 8051
microcontroller. (Refer section 7.11.2)
Q26 Wrte an 8051 assembly language program to7.14.24)
clear the accumutator and add 3
to the accumulator 10 times. (Refer oxample
Q27 What is the operation of the given 8051 microcontroller instrucions :
XRL A, direct ? (Refer section 7.7)

TECHNCAL PU8LICA TIONS An up rust tor knowledye

You might also like