MC MODULE_2 NOTES
MC MODULE_2 NOTES
• The label allows the program to access a line of code by a name. All labels must
start with an alphabetic character and end with a colon.
Example
BACK: add A, B ; the word BACK is a label.
• The comment field is used to describe the program. With the help of the comment
lines the programmer can easily interpret what each instruction does. Comment field
begins with a semicolon. Comments can be written at the end of a line or on a new
line. Comments are ignored by the assembler and are not executed.
Example
ADD A, R5 ; add contents of register R5 to accumulator.
Length of an instruction:
The instructions written in assembly language are translated into a machine code known as
opcode (operational code) to be executed by the CPU. Depending upon the number of
bytes present in each instruction in the machine language, the instructions are classified
into three categories: is translated to, we have
1-byte instruction
SAI VIDYA INSTITUTE OF TECHNOLOGY 1
2-byte instruction
3-byte instruction
• 1-byte instruction: An instruction which has only the mnemonic and the operands
will result in a 1-byte instruction.
Example: mov A, B
Any instruction will contain an opcode that specifies the nature of operation to be performed
and a set of operands on which the required operation is performed. The instruction can
have two operands called destination operand and a source operand.
The various ways by which the destination and source operand/s of an instruction
are specified are called addressing modes.
Note: In case of external data memory access, either R0 OR R1(256 bytes)/ DPTR(64KB) is
used as memory pointer register.
Example:
• MOVX A, @R0
• MOVX A, @DPTR
Example:
• SJMP 50H
• JC 20H
2. 3 INSTRUCTION SET
Instruction set of the 8051 microcontroller can be classified, based on the function they
perform into the following 5 categories.
These instructions deal with transferring (copying) the data from source to destination.
Where the destination and the source can either be a register or a memory location.
The various forms of Move instruction are:
a) MOV A ,Rn
(A) (Rn)
A 20H A 45H
R2 45H R2 45H
b) MOV A ,addr
(A) (addr)
Example: MOV A , 30H ; copy the contents of RAM location with address 30H into the
accumulator.
SAI VIDYA INSTITUTE OF TECHNOLOGY 5
A 20H A 45H
Internal
Internal
RAM
RAM
c) MOV A ,#D8
(A) D8
Example: MOV A , #45H ; copy the 8 bit data (45H) into the accumulator
20H 45H
d) MOV A ,@Rp
(A) ((Rp))
SAI VIDYA INSTITUTE OF TECHNOLOGY 6
Example: MOV A ,@R0 ;
A 20H A 45H
R0 40H R0 40H
40H 40H
45H 45H
Internal
Internal
RAM
RAM
RAM and ROM can be expanded by adding external memory chips to the 8051
microcontroller. The external memory can be as large as 64KB for each of the RAM and
ROM memory areas. Instructions that access this external memory always use indirect
addressing mode.
SAI VIDYA INSTITUTE OF TECHNOLOGY 7
a) MOVX A ,@Rp
(A) ((Rp)
A 20H A 45H
60H R1
60H
R1 45H 45H
60H R1 60H
External External
RAM RAM
b) MOVX A ,@DPTR
(A) ((DPTR))
SAI VIDYA INSTITUTE OF TECHNOLOGY 8
Example: MOVX A ,@DPTR ;
Before execution After execution
A A
20H 45H
MOVC A, @A+PC
MOVC A, @A+DPTR
The data can also be stored in the program ROM. Access to this data is made possible by
using indirect addressing and the accumulator in conjunction with the PC or the DPTR as
shown.
a) MOVC A , @A+DPTR
A 20H A 45H
ROM ROM
SAI VIDYA INSTITUTE OF TECHNOLOGY 9
b) MOVC A ,@A+PC
A A 45H
20H
PC PC 5000H
5000H
ROM ROM
➢ Data Exchanges:
Various forms of MOV, PUSH and POP instructions transfer the data in a single direction i,e.
from source to destination, leaving the source unaltered. Exchange instructions move the
data in two directions i,e. from source to destination and vice-versa.
a) XCH A ,Rn
b) XCH A ,addr
c) XCH A ,@Rp
d) XCHD A ,@Rp (exchange digits)
a) XCH A ,Rn
Operation Addressing mode Memory space
Exchange data bytes Register addressing mode 1 byte
between the accumulator
and the register Rn(of
current register bank)
SAI VIDYA INSTITUTE OF TECHNOLOGY 10
Example: XCH A , R4 ; exchange bytes between register R4 and the Accumulator.
A 0BH
20H A 06H
45H
R4 R4
45H 20H
b) XCH A , addr
Operation Addressing mode Memory space
Exchange data bytes Direct addressing mode 2 bytes
between the accumulator
and the direct memory
address(addr)
Example: XCH A, 30H; exchange the contents of the Accumulator and the memory
location(70H).
A 20H A 45H
c) XCH A ,@Rp
Operation Addressing mode Memory space
Exchange data bytes Indirect addressing mode 1 byte
between the accumulator
and the memory location
whose address is in register
Rp. Rp can be either R0 or
R1.
SAI VIDYA INSTITUTE OF TECHNOLOGY 11
Example: XCH A , @R0 ; exchange the contents of the accumulator and the memory
location whose address is in register R0.
A 20H A 45H
R0 40H R0 40H
A 6 2H A 6 5H
R0 40H R0 40H
40H 7 5H 40H 7 2H
Arithmetic Instructions:
INC destination
DEC destination
a) INC A
Operation Addressing mode Memory space
Increment the contents of Register addressing mode 1 byte
the accumulator by one.
(A) (A)+ 1
(Rn) (Rn)+ 1
c) INC addr
Operation Addressing mode Memory space
Increment the contents of Direct addressing mode 2 bytes
the direct address(addr) by
one.
(addr) (addr)+ 1
Example: INC 30H ; add one to the contents of the address 30H.
d) INC @Rp
Operation Addressing mode Memory space
Increment the contents of Indirect addressing mode 1 byte
the memory location whose
address is in Rp by one. Rp
can be either R0 or R1.
(( Rp )) ((Rp))+ 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 14
Example: INC @R0 ;add one to the contents of the memory location whose address is in
R0.
Before execution After execution
R0 40H R0 40H
((R0))= 29H 0010 1001
+ 01H 0000 0001
40H 29H 40H 2AH 2AH 0010 1010
2 A
e) INC DPTR
Operation Addressing mode Memory space
Increment the contents of Register addressing mode 1 byte
the data pointer register by
one.
(DPTR) (DPTR) + 1
Example: INC DPTR ;add one to the contents of the DPTR register.
Decrement instructions
a) DEC A
Operation Addressing mode Memory space
Decrement the contents of Register addressing mode 1 byte
the accumulator by one.
(A) (A) -1
(Rn) (Rn) - 1
Ex: DEC R2
R2 20H R2 1FH
c) DEC addr
Operation Addressing mode Memory space
Decrement the contents of Direct addressing mode 2 bytes
the direct address(addr) by
one.
(addr) (addr) - 1
d) DEC @Rp
Operation Addressing mode Memory space
Decrement the contents of Indirect addressing mode 1 byte
the memory location whose
address is in register Rp by
one. Rp can be either R0 or
R1.
((Rp)) ((Rp)) - 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 16
Ex: DEC @ R1
R1 30H R1 30H
Note:
(Rn) (Rn)+ 1
Example: ADD A , R5
Before execution After execution A=2AH 0010 1010
R5=05H + 0000 0101
A 2AH A 2FH 2FH 0 0010 1111
CY 2 F
R5 05H R5 05H
(addr) (addr)+ 1
05H 05H
30H 30H
((Rp)) ( (Rp))+ 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 18
Example: ADD A , @R1
Before execution After execution
05H 05H
40H R1 40H
d) ADD A , #D8 ;
2AH 2FH
A A
R1 05H R1 05H CY = 1
0000
0011 0000
f) ADDC A , addr
g) ADDC A , #D8
The given data along with the carry is added with data present in A register and the
result is placed in the register A.
This instruction has immediate mode of addressing.
This is a two byte instruction. The first byte represents the opcode and the second
byte represents the given data
SAI VIDYA INSTITUTE OF TECHNOLOGY 20
Ex: ADC A, #05H
h) ADDC A , @Rp;
Operation: add the contents of the interanal RAM along with the CARRY flag to the contents of the
accumulator and store the result in the accumumlactor. The address of the RAM is in the register
Rp. The register Rp can be either R0 or R1.
This instruction has indirect mode of addressing and the length of the instruction is one byte.
R1 35H R1 35H CY = 1
0000 0010 0000
0011 0000
05H 35 05H
35
R4 15H R4 15H
MUL AB
Example: MUL AB
A= FF
B= A1
A0 5FH
❖ 8051 supports only byte –over- byte division. Division operation uses registers
A(accumulator) and B as both source and destination operands for the operation.
DIV AB
Operation Addressing mode Memory space
Divide the contents of the Register addressing mode 1 byte
register A by the contents
of the register B. Store the
integer part of the
quotient in register A and
the remainder in register
B. Flags affected: CY,OV
NOTE:
• The carry flag is always cleared.
• The overflow flag is cleared to 0 unless register B holds 00H before division,
indicating that division by zero is undefined.
• Note: REMAINDER =(DIVIDEND – QUOTIENT *DIVISOR).
Example: DIV AB
Before execution After execution
A A
DBH 0AH
B B A=DBH 0A(quotient)
15H 09H B=15H 09(remainder)
SAI VIDYA INSTITUTE OF TECHNOLOGY 24
BCD ADDITION
Working of DA A instruction:
• The microcontroller checks the contents of accumulator. If the lower 4bits of
accumulator is greater than ‘9’ or if auxiliary carry is set then ‘06’ is added to
accumulator and then the auxiliary carry is set.
(A) (A)+06
(AC) 1
• After the above modification, if the higher 4 bits of accumulator is greater
than ‘9’ or carry flag is set, then ‘60’ is added to accumulator and carry flag
is set.
(A) (A) +60
(CY) 1
DA instruction works only after the ADD or ADDC instruction and not after the INC instruction.
3) Logical instructions:
The various logical operations performed by the 8051 microcontroller are the OR, AND, XOR
and the NOT operation .
AND operation:
a) ANL A, Rn
Operation Addressing mode Memory space
Logically AND each bit of Register addressing mode 1 Byte
the accumulator with the
corresponding bit of
register Rn, of the current
register bank and store
the result in accumulator.
Example: ANL A, R3
0BH 0BH
R3 R3
b) ANL A, addr
Operation Addressing mode Memory space
Logically AND each bit of Direct addressing mode 2 Bytes
the accumulator with the
corresponding bit of the
contents of the internal
RAM address(addr) and
store the result in
accumulator.
c) ANL A, #D8
e) ANL addr,A
Operation Addressing mode Memory space
Logically AND each bit of Direct addressing mode 2 Bytes
the accumulator with the
corresponding bit of the
contents of the internal
RAM address (addr) and
store the result in internal
RAM address (addr).
1) OR operation:
The general form of OR instruction is: ORL destination, source
The contents of the destination and the source are ORed and the result is stored in the
destination. The destination is normally the accumulator; the source operand can be in
memory, Register or an immediate data.
a) ORL A, Rn
Operation Addressing mode Memory space
Logically OR each bit of Register addressing mode 1 byte
accumulator with the
corresponding bit of
register Rn, of the current
register bank and store
the result in accumulator.
SAI VIDYA INSTITUTE OF TECHNOLOGY 27
Example: ORL A, R3 ; Logically OR each bit of accumulator with corresponding bit of
data in register R3, of current register bank and store the result in accumulator.
R3 R3
35H 37H
b) ORL A, addr
Operation Addressing mode Memory space
Logically OR each bit of Direct addressing mode 2 bytes
accumulator with the
corresponding bit of the
contents of the internal
RAM address(addr) and
store the result in
accumulator.
c) ORL A,#D8
Operation Addressing mode Memory space
Logically OR each bit of Immediate addressing 2 bytes
accumulator with the mode
corresponding bit of
immediate data, D8 and
store the result in
accumulator.
d) ORL A, @Rp
Operation Addressing mode Memory space
Logically OR each bit of Indirect addressing mode 1 byte
accumulator with the
corresponding bit of the
contents of the internal
RAM address contained in
register Rp and store the
result in accumulator.
SAI VIDYA INSTITUTE OF TECHNOLOGY 28
e) ORL addr,A
Operation Addressing mode Memory space
Logically OR each bit of Direct 2 bytes
accumulator with the addressing
corresponding bit of the mode
contents of the internal
RAM address (addr) and
store the result in internal
RAM address (addr).
2) XOR operation:
a) XRL A, Rn
Operation Addressing mode Memory space
Logically XOR each bit of Register addressing mode 1 byte
accumulator with the
corresponding bit of
register Rn, of the current
register bank and store
the result in accumulator.
Example: XRL A, R3
R3 44H R3 44H
SAI VIDYA INSTITUTE OF TECHNOLOGY 29
XRL A, addr
A 15H A EEH
A= 15H = 0001 0101
((R1)) = -05H = 1111 1011
35 05H
35 05H 1110 1110
b) XRL A, #D8
Operation Addressing mode Memory space
Logically XOR each bit of Immediate addressing 2 bytes
accumulator with the mode
corresponding bit of
immediate data, D8 and
store the result in
accumulator.
c) XRL A, @Rp
Operation Addressing mode Memory space
Logically XOR each bit of Indirect addressing mode 1 byte
accumulator with the
corresponding bit of
contents of the internal
RAM address contained in
register Rp and store the
result in accumulator.
SAI VIDYA INSTITUTE OF TECHNOLOGY 30
d) XRL addr,A
Operation Addressing mode Memory space
Logically XOR each bit of Direct addressing mode 2 bytes
accumulator with the
corresponding bit of the
contents of the internal
RAM address (addr) and
store the result in internal
RAM address (addr).
Clear instruction:
CLR A
Example: CLR A
A 33H A 00H
Complement instruction:
CPL A
Operation Addressing mode Memory space
Complement each bit of Register addressing mode 1 byte
the accumulator, i.e. every
1 becomes a 0 and vice-
versa.
SAI VIDYA INSTITUTE OF TECHNOLOGY 31
Example: CPL A
There are rotate opcodes that operate only on a byte, or a byte and the carry flag to
allow 8 bit(only a byte) and 9 bit(a byte and a carry flag) shift register operations.
Swap instructions are used to exchange the lower and higher nibbles in a byte.
NOTE: The rotate and the swap instructions are limited to the Accumulator.
The various rotate instructions are:
a) RL A
b) RLC A
c) RR A
d) RRC A
e) SWAP A
a) RL A
Operation Addressing mode Memory space
Rotate the contents of the Register addressing mode 1 byte
accumulator 1 bit position
to the left. The Most
Significant Bit (MSB)
becomes the Least
Significant Bit (LSB).
Before execution
A D7 D6 D5 D4 D3 D2 D1 D0
After execution
A D6 D5 D4 D3 D2 D1 D0 D7
Example: RL A
-----------------------------------------------------------------------------------------------------
Before execution
CY D7 D6 D5 D4 D3 D2 D1 D0
After execution
D7 D6 D5 D4 D3 D2 D1 D0 CY
Example: RLC A
CY
Before execution: A= B5H 1 0 1 1 0 1 0 1
1
-------------------------------------------------------------------------------------------------------
CY
After execution: A= 6BH 0 1 1 0 1 0 1 1
1
c) RR A
Operation Addressing mode Memory space
Rotate the contents of the Register addressing mode 1 byte
accumulator 1 bit position
to the right. The Least
Significant Bit (LSB)
becomes the Most
Significant Bit (MSB).
SAI VIDYA INSTITUTE OF TECHNOLOGY 33
Before execution
A D7 D6 D5 D4 D3 D2 D1 D0
After execution
A D0 D7 D6 D5 D4 D3 D2 D1
Example: RR A
-------------------------------------------------------------------------------------------------------
d) RRC A
Operation Addressing mode Memory space
Rotate the contents of the Register addressing mode 1 byte
accumulator and the carry bit
1 bit position to the right. The
Least Significant Bit (LSB)
becomes the carry bit and the
carry bit becomes the Most
Significant Bit (MSB).
Before execution
D7 D6 D5 D4 D3 D2 D1 D0 C
After execution CY
C D7 D6 D5 D4 D3 D2 D1 D0
Example: RRC A
CY
Before execution: A= 7AH 0 1 1 1 1 0 1 0
0
-------------------------------------------------------------------------------------------------------
CY
After execution: A= 3DH 0 0 0 1 1 1 1 0 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 34
e) SWAP A
Operation Addressing mode Memory space
Exchange the lower and Register addressing mode 1 byte
higher nibbles of the
accumulator.
Before execution
D7 D6 D5 D4 D3 D2 D1 D0
higher nibble lower nibble
After execution
D3 D2 D1 D0 D7 D6 D5 D4
higher nibble lower nibble
a) ANL C, b
Operation Addressing mode Memory space
Logically AND the contents Bit addressing mode 2 byte
of the carry flag, C and
the addressed bit, b and
store the result in carry
flag ,C.
SAI VIDYA INSTITUTE OF TECHNOLOGY 35
Example: ANL C , P2.3
Before execution after execution
CY= (CY) AND (P2.3)
CY 1 CY 0 1 AND 0
CY = 0
Port 2 Port 2
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0
P2.3 P2.3
b) ANL C, /b
Operation Addressing mode Memory space
Logically AND the contents Bit addressing mode 2 byte
of the carry flag, C and
the complement of the
addressed bit, b and store
the result in carry flag ,C.
7 6 54 3 2 10 7 6 5 4 3 210
20H 1 20H 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 36
c) ORL C, b
Operation Addressing mode Memory space
Logically OR the contents Bit addressing mode 2 byte
of the carry flag, C and
the addressed bit, b and
store the result in carry
flag ,C.
Port 3 Port 3
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0
P3.3 P3.3
d) ORL C, /b
7 6 54 3 2 10 7 6 5 4 3 210
20H 1 20H 1
SAI VIDYA INSTITUTE OF TECHNOLOGY 37
e) CPL C
Operation Addressing mode Memory space
Complement the contents Bit inherent addressing 1 byte
of the carry flag, C ,i.e. mode
change 1 to 0 and vice
versa.
Example: CPL C
CY 1 CY 0
--------------------------------------------------------
Case(2)
0 1
CY CY
f) CPL b
P1.2 P1.2
---------------------------------------------------------------------------
Case(2)
Port 1 Port 1
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 1
P1.2 P1.2
g) CLR C
Example: CLR C
CY x CY 0
h) CLR b
Port 0 Port 0
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
x 0
P0.2 P0.2
i) SETB C
Example: SETB C
CY x CY 1
j) SETB b
Example: SETB P1.5 ; set the contents of the bit 5 of Port1 (P1.5) to 1.
Port 1 Port 1
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
x 1
P1.5 P1.5
SAI VIDYA INSTITUTE OF TECHNOLOGY 40
k) MOV C, b
Operation Addressing mode Memory space
Copy the contents of the Bit addressing mode 2 bytes
addressed bit(b), to the
carry flag,C.
Example: MOV C, P2.4 ; copy the contents of bit 4 of port 2(P2.4) to the carry flag, C.
CY 1 CY 0
Port 2 Port 2
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0
P2.4 P2.4
l) MOV b, C
Example: MOV P0.4, C ; copy the contents of the carry flag,C to bit 4 of Port 0(P0.4).
CY 1 CY 1
Port 0 Port 0
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 1
P0.4 P0.4
Note:
• No flags , other than the carry flag are affected unless the flag is an addressed bit .
• If the destination is a port bit ,the SFR latch bit is affected , not the pin.
• ANL C, /b and ORL C,/b do not alter the addressed bit b.
SAI VIDYA INSTITUTE OF TECHNOLOGY 41
Introduction
The 8051 executes the program sequentially by fetching the instructions from the memory.
The contents of the program counter PC are used as the memory address from where the
instruction is to be fetched. While fetching the instruction from the memory, the PC contents
are automatically incremented so that the PC always contains the memory address of
the next instruction to be fetched. Thus, the microcontroller executes the instructions
sequentially. This concept is demonstrated as shown in the adjacent diagram. The program
is executed from top to bottom of the program memory.
The jump instructions are used to change this sequence of program execution. Since the
microcontroller fetches the instruction from the memory location whose address is present
in the register PC, to change this sequence of program execution the contents of PC must
be altered. Therefore, any jump instruction primarily does only one operation of
changing the contents of PC.
As shown in the adjacent diagram any part of the program or a set of instructions can be
omitted in the execution by using jump instructions which makes the control of program
execution to be diverted to any part of the program.
To realize the jump operation the programmer has to write the JMP instruction in the
program at a point where deviation from the normal sequence of program execution is
required.
sequence
JMP Program execution
Instructions not
executed
Program
Program
memory
memory
SAI VIDYA INSTITUTE OF TECHNOLOGY 42
The various jump instructions are as follows:
Byte level
Conditional
Jump instructions Bit level
Absolute jump
Long jump
Here the rel_addr is an 8-bit signed number. When the above instruction is executed, the
given 8-bit rel_addr is added to the contents of PC
PC 2312H PC 2327H
+
SAI VIDYA INSTITUTE OF TECHNOLOGY 43
The program memory of 64KB is divided into 32 parts and each part is called a PAGE. The
64𝐾𝐵
size of the each page is = 2KB. The instruction that performs the jump operation
32
within the same page is called absolute jump operation. The various pages along with the
page addresses are as shown in the following figure.
PAGE 31
.
. .
PAGE 0
PAGE 1
.
. . .
. . .
. . .
. . .
0000H 0800H F800H
The absolute jump operation performs the branch operation within the same page. For any
page the address boundary is such that the most significant 5-bits of the address will be the
same and the least significant 11-bits of the address will change. Therefore, the absolute
jump operation will alter only the least significant 11-bits of the program counter PC.
The jump operation is performed by loading the PC with the given address.
(PC) Addr
This instruction performs long jump operation by loading the PC with a 16-bit address. The
address is computed by adding the contents of A and DPTR.
Before After
(PC) = 5678H (PC) = 2188H
(A) =65H (A) = 65H
(DPTR) = 2123H (DPTR) = 2123H
Conditional jump instructions are used to perform jump operation based on a user specified
condition. Every conditional jump instruction specifies a condition.
• If the condition is false, the PC contents are not modified and the microcontroller will
execute the next instruction in the sequence as usual or the microcontroller will not
perform the jump operation.
SAI VIDYA INSTITUTE OF TECHNOLOGY 45
Description: JC instruction will branch to the address indicated by rel_addr if the Carry bit is
set.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
If the Carry bit is not set program execution continues with the instruction following the JC
instruction. This is a short jump instruction.
Ex: JC 20H
Before After
PC 3000H PC 3020H If CY =1
PC 3000H If CY =0
JNC rel_addr
Description: JNC instruction will branch to the address indicated by rel_addr if the Carry bit
is reset.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
If the Carry bit is set program execution continues with the instruction following the JNC
instruction. This is a short jump instruction.
SAI VIDYA INSTITUTE OF TECHNOLOGY 46
Before After
PC 3000H PC 3020H If CY =0
PC 3000H If CY =1
JB b, rel_addr
Description: JB instruction will branch to the address indicated by rel_addr if the specified
bit is set. In the above instruction the b is the bit address.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
If the specified bit is reset program execution continues with the instruction following the JB
instruction. This is a short jump instruction.
Before After
PC 3000H
Case1 1 PC
P2.0 3020H
JNB b, rel_addr
Description: JNB instruction will branch to the address indicated by rel_addr if the specified
bit is reset. In the above instruction the b is the bit address.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
SAI VIDYA INSTITUTE OF TECHNOLOGY 47
If the specified bit is set program execution continues with the instruction following the JNB
instruction. This is a short jump instruction.
Before After
PC 3000H
Case1 PC
P2.0 1 3000H
JBC b, rel_addr
Description: JBC instruction will branch to the address indicated by rel_addr if the specified
bit is set. After performing the jump operation, the specified bit is reset. In the above
instruction the b is the bit address.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
If the specified bit is reset program execution continues with the instruction following the
JBC instruction. This is a short jump instruction.
Before After
PC 3000H
Case1 PC P2.0 0
P2.0 1 3020H
Syntax: JZ rel_addr
PC 3000H
Case1 PC
A 00H 3020H
Case2 A PC
05H 3000H
Description: JNZ instruction will branch to the address indicated by rel_addr if the
accumulator contents are not zero.
The jump operation is performed by adding the rel_addr to the contents of PC.
(PC) (PC)+rel_addr
If the accumulator contents are zero, program execution continues with the instruction
following the JNZ instruction. This is a short jump instruction.
Before After
PC 3000H
Case1 PC
A 00H 3000H
Description: The contents of the specified operand are decremented by 1 and the result is
placed in the same operand. Ex1 : DJNZ R7, 50H
(opr) (opr)-1
Before After
PC=3000H
After decrementing the contents of the operand the microcontroller performs the jump
operation if the contents of the operand are not zero.
The first operand is compared with the second operand. The comparison is done by
subtracting the second operand from the first operand. But the result is not placed in any of
the two operands and only flags are affected.
If the two operands are not equal, then microcontroller performs jump operation by adding
the given relative address to the program counter.
The list of data transfer instructions with the internal data memory
ADDITION
Instructions
ADD A, Rn
ADD A, addr
ADD A, #D8
ADD A, @RP
ADDC A, Rn
ADDC A, addr
ADD A, #D8
SAI VIDYA INSTITUTE OF TECHNOLOGY 52
Multiplication
& Division
Instructions
MUL AB
DIV AB
Unconditional
Jump instructions
SJMP rel_addr
SAI VIDYA INSTITUTE OF TECHNOLOGY 53
AJMP abs_addr
L JMP addr_16 Decrement &
JMP @A+DPTR Jump instructions
DJNZ addr, rel_addr
DJNZ Rn, rel_addr
Conditional
Jump instructions Compare &
JC rel_addr Jump instructions
JNC rel_addr CJNE A, addr, rel_addr
JB b, rel_addr CJNE A, #D8, rel_addr
JNB b, rel_addr CJNE @RP, #D8, rel_addr
JBC b, rel_addr CJNE Rn, A, rel_addr
JZ rel_addr
JNZ rel_addr
Subroutine
Instructions
ACALL abs_addr
LCALL addr_16
RET
RETI
VTU QUESTIONS:
June-July 2008
1. Write a program to put the number 34H in register R4, R5, R6 using three different addressing
modes –(6 marks)
2. Write a program to swap the contents of R7 and R6 in register block 0in four different ways ---( 8
marks)
3. Write a program to find the address of first two internal RAM locations between 20h and 60h
that contains consecutive numbers. If so set the carry flag to 1 otherwise clear the carry flag,
using a subroutine.----(7 marks )
June- July 2009
1. Write an assembly program in 8051 to add two 16 bit numbers stored in external memory
after the addition the result must be stored in internal data memory.
1. Find the address of first two internal RAM locations between the addresses 20H and 40H which
contain the consecutive members. If so set the carry flag otherwise reset the carry flag - - (6)
2. write a program to convert the BCD number 29 in to ASCII value and display the result on port 1
and port 2 – (6)
Dec 2011
1. Write a program to find the sum of 20 bytes of data stored in an array of external RAM starting
with address 2000H. store the 16-bit result at the end of the array.
2. Write the main program to find the value of P = N! / R!. using a subroutine find the value of
factorial of given number. The values of N and R are stored in locations 30H and 31H. the result
P must be stored in location 32H.
June-July 2008
MOV A, #+96
MOV R1, #+70
ADD A, R1
2. What are the steps executed by 8051 for the following instruction:
RET AJMP addr
May/Jun 2010
(a) MOVC A,@A+DPTR, (b) MUL AB, (C) MOV B, #0FFH, (d) SUBB A, 45H
Explain any two data transfer and any one arithmetic instruction of 8051. Give an example for
each ----(6)
Dec 2010
1. Explain the addressing modes of 8051 and give an example for each – (7)
2. Explain the different conditional and unconditional JMP instruction of 8051 and specify the
range of each instruction. - - - (8)
3. Explain the following instructions of 8051
(a) XCHD A, @Ri, (b) MOV C, A , (c) SWAP A, (d) RL A, (e) MUL AB (f) DA A – (9)
Dec 2011