Instruction Set of 8051 Microcontroller: Gaurav Sharma
Instruction Set of 8051 Microcontroller: Gaurav Sharma
Gaurav Sharma
I/O port usage in 8051
• When RESET all ports are configured as input ports.
• To configure any port as an output port, 0 is written to it and to configure it
again as an input port, 1 is written to it.
• Example 1: To configure port 0 as an input port, receive a data and send it to
port 1.
MOV A, #0FFH
MOV P0, A
BACK: MOV A, P0
MOV P1, A
SJMP BACK
• Example 2: It will alternately send 55 and AA to port 0 repeatedly.
BACK: MOV A, #55H
MOV P0, A
ACALL DELAY
MOV A, #AAH
MOV P0, A
ACALL DELAY
SJMP BACK
• Example 3: It will alternately send 55 and AA to port 1 repeatedly.
MOV A, #55H
BACK: MOV P1, A
ACALL DELAY
CPL A
SJMP BACK
• Example 4: Configure port 1 as an input port and save the received data in
R5 and R7.
MOV A, #0FFH
MOV P1, A
MOV A, P1
MOV R5, A
ACALL DELAY
MOV A, P1
MOV R7, A
8051 Instructions
• Data transfer instructions
• Arithmetic instructions
• Logical instructions
• Branch instructions
• Subroutine instructions
• Bit manipulation instructions
Data Transfer Instructions
• Move the contents of a register Rn to A
– MOV A,R2
– MOV A,R7
Exchange instructions
The content of source ie register, direct memory or indirect memory
will be exchanged with the contents of destination ie accumulator.
XCH A,R3
XCH A,@R1
XCH A,54H
Bit oriented data transfer
• Carry flag in PSW is used as a single bit accumulator.
MOV C, P0.0 data at 0th pin of port 0
MOV C, 67H bit addressable RAM
MOV C, 2CH.7 same bit referred as above in bit addressable RAM
Arithmetic Instructions
Addition
• Add the contents of A with immediate data with or without carry.
ADD A, #45H
Division
• DIV AB: This instruction divides 8 bit unsigned number stored in A
by an 8 bit unsigned number stored in B. After division the quotient
will be stored in accumulator and remainder in B.
DA A (Decimal Adjust After Addition)
• When two BCD numbers are added, the answer is a non-BCD number.
To get the result in BCD, we use DA A instruction after the addition.
Adds 6 to higher or lower nibble for valid BCD.
Eg 1: MOV A,#23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78
no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABH
DA A // [A]=11, C=1 . ANSWER IS 111.
Accumulator data is changed after DA A
Increment
• Increments the operand by one. INC increments the value of source
by 1. If the initial value of register is FFH, incrementing the value will
cause it to reset to 0.
INC A
INC byte
INC DPTR
Decrement
• Decrements the operand by one. DEC decrements the value of
source by 1. If the initial value of is 0, decrementing the value will
cause it to reset to FFH.
DEC A
DEC byte
Logical Instructions
Logical AND
• ANL destination, source : ANL does a bitwise "AND" operation
between source and destination, leaving the resulting value in
destination. The value in source is not affected.
ANL A,#DATA ANL PSW, #E7H forces 3rd and 4th bit to
low
ANL A, Rn
Logical OR
• ORL destination, source : ORL does a bitwise "OR" operation
between source and destination, leaving the resulting value in
destination. The value in source is not affected.
ORL A,#DATA ORL PSW, #18H forces 3rd and 4th bit
to high
Logical Ex-OR
• XRL destination, source : XRL does a bitwise "EX-OR" operation
between source and destination, leaving the resulting value in
destination. The value in source is not affected.
XRL A,#DATA XRL P1, #40H Complements
6th bit of port 1
XRL A,Rn
Complement
• CPL A
Rotate Instructions
• RR A : This instruction is rotate right the accumulator. Each bit is
shifted one location to the right, with bit 0 going to bit 7.
• RRC A : Rotate right through the carry. Each bit is shifted one
location to the right, with bit 0 going into the carry bit in the PSW,
while the carry goes into bit 7.
• RLC A : Rotate left through the carry. Each bit is shifted one
location to the left, with bit7 going into the carry bit in the PSW,
while the carry goes into bit 0.
Other Logical Instructions
• CLR – Clear
CLR A CLR R1
CLR @R4 CLR 45H
MOV A, #3CH
SETB C sets the carry flag
RLC A
• RET instruction: pops top two contents from the stack and load
it to PC.
THANK YOU