8051 Instruction Set
8051 Instruction Set
8051 Instruction Set
Instruction Groups
The 8051 has 255 instructions
Every 8-bit opcode from 00 to FF is used except for A5.
MOV
MOV dest,source Rn-Indicates register R1,R2.R7 Direct-indicates direct internal RAM address #n- indicates immediate 8 bit data #nn- indicates immediate 16 bit data @R0-address of the memory location is available in the register R0
EXAMPLES:
MOV A,#65
MOV R5,#78
MOV DPTR,#9652
MOV P1,#65 ; where P1 is port1
MOV A, #data -Immediate 8 bit data is copied to the accumlator Ex: MOV A,#32 -data 32 is copied to the accumlator MOV A,R5- Data in R5 register is copied to the accumlator MOV A,direct -Data available in the memory location is copied to the accumlator Ex:MOV A,50 MOV A, @R0-data available in the memory location pointed by register R0 is copied to the accumlator Note: [R0] 50 M[50] 22 ;data 22 is copied to the accumlator
MOV
16-bit data transfer involving the DPTR
MOV DPTR, #data
40H
10
Example: MOV MOV MOV PUSH PUSH PUSH R6,#25H R1,#12H R4,#0F3H 6 1 4
SP=08H
SP=09H
SP=08H
XCHD
Exchange lower digit of accumulator with the lower digit of the memory location specified.
XCHD A, @Ri The lower 4-bits of the accumulator are exchanged with the lower 4-bits of the internal memory location identified indirectly by the index register. The upper 4-bits of each are not modified.
12
Arithmetic Instructions
ADD
8-bit addition between the accumulator (A) and a second operand.
The result is always in the accumulator. The CY flag is set/reset appropriately.
ADDC
8-bit addition between the accumulator, a second operand and the previous value of the CY flag.
Useful for 16-bit addition in two steps. The CY flag is set/reset appropriately.
13
Arithmetic Instructions
DA
Decimal adjust the accumulator.
Format the accumulator into a proper 2 digit packed BCD number. Operates only on the accumulator. Works only after the ADD instruction.
SUBB
Subtract an operand and the previous value of the borrow (carry) flag from the accumulator.
A A - <operand> - CY. The result is always saved in the accumulator. The CY flag is set/reset appropriately.
14
ADD A,#45 ; AA+45 ,Immediate data 45 is added with Acc and the result is stored in Accumlator ADD A,R2 ; AA+R2 ADD A,50 ; AA+M[50] ADD A,@R0 ; AA+M{[R0]}
Arithmetic Instructions
INC
Increment the operand by one.
The operand can be a register, a direct address, an indirect address, the data pointer.
DEC
MUL AB / DIV AB
Multiply A by B and place result in A:B. Divide A by B and place result in A:B.
16
INC A - Increments the contents of accumlator by 1 INC R5 - Increments the contents of R5 by 1 INC 50 - Increments the contents of memory location 50 by1 M[50]-22; before execution M[50]-23; after execution INC @R0 - Increments the contents of memory location using indirect addressing mode by1 Note- Reg R0 is holding the memory address 50 [R0]-50 M[50]-22 ; before execution, M[50]-23; after execution DEC A - decrements the contents of accumlator by 1 DEC R5 - decrements the contents of R5 by 1 DEC 50 - decrements the contents of memory location 50 by1 DEC@R0- decrements the contents of memory location using indirect addressing mode by1
Logical Operations
ANL / ORL
Work on byte sized operands or the CY flag.
ANL A, Rn ANL A, direct ANL A, @Ri ANL A, #data ANL direct, A ANL direct, #data
19
Logical Operations
XRL
Works on bytes only.
CPL / CLR
Complement / Clear. Work on the accumulator or a bit.
CLR P1.2
20
Logical Operations
RL / RLC / RR / RRC
Rotate the accumulator.
RL and RR without the carry RLC and RRC rotate through the carry.
SWAP A
Swap the upper and lower nibbles of the accumulator.
No compare instruction.
Built into conditional branching instructions.
21
RR RL RRC RLC A
EXAMPLE: RR A RR: RRC: RL: RLC:
C
C
Boolean Operations
This group of instructions is associated with the single-bit operations of the 8051. This group allows manipulating the individual bits of bit addressable registers and memory locations as well as the CY flag.
The P, OV, and AC flags cannot be directly altered.
Boolean Operations
CLR
Clear a bit or the CY flag.
CLR P1.1 CLR C
SETB
Set a bit or the CY flag.
SETB A.2 SETB C
CPL
Complement a bit or the CY flag.
CPL 40H ; Complement bit 40 of the bit addressable memory
24
; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h
CLR instruction is as same as SETB i.e.: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
Boolean Operations
ORL / ANL
OR / AND a bit with the CY flag.
ORL ANL C, 20H C, /34H ; OR bit 20 of bit addressable memory with the CY flag ; AND complement of bit 34 of bit addressable memory with the CY
flag.
MOV
Data transfer between a bit and the CY flag.
MOV C, 3FH
MOV P1.2, C
; Copy the CY flag to bit 3F of the bit addressable memory. ; Copy the CY flag to bit 2 of P1.
26
Branching Instructions
The 8051 provides four different types of unconditional jump instructions:
Short Jump SJMP
Uses an 8-bit signed offset relative to the 1st byte of the next instruction.
27
Branching Instructions
Absolute Jump AJMP
Uses an 11-bit address. 2 byte instruction
The upper 3-bits of the address combine with the 5-bit opcode to form the 1st byte and the lower 8-bits of the address form the 2nd byte.
The 11-bit address is substituted for the lower 11-bits of the PC to calculate the 16-bit address of the target.
The location referenced must be within the 2K Byte memory page containing the AJMP instruction.
Branching Instructions
The 8051 provides 2 forms for the CALL instruction:
Absolute Call ACALL
Uses an 11-bit address similar to AJMP The subroutine must be within the same 2K page.
Both forms push the 16-bit address of the next instruction on the stack and update the stack pointer.
29
Branching Instructions
The 8051 provides 2 forms for the return instruction:
Return from subroutine RET
Pop the return address from the stack and continue execution there.
Branching Instructions
The 8051 supports 5 different conditional jump instructions.
ALL conditional jump instructions use an 8-bit signed offset. Jump on Zero JZ / JNZ
Jump if the A == 0 / A != 0
The check is done at the time of the instruction execution.
Branching Instructions
Jump on Bit JB / JNB
Jump if the specified bit is set / cleared. Any addressable bit can be specified.
32
Branching Instructions
Compare and Jump if Not Equal CJNE
Compare the magnitude of the two operands and jump if they are not equal.
The values are considered to be unsigned. The Carry flag is set / cleared appropriately.
33
A, direct, rel A, #data, rel Rn, #data, rel @Ri, #data, rel
Branching Instructions
Decrement and Jump if Not Zero DJNZ
Decrement the first operand by 1 and jump to the location identified by the second operand if the resulting value is not zero.
DJNZ DJNZ Rn, rel direct, rel
No Operation
NOP
34
Jump if A=0
Jump if A/=0 Decrement and jump if A/=0
CJNE A,byte
CJNE reg,#data JC JNC JB JNB JBC
Jump if A/=byte
Jump if byte/=#data Jump if CY=1 Jump if CY=0 Jump if bit=1 Jump if bit=0 Jump if bit=1 and clear bit