Brp Microcontroller Module 2
Brp Microcontroller Module 2
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 3 categories namely 1-byte instruction, 2-byte instruction and 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 add A, R5
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.
Example:
MOV A, @R0
MOV @R1, A
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 (only 256 bytes of external memory can be accessed)
MOVX A, @DPTR (64KB of external memory can be accessed)
Example:
CLR b (where „b‟ is bit address 00h-7Fh or bit addressable SFR)
CPL b
SETB b
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.
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
A 20H A 45H
d) MOV A ,@Rp
A 20H A 45H
R0 40H R0 40H
40H 40H
45H 45H
Internal
Internal
RAM
RAM
Additional type of MOV instructions:
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.
a) MOVX A ,@Rp
A 20H A 45H
R1 60H R1 60H
External External
RAM RAM
b) MOVX A ,@DPTR
(A) ((DPTR))
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 index 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
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)
A 0BH
20H A 06H
45H
R4 R4
45H 20H
b) XCH A , addr
Operation Addressing mode Memory space
Exchange data bytes between the Direct addressing mode 2 bytes
accumulator and the memory
location (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 between the accumulator and Indirect addressing 1 byte
the memory location whose address is in register mode
Rp. (Rp can be either R0 or R1).
Example: XCH A , @R0 ; exchange the contents of the accumulator and the memory
location whose address is in register R0.
Before execution After execution
A 20H A 45H
R0 40H R0 40H
d) XCHD A ,@Rp
Operation Addressing mode Memory space
Exchange the lower nibble(lower 4 bits ) in the Indirect addressing 1 byte
accumulator along with the lower nibble of the mode
RAM memory location pointed by Rp. Rp can be
either R0 or R1. Upper nibbles are unaltered
A 6 2H A 6 5H
R0 40H R0 40H
40H 7 5H 40H 7 2H
Note:
a) PUSH addr
This instruction is used when data is to be placed on to the stack.
The SP register is incremented before storing data on to the stack so that the stack
grows up as data is stored.
(SP) (SP)+ 1
((SP)) (addr)
SP 07H SP 08H
empty
empty
08H 08H 45H TOS
stack
stack
b) POP addr
(addr) ((SP))
(SP) (SP)- 1
SP 08H SP 07H
empty
empty
stack stack
2) Arithmetic Instructions:
These instructions perform mathematical calculations on data as per the requirement.
The various arithmetic operations performed in 8051 are:
a) INC A
Operation Addressing mode Memory space
Increment the contents of Register addressing mode 1 byte
the accumulator by one.
(A) (A)+ 1
b) INC Rn
Operation Addressing mode Memory space
Increment the contents of Register addressing mode 1 byte
the register Rn ,of the
current register bank by one.
(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 pointed
by Rp by one. Rp can be
either R0 or R1.
(( Rp )) ((Rp))+ 1
Example: INC @R0 ;add one to the contents of the memory location whose address is in
R0.
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.
Before execution After execution DPTR = 0520H 0000 0101 0010 0000
+0001H 0000 0000 0000 0001
0520H 0521H 0521H 0000 0101 0010 0001
DPTR DPTR
DPH DPL DPH DPL 0 5 2 1
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
b) DEC Rn
Operation Addressing mode Memory space
Decrement the contents of Register addressing mode 1 byte
the register Rn ,of the
current register bank by one.
(Rn) (Rn) - 1
Ex: DEC R2
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 the Indirect addressing 1 byte
memory location whose address is mode
in register Rp by one. Rp can be
either R0 or R1.
Ex: DEC @ R1
Note:
There is no instruction for decrementing DPTR.
No Math flags (CY,AC , OV) are affected for increment and decrement instructions.
a) ADD A , Rn
Operation Addressing mode Memory space
Add the contents of the Register addressing mode 1 byte
register, Rn, to the contents
of the accumulator ; store
the result in the
accumulator.
Example: ADD A , R5
b) ADD A , addr
Operation Addressing mode Memory space
Add the contents of the Direct addressing mode 2 bytes
internal RAM address (addr)
to the contents of the
accumulator and store the
result in the accumulator.
05H 05H
30H 30H
c) ADD A , @Rp ;
Operation Addressing mode Memory space
Add the contents of the internal Indirect addressing mode 1 byte
RAM location ,whose address is in
register Rp to the contents of the
accumulator and store the result in
the accumulator.
d) ADD A , #D8 ;
Operation Addressing mode Memory space
Add the immediate data, D8 Immediate addressing mode 2 bytes
to the contents of the
accumulator and store the
result in the accumulator.
Example: ADD A , #05H
2AH 2FH
A A
1) ADDC A , Rn
CY AC
Before execution Before execution 0 1
A 27H A 30H A= 23H = 0010 0111
R1 09H R1 05H CY = 0
0000
0011 0000
ADDC A , addr;
Operation Addressing mode Memory space
Add the contents of the Direct addressing mode 2 bytes
internal RAM and the
contents of the carry flag to
the contents of the
accumulator and store the
result in accumulator.
ADDC A , #D8
ADDC A, #06H
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.
R1 35H R1 35H CY = 0
0000
0011 0000
06H 35 06H
35
Example: SUBB A , R4
R4 15H R4 15H
c) SUBB A ,#D8 ;
MUL AB
Example: MUL AB
A1H A0H
B B (higher order byte)
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 DBH A 0AH
A=DBH 0A(quotient)
B B B=15H 09(remainder)
15H 09H
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
d) ANL A, @Rp
Operation Addressing mode Memory space
Logically AND each bit of Indirect addressing mode 1 Byte
the accumulator with the
corresponding bit of the
immediate data and store
the result in accumulator.
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).
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.
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.
e) ORL addr,A
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 internal
RAM address (addr).
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
b) XRL A, addr
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
accumulator.
A 15H A EEH
A= 15H = 0001 0101
((R1)) = -05H = 1111 1011
35 05H
35 05H 1110 1110
c) 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.
d) 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.
e) 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.
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
-----------------------------------------------------------------------------------------------------
b) RLC 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
left. The Most Significant
Bit (MSB) becomes the
carry bit and the carry bit
becomes the Least
Significant Bit (LSB).
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).
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
-------------------------------------------------------------------------------------------------------
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 0 1 1 1 1 0 1 0
-------------------------------------------------------------------------------------------------------
CY
After execution: A= 3DH 0 0 0 1 1 1 1 0 1
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.
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
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
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
Operation Addressing mode Memory space
Set the contents of the Bit inherent addressing 1 byte
carry flag to 1. mode
Example: SETB C
CY x CY 1
j) SETB b
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
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
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.
3.1 Introduction
The 8051 executes the program sequentially by fetching the
execution
Program
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 Program
to bottom of the program memory.
memory
Jump operation
sequence
Program execution
JMP
Instructions not
executed
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
Since the PC contents are changed, the microcontroller is said to perform the jump
operation.
Since the relative address is added to the PC, the actual contents of PC after the
execution of the instruction depend on the present contents of the PC. Hence it is
called relative mode of addressing.
Since the relative address is only 8-bit
signed number, the range of the relative -128
address is -128 to +127. That is from
the current location the microcontroller Current location
can jump by 127 locations in the forward
direction and 128 locations in the 127
reverse direction. Therefore, the range
of jump is very limited and hence it is
called SHORT jump operation. This is
described in the adjacent diagram.
PC 2312H PC 2327H
The program memory of 64KB is divided into 32 parts and each part is called a PAGE. The
size of the each page is = 2KB. The instruction that performs the jump operation
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
.
. . .
. . .
. . .
. . .
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.
(PC) Addr
Before After
PC = 5678H PC=1235H
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.
(PC) A+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.
1) JC rel_addr
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.
Before After
PC 3000H PC 3020H If CY =1
PC 3000H If CY =0
2) 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.
Before After
PC 3000H PC 3020H If CY =0
PC 3000H If CY =1
3) 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.
NOTE: „b‟ can be bit addressable SFR or bit addressable RAM(20h-2fh)
Before After
PC 3000H
Case2 0 PC 3000H
P2.0
4) 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
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
5) 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
1) JZ rel_addr
If the accumulator contents are not zero, program execution continues with the next
instruction following the JZ instruction. This is a short jump instruction.
Ex: JZ 20H Before After
PC 3000H
Case1 PC
A 00H 3020H
2) JNZ rel_addr
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
(operand) (operand)-1
After decrementing the contents of the operand the microcontroller performs the jump
operation if the contents of the operand are not zero.
Before After
PC=3000H
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.
(A) N
If the two operands are not equal, then microcontroller performs jump operation by adding
the given relative address to the program counter.
If the two operands are equal, then the PC contents are not affected and the microcontroller
executes the next instruction in the sequence without performing jump operation.
Common practice when writing a large program is to divide the total task into small
independent tasks or modules that are written independently as subroutines and brought
together to build a large program.
In terms of efficiency, subroutines save memory space. For example, if we need a 100ms
delay five times in the main program, we can write a 100ms delay subroutine once and call
it five times.
Sequence of execution
Jumps to the
subroutine
Sequence of execution
CALL
Next instrn
Sequence of execution
Returns to
the main
program
RET
SUBROUTINE
MAIN PROGRAM
Absolute CALL:
Syntax: ACALL abs_addr
This call instruction calls a subroutine which is in the same page as that of the calling
program. The given abs_addr is the 11-bit address of the subroutine. The instruction is
executed as follows.
The content of the program counter which is the return address of the main program is
preserved on the stack by performing PUSH operation. After preserving the contents of the
PC, the 11-bit address is transferred to the PC. The most significant 5-bits of the PC remain
unchanged.
(SP) (SP)+1
((SP)) (PCL)
(SP) (SP)+1
((SP)) (PCH)
(PC) abs_addr
76H 00H
76H
XX 75H XX
SP 75H
XX XX
74H 74H
Stack memory Stack memory
LONG CALL:
This call instruction calls a subroutine anywhere in the memory. The given addr is the
16-bit address of the memory where the subroutine is stored. The instruction is executed as
follows.
The content of the program counter which is the return address of the main program is
preserved on the stack by performing PUSH operation.
(SP) (SP)+1
((SP)) (PCL)
(SP) (SP)+1
((SP)) (PCH)
After preserving the contents of the PC, the 16-bit address is transferred to the PC.
(PC) addr
Before After
Ex: LCALL 0768H (SP)= 77H
(SP)= 75H (PC)= 0768H
(PC)= 3800H
76H 00H
76H
XX 75H XX
SP 75H
XX XX
74H 74H
Stack memory Stack memory
At the end of the subroutine, the control of program execution is transferred to the main
program. For this RET instruction is used. This RET instruction will not specify the address
of the main program to which the control must be transferred.
Syntax: RET
This is an instruction to transfer the control of the program execution from the subroutine to
the main program. The instruction doesn‟t specify the address of the main program to which
the control must be transferred. The instruction is executed as follows:
The address of the main program to which the control must be transferred is called “return
address”. This return address must be present in the stack memory.
The contents of the top of the stack memory are transferred to the program counter PC.
With this the microcontroller automatically jumps to the main program.
(PCH) ((SP))
(SP) (SP)-1
(PCL) ((SP))
(SP) (SP)-1
Ex: RET
Before After
(SP)= 75H
(SP)= 77H (PC)= 3800H
(PC)= 0768H
The list of data transfer instructions with the internal data memory
PUSH addr
ADDITION SUBTRACTION POP addr
Instructions Instructions
SUBB A, Rn
ADD A, Rn SUBB A, addr BIT LEVEL
SUBB A, #D8 instructions
ADD A, addr SUBB A, @RP MOV b, C
MOV C, b ROTATE
ADD A, #D8 SETB b instructions
DECREMENT
SETB C RL A
Instructions
ADD A, @RP CLR b RLC A
DEC A
CLR C RR A
DEC Rn
ADDC A, Rn CPL b RRC A
DEC addr
CPL C SWAP A
ADDC A, addr DEC @RP
NOTE: No ANL C, b
Instruction for ANL C, /b
ADD A, #D8
Decrementing ORL C, b
ADDC A, @RP DPTR ORL C, /b
DA A
Increment
instructions INCREMENT
Byte level OR Byte level XOR Multiplication
INC A Instructions
instructions Instructions
INC addr INC A & Division
ORL A, Rn XRL A, Rn Instructions
INC DPTR INC Rn
ORL A, addr XRL A, addr MUL AB
INC @RP INC addr
ORL A, #D8 XRL A, #D8 DIV AB
INC Rn INC @RP
ORL A, @RP XRL A, @RP
INC DPTR
ORL addr, A XRL addr, A
ORL addr, #D8 XRL addr, #D8 Byte level AND
instructions
Clear/ complement ANL A, Rn
Multiplication Of Accumulator
Multiplication& ANL A, addr
CLR A ANL A, #D8
Division CPL A
Instructions ANL A, @RP
MUL AB ANL addr, A
DIV AB ANL addr, #D8
Unconditional Subroutine
Decrement &
Jump instructions Instructions
Jump instructions
SJMP rel_addr ACALL abs_addr
DJNZ addr, rel_addr
AJMP abs_addr LCALL addr_16
DJNZ Rn, rel_addr
L JMP addr_16 RET
JMP @A+DPTR RETI
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
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 ALP to add two input data’s of 16 bit result in three addressing modes. (6 marks )
Dec 09/ jan 10
1. Write an ALP in 8051 to find the largest number among the twelve 8bit numbers stored in the
internal RAM.---- (7 marks )
2. Write an ALP to perform the following operation:
Z= (X1+Y1)*(X2+Y2) where X1, X2, Y1, Y2 are the 8-bit hexadecimal numbers stored in RAM
locations. Write a subroutine for the additions and assume that each addition results in 8-bit
answer.----(7 marks)
May/Jun 2010
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.
2. Write the result of after the execution of each of the following instruction –(8)
Dec 2011
1. Write a program to set the carry flag to ‘1’, if the number in register A is even other reset the
carry flag. -- - (4)
2. 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)
3. What does the folloing program do? What is the result in accumulator
Start: MOV A, R3
RLA
ANL A, #0AAH
PUSH ACC
MOV A, R3
RRA
ANL A, #55H
MOV R3, A
POP ACC
ORL 03H, A
SJMP $
END
4. 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
2. Explain the different ranges for JMP instruction available in 8051 microcontroller.--- ( 8
marks)
Dec 08/ Jan 09
3. 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
- 2050H FACT: |
- |
- |
- |
2000H: ACALL FACT |
4. (i)Mention the advantages
- of using the subroutine ------( 6 marks ) |
(ii) Give the address
- range of instructions SJMP, AJMP, LJMP RET
(iii) Mention the differences
- between RET and RETI instructions