8051 Microcontroller Instruction
8051 Microcontroller Instruction
The external basic operations performed by a microcontroller are called machine cycles.
The executive of an instruction involves execution of one or more machine cycles in a specified
order. The 8031/8051 microcontroller takes one to four machine cycles to execute an instruction.
The basic timing of the 8031/8051 machine cycle is shown in Fig. 5.1.
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
® ® ® ® ® ® ®® ® ® ® ® ®® ® ®® ® ®
® ® ® ® ®
® ® ® ® ® ® ® ®® ® ® ®
S1 S2 S3 S4 S5 S6
One Machine Cycle. ®
®
Fig. 5.1 : Basic timing of a machine cycle.
The entire timing of a machine cycle of 8031/8051 is divided into 6 states and they are
denoted as S1, S2, S3, S4, S5 and S6. The timing of each state is two clock periods and they are
denoted as P1 and P2.
A state in a machine cycle is a basic time interval for discrete operation of the microcontroller
such as fetching an opcode byte, decoding an opcode, executing an opcode, writing a data, etc. The
time taken to execute a machine cycle is 12 clock periods and so the time taken to execute an instruction
is obtained by multiplying the number of machine cycles of that instruction by 12 clock periods.
Instruction execution time = C × 12 × T
1
= C × 12 × f
where, C = Number of machine cycles of an instruction.
T = Time period of crystal frequency in seconds.
f = Crystal frequency in Hz.
The External program memory fetch machine cycle is executed by the 8031/8051 to fetch
the opcode and subsequent instruction bytes from the memory. The timing diagram of an external
memory fetch cycle is shown in the Fig. 5.2. During one machine cycle (6 states), two consecutive
bytes of program memory are read. In one-byte instruction, the second byte is discarded. The
timing of various signals involved in the fetch operation are shown in the timing diagram in Fig. 5.2.
Read Second Byte of the Instruction
or Read Opcode of next Instruction
Read Opcode or Memory Read/Write Cycle
® ® ® ®
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
CLK
S1 S2 S3 S4 S5 S6 ®
® ® ® ® ® ® ® ® ®
® ®
ALE
PSEN
A15 - A8
(P2.7 - P2.0) High Byte Address A15 - A8(PChb) High Byte Address A15 - A8(PChb/DPhb )
1. At the falling edge of phase P2 of first state S1, the microcontroller outputs the low byte address on AD7-AD0 lines
and high byte address on A15-A8 lines. For program memory fetch, the content of the Program Counter (PC) is the
address of the program code. The ALE is asserted high to enable the address Latch.
2. At the middle of state S2, the ALE is asserted low and this enables the Latch to take low byte of the address and
keep on its output lines.
3. The program store enable PSEN is asserted low to fetch the opcode from the memory and load on the
AD7-AD0 lines at the state S3.
4. The microcontroller utilizes the first three states (S1, S2 and S3) to fetch the opcode from the memory.
5. During the remaining three states of one machine cycle (S4, S5 and S6), the microcontroller fetches the second byte
of the same instruction or opcode of the next instruction or executes the external memory read/write cycle.
6. When executing the one-byte instruction, the states S4, S5 and S6 are used by the processor for internal
operations to decode the instructions and for completing the task specified by the one-byte instruction.
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051 5. 3
The memory read cycle is executed by the 8031/8051 to read a data from the external data
memory. The data memory read cycle is executed immediately after an opcode fetch if the instruction
execution requires an external data memory access. The timing diagram of the external memory
read cycle is shown in Fig. 5.3. The timings of various signals involved in read operation are shown
in the timing diagram.
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
CLK
S4 ® S5 ® S6 S1 S2 S3 ®
® ® ® ® ® ® ® ® ®
ALE
RD
A15 - A8
(P2.7 - P2.0) High Byte Address A15 - A8(DPhb)
1. External memory read operation needs six states. In one-byte instruction, when the external memory access is
required, these six states are S4, S5 and S6 of the first machine cycle and S1, S2 and S3 of the second machine
cycle. (In two-byte instruction, when external memory access is required these six states are S1 to S6 of the
second machine cycle.)
2. At the first half of the state S4, the ALE is asserted high to enable the address latch.In this state, the microcontroller
outputs the content of the Data Pointer low (DPl b ) on AD7- AD0 lines and the content of the Data Pointer high (DP hb)
on A15-A8 lines.
3. At the S1 state of the second machine cycle, the memory read signal is asserted low and the data in the specified
address can be read and placed on the AD 7-AD0 lines. The read signal is asserted low for the three states S1, S2
and S3 of the second machine cycle.
4. At the end of the S3 state of the second cycle, the RD signal is asserted low and at this time the data is latched
into the microcontroller.
5. The high byte of address A 15-A8 is valid for six states, i.e., from the S4 of the first machine cycle to S3 of second
machine cycle.
6. At the last three states of the second machine cycle, the microcontroller reads the next byte of program memory
and discards it.
5. 4 MICROPROCESSORS AND MICROCONTROLLERS
CLK
S4 ® S5 ® S6 ® S1 ® S2 ® S3 ®
® ® ® ® ® ®
ALE
WR
A15 - A8
(P2.7 - P2.0) High Byte Address A15 - A8(DPhb)
1. This write operation needs six states. In one-byte instruction, when the external memory access is required,
these six states are S4, S5 and S6 of the first machine cycle and S1, S2 and S3 of the second machine cycle.
(In two-byte instruction, when external memory access is required these six states are S1 to S6 of the second
machine cycle.)
2. At the first half of the state S4, the ALE is asserted high to enable the address latch. In this state, the
microcontroller outputs the content of the Data Pointer low (DPlb) on the AD7- AD0 lines and the content of Data
Pointer high (DPhb) on the A15-A8 lines.
3. At the S1 state of the second machine cycle, the memory write signal is asserted low and the data is output
on the AD7-AD0 lines. The write signal is asserted low by the microcontroller for three states S1, S2 and S3
of the second machine cycle.
4. At the end of the S3 state of the second cycle, the WR signal is asserted low and at this time the data is
latched into the external memory.
5. The high byte of address A8-A15 valid for six states, i.e., from S4 of the first machine cycle to S3 of the second
machine cycle.
6. At the last three states of the second machine cycle, the microcontroller reads the next byte of the program
memory and discards it.
Port Operation Cycle
The port value can be changed during the port operation machine cycle. The timing diagram
of the port operation cycle is shown in the Fig. 5.5. The various signals during the port operation
cycle are shown in the timing diagram. By using some instructions we can change the port value
immediately. For this, the 8031/8051 executes the port operation cycle.
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051 5. 5
1. The port operation cycle needs six states. When port operation is required in 1-byte instruction, these six states
are S4, S5 and S6 of the first machine cycle and S1, S2 and S3 of the second machine cycle.
2. In the fifth state S5 of the every machine cycle, all the port values are sampled.
3. The ports P0 and P1 are sampled at phase P1 of state S5. The ports P2, P3, RST are sampled at phase P2 of the
state S5 in the first machine cycle.
4. The port values are changed by placing the new value on the specified port.
5. The changes in the new value takes place at the S1 state of the second machine cycle.
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
CLK
S4 S5 S6 S1 S2 S3 ®
® ® ® ® ® ® ® ® ® ®
®
ALE
Sampling
Time
®
®
P0, P1 Inputs
Sampled Sampling
® Time
®
P2, P3, RST
Inputs Sampled
The size of an 8031/8051 instruction is one to three bytes. The first byte is opcode and the
subsequent bytes are address or data. The 8031/8051 microcontroller executes the instructions in one to
four machine cycles. Based on the method of execution of the machine cycles, the instructions can be
classified as shown below. The various operations performed during execution is also shown below.
Case (i) : 1-byte, 1-cycle - Opcode fetch (3 states) + Dummy program memory fetch (3 states)
Case (ii) : 2-byte, 1-cycle - Opcode fetch (3 states) + Fetch second byte of instruction (3 states)
Case (iii) : 1-byte, 2-cycle - Opcode fetch (3 states) + Data memory read/write (6 states) +
dummy program memory fetch (3 states)
Case (iv) : 2-byte, 2-cycle - Opcode fetch (3 states) + Fetch second byte of instruction (3 states)
+ Data memory read/write (6 states)
Case (v) : 3-byte, 2-cycle - Opcode fetch (3 states) + Fetch second byte (3 states) + fetch
third byte (3 states) + dummy program memory fetch (3 states)
Case (vi) : 1-byte, 4-cycle - Opcode fetch (3 states) + Dummy fetches (7 × 3 states)
The timing diagram of MOVX and ADD instructions are shown in Fig. 5.6 and Fig. 5.7 respectively:
5. 6
Read Opcode of Next
Instruction from Program
Read Opcode Read Data from Memory ® Memory and Discard
® ® ® ® ®
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
CLK
® ® ® ® ® ® ®
S1 ® ® S2 ® S3 ® ® S4 ® S5 ® ® S6 ® S1 ® S2 ® ® S3 ® ® S4 ® S5 ® S6 ®
ALE
PSEN
RD
A15 - A8
20H(PChb) 35H(DPhb) 20H(PChb)
(P2.7 - P2.0)
The MOVX A,@DPTR instruction is used to move the content of the data memory addressed
by the DPTR to the A-register (or accumulator). The timing diagram of this instruction is shown in
Fig. 5.6.
The MOVX A,@DPTR is a one-byte instruction and executed in two machine cycles. In
the first three states (S1, S2 and S3) of the first machine cycle, the opcode is fetched and the next
six states (S4, S5 and S6 of the first machine cycle and S1, S2 and S3 of the second machine
cycle) are used for data memory read operation. During the last three states (S4, S5 and S6) of the
second machine cycle, a dummy program memory fetch is performed and it is discarded. The
controller will not increment the program counter for this dummy fetch.
In the timing diagram shown in Fig. 5.6 it is assumed that 201AH is the address of the
program memory where the instruction is stored. Also, it is assumed that the content of the DPTR
(Data Pointer) is 354CH and the content of the data memory location with address 354CH be 7FH.
The opcode of the MOVX A,@ DPTR instruction is E0H.
Timing diagram of ADD A, #DATA
The instruction ADD A,#data is used to add an 8-bit immediate data to the A-register
(accumulator). The timing diagram of this instruction is shown in Fig. 5.7. The ADD A,#data is a
two-byte instruction and executed in one machine cycle. In the first three states (S1, S2 and S3) of
the machine cycle, the opcode is fetched from the program memory. In the next three states (S4,
S5 and S6), the immediate data (which is the second byte of instruction) is fetched from the
program memory.
® Read Opcode Read 2nd Byte of the Instruction
®
® ®
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
CLK
S1 S2 ® S3 S4 S5 ® S6 ®
® ® ® ® ® ® ® ® ®
ALE
PSEN
AD7 - AD0
FFH(PClb) Opcode 24H 00H(PClb) Data C2H
(P0.7 - P0.0)
A15 - A8
(P2.7 - P2.0) 20H(PChb) 21H(PChb)
In the timing diagram shown in Fig. 5.7, it is assumed that 20FFH and 2100H are the address
of the program memory where the two bytes of the instruction are stored. Also, it is assumed that
the immediate data in the instruction is C2H. The opcode of ADD A,#data instruction is 24H.
5.2 A DD RES S ING M OD ES
Every instruction of a program has to operate on a data. The method of specifying the data to
be operated by the instruction is called addressing. The 8031/8051 has the following types of addressing:
1. Immediate addressing
2. Direct addressing
3. Register addressing
4. Register indirect addressing
5. Implied addressing
6. Relative addressing
Immediate Addressing
In immediate addressing mode, an 8/16-bit immediate data/constant is specified in the instruction itself.
Example :
MOV A, #6CH
Move the immediate data 6CH given in the instruction to the A-register. (Accumulator).
Example :
MOV DPTR, #0100H
Load the immediate 16-bit constant given in the instruction in the DPTR (Data pointer). This constant will be an
address of the data memory location.
Direct Addressing
In direct addressing mode, the address of the data is directly specified in the instruction. The direct address can be
the address of an internal data RAM location (00H to 7F H ) or the address of a special function register (80H to FF H ).
Example :
MOV A,07 H
The address of the R7-register of bank-0 is 07. This instruction will move the content of the R7-register to the
A-register (Accumulator).
Register Addressing
In register addressing mode, the instruction will specify the name of the register in which the data is available.
Example :
MOV R2,A
The content of the A-register (accumulator) is moved to register R2 of the currently selected memory bank.
Implied Addressing
In implied addressing mode, the instruction itself specifies the data to be operated by the instruction.
Example :
CPL C
Complement carry flag.
Relative Addressing
In relative addressing mode, the instruction specifies the address relative to the program counter . The instruction
will carry an offset whose range is -12810 to + 12710 . The offset is added to the PC to generate the 16-bit physical address.
Example :
JC Offset
If carry is one, the program control jumps to an address obtained by adding the content of the program counter
and offset value in the instruction.
The 8031/8051 instructions can be classified into the following five groups:
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branching instructions
5. Boolean instructions
The summary of 8031/8051 instructions are provided in Table-5.1. The meaning of mnemonics
used in the instruction set are listed in Table-5.2. The instructions affecting the flags are also listed
in Table-5.3.
TABLE-5.1 : SUMMARY OF 8031/8051 INSTRUCTION SET
1. MOV A,Rn 11 1 0 1 r r r 1 12
2. MOV A,direct 11 1 0 0 1 0 1 2 12
3. MOV A,@Ri 11 1 0 0 1 1 i 1 12
4. MOV A, #data 01 1 1 0 1 0 0 2 12
5. MOV Rn,A 11 1 1 1 r r r 1 12
6. MOV Rn,direct 10 1 0 1 r r r 2 24
7. MOV Rn,#data 01 1 1 1 r r r 2 12
8. MOV direct,A 11 1 1 0 1 0 1 2 12
9. MOV direct,Rn 10 0 0 1 r r r 2 24
5. 10 MICROPROCESSORS AND MICROCONTROLLERS
Table-5.1 continued …
Size of Time taken to execute
S.No. Mnemonic Opcode instruction the instruction
in bytes in clock periods
67. XRL A,@ Ri 0 1 1 0 0 1 1 i 1 12
68. XRL A,#data 0 1 1 0 0 1 0 0 2 12
69. XRL direct,A 0 1 1 0 0 0 1 0 2 12
70. XRL direct,#data 0 1 1 0 0 0 1 1 3 24
71. CLR A 1 1 1 0 0 1 0 0 1 12
72. CPL A 1 1 1 1 0 1 0 0 1 12
73. RL A 0 0 1 0 0 0 1 1 1 12
74. RLC A 0 0 1 1 0 0 1 1 1 12
75. RR A 0 0 0 0 0 0 1 1 1 12
76. RRC A 0 0 0 1 0 0 1 1 1 12
77. SWAP A 1 1 0 0 0 1 0 0 1 12
IV. BRANCHING INSTRUCTIONS
78. ACALL addr11 a10 a9 a8 1 0 0 0 1 2 24
79. LCALL addr16 0 0 0 1 0 0 1 0 3 24
80. RET 0 0 1 0 0 0 1 0 1 24
81. RETI 0 0 1 1 0 0 1 0 1 24
Table-5.1 continued …
Instruction CF AF OF PF
ADD + + + +
ADDC + + + +
ANL C,bit + - - -
ANL C,/bit + - - -
CJNE + - - -
CLR C 0 - - -
CPL C + - - -
DIV 0 - 0 -
DA + + - +
MUL 0 - + -
MOV C, bit + - - -
ORL C, bit + - - -
ORL C,/bit + - - -
RRC A + - - -
RLC A + - - -
SETB C 1 - - -
SUBB + + + +
The instruction set of the 8031/8051 microcontroller includes a variety of instructions for
data transfer between the registers and the memory locations. The various mnemonics used for
data transfer instructions are MOV, MOVC, MOVX, PUSH, POP, XCH and XCHD, and they
perform any one of the following operations:
� Copy the content of an SFR to the internal memory or vice versa.
� Load an immediate operand to the SFR/internal memory.
� Exchange the content of the SFR/internal memory with the accumulator.
� Copy the content of the program memory to the accumulator.
� Copy the content of the data memory to the accumulator or vice versa.
The data transfer instructions of 8031/8051 are listed in Table-5.4 with a brief explanation
about each instruction.
TABLE - 5.4 : DATA TRANSFER INSTRUCTIONS
1. MOV A,Rn (A) ← (Rn) The content of register Rn is moved to the accumulator (A-register).
The Rn can be any one of the 8 registers of the currently selected bank.
2. MOV A,direct direct = 8-bit address of The content of internal RAM/SFR (whose address is specified directly
internal RAM/SFR in the instruction) is moved to accumulator (A-register).
(A) ← (RAM/SFR)
3. MOV A,@Ri (Ri) = Internal RAM address The content of internal RAM memory (whose address is specified by
(A) ← (RAM) the Ri-register) is moved to the accumulator (A-register). The register
Ri can be either R0 or R1 or the currently selected register bank.
4. MOV A,#data (A) ← data The data given in the instruction is moved to the accumulator (A-register).
5. MOV Rn,A (Rn) ← (A) The content of the accumulator is moved to register Rn, where Rn is
any one of the 8 registers of the currently selected register bank.
6. MOV Rn,direct direct = 8-bit address of The content of internal RAM/SFR (whose address is directly specified
internal RAM/SFR in the instruction) is moved to register Rn, where Rn is any one of the
(Rn)← (RAM/SFR) 8 registers of the currently selected register bank.
7. MOV Rn,#data (Rn) ← data The immediate data given in the instruction is moved to register Rn,
where Rn is any one of the 8 registers of the currently selected register
bank.
8. MOV direct,A direct = 8-bit address of internal The content of the accumulator is moved to internal RAM/SFR
RAM/SFR (RAM/SFR) ← (A) whose address is directly specified in the instruction.
9. MOV direct,Rn direct = 8-bit address of internal The content of register Rn is moved to internal RAM/SFR
RAM/SFR (RAM/SFR) ← (Rn) whose address is directly specified in the instruction.
10. MOV direct,direct direct = 8-bit address of internal The content of one internal RAM/SFR is moved to another
RAM/SFR internal RAM/SFR. The address of the source and destination
(RAM/SFR) ←(RAM/SFR) are directly specified in the instruction.
11. MOV direct,@Ri (Ri) = Internal RAM address The content of internal RAM whose address is specified by Ri
of source operand is moved to another internal RAM/SFR whose address is
direct = Internal RAM/SFR directly specified in the instruction. The register Ri can be
address of destination operand either R0 or R1.
(RAM/SFR) ← (RAM)
MICROPROCESSORS AND MICROCONTROLLERS
Table-5.4 continued …
S.No. Instruction Symbolic representation Explanation
12. MOV direct,#data direct = Address of internal RAM/SFR The immediate data given in the instruction is moved to the
(RAM/SFR) ← data internal RAM/SFR, whose address is directly specified in the
instruction.
13. MOV @Ri,A (Ri) = Internal RAM address The content of the accumulator is moved to an internal RAM
(RAM) ← (A) location whose address is specified by the Ri-register. The
register Ri can be either R0 or R1.
14. MOV @Ri,direct direct = Internal RAM/SFR address The content of the internal RAM/SFR whose address is directly
of source operand specified in the instruction is moved to another internal RAM
(Ri) = Internal RAM address of location whose address is specified by the Ri-register. The
destination operand. register Ri can be either R0 or R1.
(RAM) ← (RAM/SFR)
15. MOV @Ri,#data (Ri) = Internal RAM address The immediate data given in the instruction is moved to an
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051
21. MOVX @Ri,A (Ri) = 8-bit address of external data RAM The content of the accumulator is moved to the external data
(RAM) ← (A) RAM. The content of the Ri is the 8-bit address of external
RAM. The register Ri can be either R0 or R1 of the currently
selected register bank.
22. MOVX @DPTR,A (DPTR) = 16-bit address of external data RAM The content of the accumulator is moved to the external data
(RAM) ← (A) RAM. The content of the DPTR is the 16-bit address of the
external RAM.
23. PUSH direct (SP) ← (SP) + 1 The stack pointer is incremented by one. The content of the internal
direct = 8-bit address of internal RAM/SFRinternal RAM/SFR (whose address is directly specified in the instruction)
((SP)) ← (RAM/SFR) is moved to the internal RAM memory pointed by the SP.
24. POP direct direct = 8-bit address of internal RAM/SFR The content of the internal RAM memory pointed by the SP is
(RAM/SFR) ← ((SP)) moved to the internal RAM/SFR (whose address is directly
(SP) ← (SP) − 1 specified in the instruction). Then the stack pointer is
decremented by one.
26. XCH A,direct direct = 8-bit address of internal RAM/SFR The content of the internal RAM /SFR whose address is directly
(A) → (RAM/SFR) specified in the instruction is exchanged with the accumulator.
←
27. XCH A,@Ri (Ri) = 8-bit address of internal RAM The content of the internal RAM whose address is specified by
(A) →
← (RAM) the Ri-register is exchanged with the accumulator. The register
Ri can be either R0 or R1 of the currently selected register bank.
28. XCHD A,@Ri (Ri) = 8-bit address of internal RAM The lower nibble of the internal RAM addressed by Ri-register
(A) → ← (RAM) is exchanged with the lower nibble of the accumulator. The
3-0 3-0
content of the upper nibble of RAM and accumulator are not
altered. The Register Ri can be either R0 or R1 of the currently
selected register bank.
MICROPROCESSORS AND MICROCONTROLLERS
5.5 ARI THM ETI C I NST RU CTION S
The arithmetic group includes instructions for performing addition, subtraction, multiplication, division, increment and decrement
operation on the binary data. The mnemonic used in arithmetic instructions are ADD, ADDC, SUBB, INC, DEC, MUL, DIV and DA. The
results of most of the arithmetic operations are stored in the accumulator except a few decrement and increment operations. The
arithmetic instructions except increment and decrement instructions modify the flags of 8031/8051. The arithmetic instructions of 8031/
8051 are listed in Table-5.5.
TABLE- 5.5 : ARITHMETIC INSTRUCTIONS OF 8031/8051
30. ADD A,direct direct = 8-bit address of internal RAM/SFR The content of internal RAM/SFR and accumulator are added.
(A) ← (A) + (RAM/SFR) The result is stored in the accumulator. The address of internal
RAM/SFR is directly specified in the instruction.
31. ADD A,@Ri (Ri) = Address of internal RAM The content of the internal RAM and the accumulator are added.
(A) ← (A) + (RAM) The result is stored in the accumulator. The register Ri holds the
address of the internal RAM and Ri can be either R0 or R1
of the currently selected register bank.
32. ADD A,#data (A) ← (A) + data The immediate data given in the instruction is added to
accumulator.
33. ADDC A,Rn (A) ← (A) + CF + (Rn) This instruction is same as ADD A,Rn except that the current
value of carry flag (i.e., previous carry) is also added to the sum.
34. ADDC A,direct direct = address of internal RAM/SFR This instruction is same as ADD A,direct except that the
(A) ← (A) + CF + (RAM/SFR) current value of carry flag (i.e., previous carry) is also added
to the sum.
35. ADDC A,@Ri (Ri) = address of internal RAM This instruction is same as ADD A,@Ri except that the current
(A) ← (A) + CF + (RAM) value of the carry flag (i.e., previous carry) is also added
to sum.
5. 19
Table-5.5 continued …
S.No. Instruction Symbolic representation Explanation 5. 20
36. ADDC A,#data (A) ← (A) + CF + data The immediate data given in the instruction, the carry flag and
the content of the accumulator are added. The result is stored
in the accumulator.
37. SUBB A,Rn (A) ← (A) − CF − (Rn) The carry flag and the content of the Rn-register are subtracted
from the content of the accumulator. The result is stored in the
accumulator. The register Rn can be any one of the 8 registers
of the currently selected register bank.
38. SUBB A,direct direct = Address of internal RAM/SFR The carry flag and the content of RAM/SFR (specified by
(A) ← (A) − CF− (RAM/SFR) direct address) are subtracted from the content of the
accumulator. The result is stored in the accumulator.
39. SUBB A,@Ri (Ri) = Address of internal RAM The carry flag and the content of RAM (specified by the Ri-register)
(A) ← (A) − CF − (RAM) are subtracted from the content of the accumulator. The result is
stored in the accumulator. The register Ri can be either R0 or R1 of
the currently selected register bank.
40. SUBB A,#data (A)← (A) − CF − data The carry flag and the data given in the instruction are subtracted
from the accumulator. The result is stored in the accumulator.
41. INC A (A) ← (A) + 1 The content of the accumulator is incremented by one.
42. INC Rn (Rn) ← (Rn) +1 The content of register Rn is incremented by one. The Rn can be
any one of the eight registers of the currently selected register
bank.
43. INC direct direct = Address of internal RAM/SFR The content of RAM/SFR (whose address is directly given in the
(RAM/SFR) ← (RAM/SFR) +1 instruction) is incremented by one.
44. INC @Ri (Ri) = Address of internal RAM/SFR The content of RAM (whose address is specified by Ri) is
(RAM) ← (RAM) +1 incremented by one. The Ri can be either R0 or R1 of the currently
selected register bank.
45. DEC A (A) ← (A) − 1 The content of the accumulator is decremented by one.
46. DEC Rn (Rn) ← (Rn) − 1 The content of register Rn is decremented by one. The Rn can be
any one of the eight registers of the currently selected register bank.
MICROPROCESSORS AND MICROCONTROLLERS
Table-5.5 continued …
S.No. Instruction Symbolic representation Explanation
47. DEC direct direct = Address of internal RAM/SFR The content of RAM/SFR (whose address is directly given in the
(RAM/SFR) ← (RAM/SFR) − 1 instruction) is decremented by one.
48. DEC @Ri (Ri) = Address of internal RAM The content of RAM (whose address is specified by Ri) is
(RAM) ← (RAM) −1 decremented by one. The Ri can be either R0 or R1 of the currently
selected register bank.
49. INC DPTR (DPTR) ← (DPTR) + 1 The 16-bit content of the DPTR (Data Pointer) is incremented by one.
50. MUL AB (B) (A) ← (A) × (B) The contents of A and B registers are multiplied. The low byte of the
high low product is stored in the A-register and high byte of the product is stored
byte byte
in the B-register.
51. DIV AB (A) ← (A) ÷ (B) The content of the A-register is divided by the content of the B-register.
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051
Quotient The quotient is stored in the A-register and the remainder is stored in
(B) ← (A) MOD (B) B-register.
Remainder
52. DAA i) If (A)3 − 0 > 9 or AF = 1 then This instruction is executed after the addition of two packed BCD data,
(A)3 − 0 ← (A) 3 − 0 + 06 to convert the result in the accumulator to the packed BCD data. If the
lower nibble of the accumulator is greater than 09 or the AF is set,
ii) If (A)7 − 4 > 9 or CF = 1 then
then it is corrected by adding 06. If the upper nibble of the accumulator
(A) 7 − 4 ← (A) 7 − 4 + 06 is greater than 09 or the CF is set, then it is corrected by adding 06.
The logical group includes instructions for performing logical AND, OR, Exclusive-OR, and Complement operations, and instructions
for right and left rotation. The mnemonic used in logical operations are ANL, ORL, XRL, CLR, CPL, RL, RLC, RR, RRC and SWAP. The
logical operations except rotate through carry do not modify the flags of 8031/8051. In rotate through carry, the carry flag alone is
modified. In most of the logical instructions, the result is stored in the accumulator and in some instructions the result is stored in the
internal RAM/SFR. The logical instructions of 8031/8051 are listed in Table-5.6 with a brief explanation about each instruction.
5. 21
TABLE - 5.6 : LOGICAL INSTRUCTIONS
53. ANL A,Rn (A) ← (A) & (Rn) The content of the register Rn and the accumulator are bit by bit logically
ANDed, and the result is stored in the accumulator. The register Rn can
be any one of the 8 registers of the currently selected register bank.
54. ANL A,direct direct = Address of internal RAM/SFR The content of the RAM/SFR (whose address is directly given in the
(A) ← (RAM/SFR) & (A) instruction) and the accumulator are bit by bit logically ANDed, and the
result is stored in the accumulator.
55. ANL A,@Ri (Ri) = Address of internal RAM The content of the RAM (whose address is specified by Ri) and the
(A) ← (RAM) & (A) accumulator are bit by bit logically ANDed, and the result is stored in
the accumulator. The register Ri can be either R0 or R1 of the currently
selected register bank.
56. ANL A,#data (A) ← (A) & data The data given in the instruction and the content of the accumulator
are bit by bit logically ANDed, and the result is stored in the accumulator.
57. ANL direct, A direct = Address of internal RAM/SFR The content of the accumulator and the RAM/SFR are bit by bit logically
(RAM/SFR) ← (RAM/SFR) & (A) ANDed, and the result is stored in the RAM/SFR. The address of the
RAM/SFR is directly specified in the instruction.
58. ANL direct,#data direct = Address of internal RAM/SFR The data given in the instruction and the content of RAM/SFR are bit by
(RAM/SFR) ← (RAM/SFR) & data bit logically ANDed, and the result is stored in RAM/SFR. The address
of the RAM/SFR is directly specified in the instruction.
59. ORL A,Rn (A) ← (A) | (Rn) The content of the register Rn and the accumulator are bit by bit logically
ORed, and the result is stored in the accumulator. The register Rn can
be any one of the 8 registers of the currently selected register bank.
60. ORL A,direct direct = Address of internal RAM/SFR The content of the RAM/SFR (whose address is directly given in the
(A) ← (RAM/SFR) | (A) instruction) and the accumulator are bit by bit logically ORed, and the
result is stored in the accumulator.
61. ORL A,@Ri (Ri) = Address of internal RAM The contents of the RAM (whose address is specified by Ri) and the
(A) ← (RAM) | (A) accumulator are bit by bit logically ORed, and the result is stored in
the accumulator. The register Ri can be either R0 or R1 of the currently
selected register bank.
MICROPROCESSORS AND MICROCONTROLLERS
Table-5.6 continued …
Rn can be any one of the 8 registers of the currently selected register bank.
66. XRL A,direct direct = Address of internal RAM/SFR The contents of the RAM/SFR (whose address is directly given in the
(A) ← (RAM/SFR) ^ (A) instruction) and the accumulator are bit by bit logically exclusive-ORed,
and the result is stored in the accumulator.
67. XRL A,@Ri (Ri) = Address of internal RAM The contents of the RAM (whose address is specified by Ri) and the
(A) ← (RAM) ^ (A) accumulator are bit by bit logically exclusive-ORed, and the result
is stored in the accumulator. The register Ri can be either R0 or R1
of the currently selected register bank.
68. XRL A,#data (A) ← (A) ^ data The data given in the instruction and the content of the accumulator
are bit by bit logically exclusive-ORed, and the result is stored in the
accumulator.
69. XRL direct,A direct = Address of internal RAM/SFR The contents of the accumulator and RAM/SFR are bit by bit logically
(RAM/SFR) ← (RAM/SFR)^ (A) exclusive-ORed, and the result is stored in the RAM/SFR. The address
of the RAM/SFR is directly specified in the instruction.
70. XRL direct,#data direct = Address of internal RAM/SFR The data given in the instruction and the content of RAM/SFR are bit
(RAM/SFR) ← (RAM/SFR)^ data by bit logically exclusive-ORed, and the result is stored in the RAM/SFR.
The address of the RAM/SFR is directly specified in the instruction.
5. 23
Table-5.6 continued …
S.No. Instruction Symbolic representation Explanation 5. 24
71. CLR A (A) ← 0 The content of the accumulator is cleared.
72. CPL A (A) ← ~ (A) The content of the accumulator is complemented.
73. RL A The content of the accumulator is rotated left by one bit. The most
A
significant digit (B7) is moved to the least significant digit (B0) position.
B7 B6 B5 B4 B3 B2 B1 B0
Bn+1 ¬ Bn ; B0 ¬ B7
A
74. RLC A The content of the accumulator along with the carry is rotated left by
CF B7 B6 B5 B4 B3 B2 B1 B0
one bit. The carry is moved to the least significant digit position and
Bn+1 ¬ Bn ; B0 ¬ CF ; CF ¬ B7 the most significant digit is moved to the carry.
75. RR A The content of the accumulator is rotated right by one bit. The least
A
significant digit (B0) is moved to the most significant digit (B7) position.
B7 B6 B5 B4 B3 B2 B1 B0
Bn ¬ Bn+1 ; B7 ¬ B0
76. RRC A A The content of the accumulator along with the carry is rotated right by
CF B7 B6 B5 B4 B3 B2 B1 B0 one bit. The carry is moved to the most significant digit (position) and
the least significant digit (B0) is moved to carry.
Bn ¬ Bn+1 ; B7 ¬ CF ; CF ¬ B0
77. SWAP A (A)3-0 (A)7-4 The higher nibble of the accumulator is exchanged with the lower
nibble of the accumulator.
5.7 PROGR AM BRANCHING I NSTRUCTI ONS
Normally a program is executed sequentially and the PC (Program Counter) keeps track of the address of the instructions and it
is incremented appropriately after each fetch operation. The program branching instructions will modify the content of the PC so that,
the program control branches to a new address. The program branching instructions of 8031/8051 includes conditional and unconditional
branching instruction. In conditional branching instructions, the content of the PC is modified, only if the condition specified in the
MICROPROCESSORS AND MICROCONTROLLERS
instruction is true, whereas in unconditional branching instruction, the PC is always modified. The instructions like ACALL and LCALL
will save the previous value of the PC in the stack before modifying the PC. The program branching instructions of 8031/8051 are listed
in Table-5.7 with a brief explanation about each instruction.
TABLE - 5.7 : PROGRAM BRANCHING INSTRUCTIONS
is moved to the lower 11-bit position of the PC. (The 11- bit address is the
second byte of the instruction and upper 3 bits of the opcode.) Now, the
controller starts fetching the instructions from this new address.
79. LCALL addr16 (PC) ← (PC) + 3 This instruction is used to unconditionally call a subroutine anywhere
(SP) ← (SP) + 1 in the 64 k memory space. First, the PC is incremented by three to point
((SP)) ← (PC)7-0 to the next instruction. Next, the SP is incremented and the content of the
(SP) ← (SP) + 1 PC is saved in the stack memory pointed by the SP. Then, the 16-bit
((SP)) ← (PC)15-8 address given in the instruction is moved to the PC and so the controller
(PC) ← addr16 starts fetching the instruction from this new address.
80. RET (PC)15-8 ← ((SP)) This instruction is used to terminate a subroutine. On execution of this
(SP) ← (SP) − 1 instruction, the content of the stack memory pointed by the SP is moved
(PC)7-0 ← ((SP)) to the high byte of PC and SP is decremented by one. Then, the content of
(SP) ← (SP) − 1 the stack memory pointed by SP is moved to the low byte of PC and again
the SP is decremented by one.
81. RETI (PC)15-8← ((SP)) This RETI instruction is used to terminate an interrupt service subroutine.
(SP) ← (SP) − 1 This instruction moves the top of the stack to the PC similar to that of RET
(PC)7-0 ← ((SP)) instruction and in addition restores the interrupt logic to accept additional
(SP) ← (SP) − 1 interrupts of the same priority level as the one just processed.
5. 25
Table-5.7 continued …
S.No. Instruction Symbolic representation Explanation 5. 26
82. AJMP addr11 (PC) ← (PC) + 2 This instruction is used to unconditionally jump to a memory location
(PC)10-0 ← addr11 within the same 2 k block of program memory in which the instruction
following AJMP is stored. First, the PC is incremented by two to point to
the address of next instruction and then the 11-bit address given in the
instruction is moved to the lower 11-bit position of the PC. The 11-bit
address is the second byte of the instruction and upper 3 bits of opcode.
83. LJMP addr16 (PC) ← addr16 This instruction is used to unconditionally jump to any location in the
64 k memory space. Upon execution of this instruction, the 16-bit
address given in the instruction is moved to the PC, and so the controller
starts fetching the instruction from this new address.
84. SJMP offset (PC) ← (PC) + 2 This instruction is used to unconditionally transfer the program control
(PC) ← (PC) + offset to a new address obtained by adding the 8-bit signed offset to the
content of the PC. The offset will be in the range of −12810 to +12710.
85. JMP @A+DPTR (A) + (DPTR) = Address This instruction computes the address to which the program control
(PC) ← Address has to be transferred and loads this address in the PC. The address is
given by the sum of the signed 8-bit in the accumulator and the 16-bit
content of the DPTR.
86. JZ offset (PC) ← (PC) + 2 First, the content of the PC is incremented by two. Next, the content of
If (A) = 0 then the accumulator is checked. If the content of the accumulator is zero,
(PC) ← (PC) + offset then the 8-bit signed offset given in the instruction is added to the PC,
so that the program control branches to new address. If the accumulator
is not zero then PC is not modified, so that the next instruction of the
program is fetched and executed.
87. JNZ offset (PC) ← (PC) + 2 First, the content of the PC is incremented by two. Next the content of
If (A) ≠ 0 then the accumulator is checked, if the content of the accumulator is not
(PC) ← (PC) + offset zero, then the 8-bit signed offset given in the instruction is added to the
PC, so that the program control branches to a new address. If the
accumulator is zero, then PC is not modified so that the next instruction
of the program is fetched and executed.
MICROPROCESSORS AND MICROCONTROLLERS
Table-5.7 continued …
88. CJNE A,direct,offset (PC) ← (PC) + 3 First, the PC is incremented by three to point to the next instruction. The
direct = Address of internal RAM/SFR content of the accumulator and the internal RAM/SFR (whose address
If (A) ≠ (RAM/SFR) then is directly specified in the instruction) are compared. If the contents
(PC) ← (PC) + offset are not equal then the program control is transferred to a new address.
If (A) < (RAM/SFR) then, CF ← 1 The new address is the sum of the PC and offset given in the instruction.
If (A) > (RAM/SFR) then, CF ← 0 Also, if the content of the accumulator is less than RAM/SFR, then the
carry flag is set, otherwise it is cleared.
89. CJNE A,#data,offset (PC) ← (PC) + 3 This instruction is same as CJNE A,direct,offset except that the
If (A) ≠ data then comparison is performed with the immediate data given in the
(PC) ← (PC) + offset instruction and the accumulator.
If (A) < data then, CF ← 1
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051
90. CJNE Rn,#data,offset (PC) ← (PC) + 3 This instruction is same as CJNE A,direct,offset except that the
If (Rn) ≠ data then comparison is performed with the content of the Rn and the immediate
(PC) ← (PC) + offset data. The Rn can be any one of the eight registers of the currently
If (Rn) < data then, CF ← 1 selected register bank.
If (Rn) > data then, CF ← 0
91. CJNE @Ri,#data,offset (PC) ← (PC) + 3 This instruction is same as CJNE A,direct,offset except that the
(Ri) = Address of internal RAM comparison is performed between the RAM (whose address is specified
If (RAM) ≠ data then by Ri) and the immediate data given in the instruction. The register Ri
(PC) ← (PC) + offset can be either R0 or R1 of the currently selected register bank.
If (Ri) < data then, CF ← 1
If (Ri) > data then, CF ← 0
92. DJNZ Rn,offset (PC) ← (PC) + 2 First, the PC is incremented by two to point to the address of the next
(Rn) ← (Rn) − 1 instruction. Then, the content of register Rn is decremented by one.
If (Rn) ≠ 0 then If the content of Rn (after decrement) is not equal to zero then, the offset
(PC) ← (PC) + offset given in the instruction is added to the PC so that, the program control
branches to a new address. The register Rn can be any one of the
eight registers of the currently selected register bank.
5. 27
Table-5.7 continued …
S.No. Instruction Symbolic representation Explanation 5. 28
93. DJNZ direct,offset (PC) ← (PC) + 2 This instruction is same as DJNZ Rn, offset except that the content
direct = Address of RAM/SFR of the RAM/SFR is decremented and compared. The address of the
(RAM/SFR) ← (RAM/SFR) −1 RAM/SFR is directly specified in the instruction.
If (RAM/SFR) ≠ 0 then
(PC) ← (PC) + offset
94. NOP (PC) ← (PC) + 1 This instruction will not perform any operation, except that the PC is
incremented by one to point to the next instruction. Execution of NOP
will produce a delay of one machine cycle time and so, this instruction
can be used to create small delays in multiplies of machine cycle time.
96. CLR bit bit = Address of particular bit The particular bit of RAM/SFR whose address is specified in the
of RAM/SFR instruction is cleared to zero.
(bit) ← 0
100. CPL bit bit = Address of particular bit of RAM/SFR The particular bit of RAM/SFR whose address is
(bit) ← ~ (bit) specified in the instruction is complemented.
101. ANL C,bit bit = Address of particular bit of RAM/SFR The particular bit of RAM/SFR is logically ANDed with the
CF ← CF & (bit) carry flag and the result is stored in the carry flag.
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051
102. ANL C,/bit bit = Address of particular bit of RAM/SFR The complement of the particular bit of RAM/SFR is
(bit) ← ~ (bit) logically ANDed with the carry flag and the result is stored
CF ← CF & (bit) in the carry flag.
103. ORL C,bit bit = Address of particular bit of RAM/SFR The particular bit of RAM/SFR is logically ORed with the
CF ← CF | (bit) carry flag and the result is stored in the carry flag.
104. ORL C,/bit bit = Address of particular bit of RAM/SFR The complement of the particular bit of RAM/SFR is
(bit) ← ~ (bit) logically ORed with the carry flag and the result is stored in
CF ← CF | (bit) the carry flag.
105. MOV C,bit bit = Address of particular bit of RAM/SFR The particular bit of RAM/SFR is moved to the carry flag. The
CF ← (bit) address of the bit is directly given in the instruction.
106. MOV bit,C bit = Address of particular bit of RAM/SFR The carry flag is moved to a particular bit of RAM/SFR whose
(bit) ← CF address is directly specified in the instruction.
5. 29
Table-5.8 continued …
5. 30
S.No. Instruction Symbolic representation Explanation
107. JC offset (PC) ← (PC) + 2 The content of the PC is incremented by two to point to the
If CF = 1 then, (PC) ← (PC) + offset next instruction. Then the carry flag is checked. If the carry flag
is one, then the offset given in the instruction is added to the
PC so that the program control is transferred to a new address.
108. JNC offset (PC) ← (PC) + 2 Same as JC offset, except that the branching will take place
If CF = 0 then, (PC) ← (PC) + offset if the carry flag is zero.
109. JB bit, offset bit = Address of particular bit of RAM/SFR The content of the PC is incremented by three to point to the
(PC) ← (PC) + 3 next instruction. The particular bit of RAM/SFR is tested. If
If (bit) = 1, then (PC) ← (PC) + offset the bit is one, then the offset given in the instruction is added
to PC so that the program control is transferred to a new
address.
110. JNB bit, offset bit = Address of particular bit of RAM/SFR The content of the PC is incremented by three to point to the
(PC) ← (PC) + 3 next instruction. The particular bit of RAM/SFR is tested. If the
If (bit) = 0, then (PC) ← (PC) + offset bit is zero, then the offset given in the instruction is added to the
PC so that the program control is transferred to a new address.
111. JBC bit, offset bit = Address of particular bit of RAM/SFR The content of the PC is incremented by three to point to the
(PC) ← (PC) + 3 next instruction. The particular bit of RAM/SFR is tested. If
If (bit) = 1, then bit ← 0 ; (PC) ← (PC) +offset the bit is one, then clear the bit and the offset given in the
instruction is added to the PC, so that the program control is
transferred to a new address.
MICROPROCESSORS AND MICROCONTROLLERS
C HAPTER 5 I NSTRUCTION S ET O F 8031/8051 5. 31
5.6 How does an 8051 microcontroller differentiate between external program memory access and
data memory access ?
With external program memory, the controller can perform only read operations but with external
data memory, the controller can perform both read and write operations. For reading program
memory, the controller asserts PSEN as low, for reading data memory the controller asserts RD as
low, and for writing data memory the controller asserts WR as low.
5.7 What are the addressing modes available in an 8051 controller ?
The addressing modes available in the 8051 microcontroller are:
� Immediate addressing � Register indirect addressing
� Direct addressing � Implied addressing
� Register addressing � Relative addressing