Instruction Set of 8051
Instruction Set of 8051
Register
Specific
Code Data
Addressing using MOV, MOVX and MOVC
Addressing Modes
Direct
Indirect
Register
Register specific
Immediate
External
a) Code access
b) Data access
Direct Addressing Mode
Definition:
• Data is available in the memory and the address of that memory is specified
in an instruction.
Examples: Internal
• MOV A,40H RAM
39H
• MOV R2,14H
Accumulator 40H
41H
Direct Addressing Mode
Entire 128 bytes of RAM can be accessed using direct
addressing mode.
It is most often used to access RAM loc. 30H – 7FH
(General Purpose RAM area).
SFR address range from 80 – FFH
MSB 0 = GP RAM and MSB 1= SFRs
Examples:
• MOV A,@R0
• MOV @R0,A
Internal
RAM
39H
R0
Accumulator 40H
40H
41H
Caution
The numbers in the register must be a RAM address.
Only register R0 or R1 may used for indirect addressing.
Register ADDRESSING MODE
Examples:
• MOV A,R2
• MOV R2,A
Accumulator R2
Register Addressing Mode
Each register bank consist of registers R0 to R7.
MOV DPTR, A
MOV Rm, Rn
caution
It is impossible to have immediate data as a destination.
All the numbers must start with 0-9 or assembler assumes the number as label.
Register to register moves using the register addressing mode occur between A register and R0
to R7
Register Specific Addressing Mode
Definition:
• Data is available in the specific register such as A register or DPTR.
Examples:
• SWAP A
• RR A
• DA A
Immediate Addressing Mode (#)
Definition:
• Data is available in the instruction itself.
Examples:
• MOV A,#30H
• MOV DPTR,#1234H
• MOV R0,#54H
Accumulator 30H
Immediate Addressing Mode
Specify data by its value
Definition:
• Data is available in the external Data
memory and the address of that memory is
either in R0, R1 or DPTR.
Examples:
• MOVX A,@R0
• MOVX A,@DPTR
External Addressing Mode
SP + 2 Y SP
PUSH Y POP Y
SP + 1 X SP - 1
PUSH X POP X
SP SP - 2
MOV:
• 16 bit data transfer involving the
DPTR
MOV DPTR,#1234h
Data transfer group…
MOVC:
Move Code Byte
Load the A register with a byte from program memory.
Must use indexed addressing
MOVC A, @A+DPTR
MOVC A, @A+PC
Data transfer group…
MOVX:
Data transfer between the A register and
a byte from external data memory.
MOVX A, @Ri
MOVX A, @DPTR
MOVX @Ri, A
MOVX @DPTR, A
Data transfer group…
PUSH/POP:
Push and Pop a data byte onto the stack.
The data byte is identified by a direct
address from the internal RAM locations.
PUSH DPL
POP 40H
Data transfer group…
XCH:
Exchange A register and a byte variable
XCH A, Rn
XCH A, direct
XCH A, @Ri
XCHD:
Exchange lower digit of A register with the lower digit of the
memory location specified.
XCHD A, @Ri
The lower 4-bits of the A register 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.
Data Transfer Group
MOV <dest-byte >, < src-byte > ; dest source
Instruction Example Operation Addressing Bytes Machine Clock Flags
Mode Cycle pulses
MOV A, Rn MOV A, R1 A = R1 Register 1 1 12 No
MOV Rn, A MOV R5, A R5 = A Register 1 1 12 No
MOV A, Direct MOV A, 40H A = (40) Direct 2 1 12 No
MOV direct, A MOV 80H, A (80) = A Direct 2 1 12 No
MOV A, @Ri MOV A, @R0 A = ((R0)) Register 1 1 12 No
indirect
MOV A, # data MOV A, #30 A = 30 Immediate 2 1 12 No
MOV Rn, Direct MOV R3, 30H R3 = (30) Direct 2 2 24 No
MOV Rn, # data MOV R7, #25H R7 = 25 Immediate 2 1 12 No
MOV direct, Rn MOV 30H, R5 (30) = R5 Direct 2 2 24 No
MOV direct, direct MOV 20H, 30H (20) = (30) Direct 3 2 24 No
MOV direct, @Ri MOV 20H, @R1 (20) = ((R1)) Register 2 2 24 No
indirect
MOV direct, # data MOV 20H, #10H (20) = 10 Immediate 3 2 24 No
Data Transfer Group
Instruction Example Operation Addressing Bytes Machine Clock Flags
Mode Cycle pulses
DIV AB:
• Divide A by B and place result as follows:
• Quotient in A
• Remainder in B
DA A:
• Decimal adjust A register for addition.
Arithmetic Group
CPL A:
◦ Complement of A is loaded in A.
SWAP A:
◦ Low order nibble and high order nibble of A will be
interchanged.
Logical Group…
RR – RL – RRC – RLC A
RR:
C
RRC:
RL:
C
RLC:
Logical Group
Instruction Example Addressing Bytes Machine Clock Flags
Mode Cycle pulses
ANL < dest-byte >, < src-byte >
ANL A, Rn ANL A, R3 Register 1 1 12 No
ANL A, Direct ANL A, 50H Direct 2 1 12 No
ANL A, @Ri ANL A, @R2 Register indirect 1 1 12 No
ANL Direct, A ANL 30H, A Direct 2 1 12 No
ANL A, # data ANL A, #45H Immediate 2 1 12 No
ANL Direct, # data ANL 54H, #33H Immediate 3 2 24 No
Logical Group
Instruction Example Addressing Bytes Machine Clock Flags
Mode Cycle pulses
ORL < dest-byte >, < src-byte >
ORL A, Rn ORL A, R3 Register 1 1 12 No
ORL A, Direct ORL A, 50H Direct 2 1 12 No
ORL A, @Ri ORL A, @R2 Register indirect 1 1 12 No
ORL Direct, A ORL 30H, A Direct 2 1 12 No
ORL A, # data ORL A, #45H Immediate 2 1 12 No
ORL Direct, # data ORL 54H, #33H Immediate 3 2 24 No
Logical Group
Instruction Example Addressing Bytes Machine Clock Flags
Mode Cycle pulses
XRL < dest-byte >, < src-byte >
XRL A, Rn XRL A, R3 Register 1 1 12 No
XRL A, Direct XRL A, 50H Direct 2 1 12 No
XRL A, @Ri XRL A, @R2 Register indirect 1 1 12 No
XRL Direct, A XRL 30H, A Direct 2 1 12 No
XRL A, # data XRL A, #45H Immediate 2 1 12 No
XRL Direct, # data XRL 54H, #33H Immediate 3 2 24 No
Logical Group
JNB bit, rel (Jump if bit (PC) = (PC) + 3 JNB P2.1, Back 3 2 24
not set) IF (bit) = 0
THEN (PC) = (PC) + rel
JBC bit, rel (Jump if bit set (PC) = (PC) + 3 JBC P2.1, Back 3 2 24
& clear bit) IF (bit) = 1
THEN
Bit= 0 and
(PC) = (PC) + rel
Subroutine Operations
ACALL addr11
Lower 11 bits of PC will be replaced with addr11.
LCALL addr16
New value of PC will be addr16.
RET
Top two bytes from stack will be popped and loaded in PC.
RETI
Return from interrupt routine.
Program Transfer & Machine Control Group
Instruction Operation Example Bytes Machine Clock
Cycle pulses
Absolute Call:- (within 2K) (PC) = ((PC) +2)
ACALL addr 11 (Opcode 31) (SP) = (SP) + 1 ACALL 2 2 24
Delay
((SP)) = (PC7-0)
(SP) = (SP) + 1
((SP)) = (PC15-8)
(PC10-0) = Page address
CJNE @Ri, # data, rel Same as Above CJNE @R1, #98, Back 3 2 24
Program Transfer & Machine Control Group
Instruction Operation Example Bytes Machine Clock
Cycle pulses
DJNZ < byte >, < ret-addr > (Decrement specified memory location/register by 1 and jump if not
zero)
DJNZ < byte >, < ret-addr > (PC) = (PC) + 2 DJNZ R5, SUB 2 2 24
(byte) = (byte) – 1
If (byte) < > 0
Then
(PC) = (PC) + rel
NOP No Operation NOP 1 1 12
2.3 Assembler Directives
ORG-Originate
The ORG directive allows us to set the beginning address.
The number after ORG may be Hex or Decimal.
If it is decimal assembler converts into Hex.
Ex : ORG 0000H.
Dbit
This directive defines bit type variable.
Ex
Name DBitInitial value
Num1 DBit 0 or 1.
Db- define byte
This directive defines byte type variable.
DB is used to define binary, decimal, hexadecimal and ASCII numbers.
Ex
Name DB Initial value Data type.
Num1 DB 24 Decimal
Num2 DB 00110011B Binary
Num3 DB 24H Hexadecimal
Num4 DB “237” ASCII
Equ- Equate
It is used to give a name to some value or symbol in the program.
EQU is used to define constant without occupying a memory location.
Each time when the assembler finds that name in the program it replaces that name
by assigned value.
Ex XYZ EQU 40H.
DS
This directive declares 24byte stack in data segment that starts at internal RAM
location 08H.
Public and Extrn
These directive are used where the source file start.
The directive public declares the variables defined in a specific file that can be used in
the other source file.
The directive extrn declares the variables that are used in the present source file but
are defined in some other source file.
END
This directive is placed at the end of source and it acts the last statement of program.
The END directive terminate the entire program.
University Questions (EXTC)
Dec-2009
1) Explain the difference between SJMP, AJMP and LJMP instructions of
8051. (05 Marks)
May-2010
2) Explain any four bit level instructions of 8051. (04 Marks)
May-2011
3) Explain the following instructions of 8051 (10 Marks)
(a) LCALL
(b) MOV @R0, iram addr
(c) MUL
(d) CJNE @Rp, #n, radd
(e) DJNZ Rn, radd
University Questions
May-2011
1) Explain following assembler directives of ASM51. (05 Marks)
1. DBIT 2. DS 3.EXTRN 4. PUBLIC 5. ORG
2) Explain addressing modes of 8051 with examples. (10 Marks)
Nov-2011
3) Explain following assembler directives of ASM51. (05 Marks)
1. DBIT 2. DS 3.EXTRN 4. PUBLIC 5. ORG
4) Explain addressing modes of 8051 with examples. (10 Marks)
May-2012
5) Explain following instructions :- (04 Marks) 1.
XCHD A, @Ri 2. RETI
May-2013
6) Explain different assembly directives of 8051. (05
University Questions (EXTC)
May-2015
7) Explain 8051 assembler directives. (05 Marks)
8) For an 8051 system of 11.0592MHz. Find how long it takes to (10
Marks)
execute each of following instructions.
a) MOV R3,#55H
b) DJNZ R2,TARGET
c) LJMP
d) SJMP
e) MUL AB
9) Explain operations of PUSH and POP instructions with example. (05 Marks)