8051 Instruction Set
8051 Instruction Set
8051 Instruction Set
Mnemonic Description
MOVC A, @A+DPTR MOV DPTR, #1234h Copy the immediate no. 1234 to DPTR
MOV A, #56h Copy the immediate no 56 to A
MOVC A, @A+DPTR Copy the content of the address 128Ah found
at ROM to A.
MOVC A, @A+PC This instruction will copy the contents of
code memory location formed by adding
PC and A, to the accumulator.
Data Transfer Instructions
3. MOVX- Normally used with external RAM, I/O address. All external
data move must involve A register
MOVX A, @Ri MOVX A, @R0 This instruction will copy the data from 8 bit address
pointed by register Ri of the selected register bank to the
accumulator.
MOVX A, @DPTR MOVX A, @DPTR This instruction will copy the contents of external data
memory location, pointed by DPTR to the accumulator.
MOVX @Ri, A MOVX @R1, A This instruction will copy the contents of the accumulator
to the external data memory location pointed by register
Ri of selected register bank.
MOVX @ DPTR, A MOVX @ DPTR, A This instruction will copy the contents of the accumulator
to the 16-bit address, pointed by DPTR.
4. PUSH
PUSH ADDR; Increment SP, copy the data in the address to the internal
RAM address contained in SP.
4. POP
POP ADDR; copy the data in the internal RAM address contained in SP.to address,
decrement SP
5. XCH –Exchange the contents from destination to source and source to destination
Example Description
XCH A, R1 This instruction will load the contents of register R1 of selected register bank in the
accumulator and at the same time the contents of original accumulator will be copied in
register R1
XCH A, 10H This instruction will load the contents of memory location whose address is 10H to the
accumulator and at the same time the contents of accumulator are transferred to the
memory location whose address is 10H
XCH A,@RO This instruction will load the contents of memory location pointed by register R0 of selected
register bank to the accumulator and at the same time the contents of accumulator arc copied
to the memory location pointed by R0 register of the selected register bank.
XCHD A, @R0 This instruction exchange the lower nibble of accumulator (bit 3-0) with the lower nibble of
the memory location indirectly addressed by the specified register R0/R1.
List of All Possible Data Transfer Instruction
Note
1. Direct- Address
2. Rn- R0 to R7
Instruction
Note
1. Direct- Address
2. Rn- R0 to R7
3. Ri- R0 or R1
4. @- Indirect Address
Arithmetic Group
• Using Arithmetic Instructions, you can perform addition, subtraction,
multiplication and division.
• The arithmetic instructions also include increment by one, decrement
by one and a special instruction called Decimal Adjust Accumulator.
Arithmetic Group
Mnemonic Description
INC Increment by 1
DEC Decrement by 1
MUL Multiply
DIV Divide
ADDC A, @Ri Register Indirect ADDC A, This instruction will add the contents of memory location
@R0 pointed by register Ri of selected register bank with the
accumulator and earn' flag. The result is stored in
accumulator.
ADDC A, #data Immediate ADDC A, #40H This instruction will add the contents of accumulator with
addressing immediate data specified in the instruction along with
carry.
SUBB Instruction
Mnemonics Addressing Example Description
mode
SUBB A, Rn Register addressing SUBB A, Rl This instruction will subtract the contents of register Rn
of the current register bank and carry flag together,
from the accumulator. The result is stored in
accumulator.
SUBB A, Direct addressing SUBB A, 10H This instruction will subtract the contents of memory
direct location whose direct address is specified in the
instruction and carry flag together from the contents of
accumulator. The result is stored in accumulator.
SUBB A, Register Indirect SUBB A, @R0 This instruction will subtract the contents of memory
@Ri location pointed by register Ri and contents of carry flag
from the accumulator. The result is stored in
accumulator.
SUBB A, Immediate SUBB A. #40H This instruction will subtract the contents of data
#data addressing specified m the instruction and contents of carry flag
from the contents of accumulator.
The result is stored in accumulator.
INC Instruction and DEC Instruction
Mnemonics Addressing Example Description Mnemonics Addressing Example Description
mode mode
INC Rn Register INC R5 This instruction will
addressing increment the contents of DEC Rn DEC R5
Register This instruction will decrement
register Rn of selected addressing the contents of register Rn of
register bank by 1. selected register bank by 1-
INC direct Direct addressing INC 10H This instruction will
increment
the contents of memory DEC direct Direct addressing DEC 10H This instruction will decrement
location whose address is the contents of memory
specified in the instruction by location whose direct address is
1 specified in the instruction by 1.
INC @Ri Register Indirect INC, @R0 This instruction will DEC @Ri Register Indirect DEC @R0 This instruction will decrement
increment the contents of the
memory location that is contents of memory location
pointed by register Ri by 1. that is pointed by register Ri by
1.
MUL AB
• This instruction multiplies an eight bit unsigned integer in the Accumulator and the
13 register. The low-order byte of the sixteen-bit product is left in the accumulator,
and the high-order byte in B.
• Example: Let A = 50 H, B = A0 H after execution of MUL AB the content of register B =
32 H and register A = 00 H (as (50H) X (A0 M) = (3200H)).
DIV AB
• This instruction divides the unsigned number in accumulator with the
unsigned number in register B.
• Accumulator contents the quotient of the result and register B contains
the remainder.
• Example; Let [A] = FB H and [B] = 12 H then DIV AB will result [A] = 0DH
(quotient), [B] = 11 H (remainder)
DAA
• Decimal Adjust Accumulator for addition, to adjust to BCD format(0 to 9)
• All no. must be in BCD before addition, only ADD and ADDC are adjusted to
BCD
Logical Instructions
• The next group of instructions are the Logical Instructions, which
perform logical operations like AND, OR, XOR, NOT, Rotate, Clear and
Swap. Logical Instruction are performed on Bytes of data on a bit-by-
bit basis.
Mnemonic Description
ANL Logical AND
ORL Logical OR
XRL Ex-OR
CLR Clear Register
CPL Complement the Register
RL Rotate a Byte to Left
RLC Rotate a Byte and Carry Bit to Left
RR Rotate a Byte to Right
RRC Rotate a Byte and Carry Bit to Right
SWAP Exchange lower and higher nibbles in a Byte
Rotate
Bit Manipulation Instruction
Examples
• SET B 00h
• CPL 7Fh
• CLR C
• CLR ACC.5
• ANL P0, #0Fh
• SETB TR1
Branching Instructions in 8051
• In some cases, a microcontroller needs to perform the same tasks multiple numbers of times
across the program, such as generating a delay.
• A subroutine is responsible for performing these repetitive tasks. Using subroutines saves
memory and makes the program more efficient.
• Instead of repeating the same few lines of code for some task you need to execute multiple
times, you can just write it once and give it a label.
• Every time you need those lines to execute, just use the label to jump to the area where you
had stored the labeled code.
• In essence, a subroutine is like a function, and it is placed at a different memory location then
the program memory.
• The call instruction is used to transfer the control from the presently executing program code
to the subroutine, and the return instruction is used to return the control to the program
code
Stack and control transfer
• The stack of the 8051 plays a crucial role in the transfer of control when it
comes to call instructions.
• When the call instruction executes, the program counter increments so that it
points to the next instruction.
• The contents of the program counter are pushed into the stack (lower byte
first).
• After this, the program counter is loaded with the starting instruction of the
subroutine transferring the control.
• Once the RET instruction is encountered, the contents stored in the stack are
popped back into the program counter, and the microcontroller starts
executing the program code from where it had left it.
Stack and control transfer in CALL
LCALL Instruction Flags affected
Execution Auxilary
Opcode Operand Description Size Carry Overflow
Time carry
Transfers the
LCALL instruction can access an address of 16 bits. Due to this reason, this instruction can access any memory location in
the ROM space, but this instruction takes up 3 bytes of space and can waste memory resources. To save memory, the ACALL
instruction is used.
Example (LCALL)
The ACALL instruction is 2 bytes in size and can be used to access any address in a 2KB page. This
instruction affects only 11 bits or the program counter as compared to 16 in the case of LCALL.
When this instruction is executed, the PC saves its current value on the stack, then the lower byte of
the ADDRESS is stored in the lower byte section of PC, the remaining 3 bits of ADDRESS are stored in
bits 0, 1, and 2 of PC. The bits 3-7 of the PC are empty.
Example
• Example
ORG 0000H; Directive for starting address of the program code
MOV A,#50H; Moves 50H into the accumulator
MOV R0,#25H; Moves 25H into the accumulator
ACALL Delay; Calls delay subroutine
ADD A, R0; Adds A and R0. stores the result in A
ORG 0300H; Directive for starting address of delay subroutine
DELAY: NOP; Performs no operation for 1 machine cycle
NOP; Performs no operation for 1 machine cycle
RET; Returns control to program code
RET Instruction
Flags affected
Opcode Operand Description Size Execution Time
Carry Overflow Auxilary carry
Returns control
RET NONE to the program 1 byte 24 clock cycles Unaffected Unaffected Unaffected
code
The RETI instruction is used to return from interrupt subroutines and works in the same way as the RET instruction if
used outside a subroutine. When used inside a subroutine, RETI first enables interrupts of equal and lower priorities
to the interrupt that it is used in. The program execution continues at the address that is calculated by popping the
topmost 2 bytes off the stack. The most-significant-byte is popped off the stack first, followed by the least-
significant-byte.
Jumps in 8051
The program given below adds 3 ten times to the accumulator. It uses the DJNZ
instruction for looping.
MOV A,#0; clears the accumulator
MOV R2,#10; moves 10 into r2 which cats as a counter
AGAIN: ADD A,#03; adds 3 into the accumulator till r2!=0
DJNZ R2,AGAIN; decrements value in r2 and jumps to again label till value !=0
MOV R5,A; Moves the result into R5