Microprocessors and
Microcontrollers
Module IV
Instruction set of 8051
▪ Data transfer instructions
▪ Arithmetic instructions
▪ Logical instructions
▪ Branch instructions
▪ Subroutine instructions
▪ Bit manipulation instructions
2
Data transfer instructions
1. Move the contents of a register Rn to A.
MOV A,R2
2. Move the contents of a register A to Rn
MOV R4,A
3. Move an immediate 8 bit data to register A or to Rn
or to a memory location(direct or indirect)
MOV A, #45H (Move value 45 to A)
MOV R6, #51H (Move value 51 to R6)
3
Data transfer instructions . . . Contd.
4. Move the contents of a memory location to A or A to
a memory location using direct and indirect
addressing
MOV A, 65H (Move data at RAM location 65 to A)
MOV A, @R0 (Move data at RAM location addressed by
R0 to A)
5. Move the contents of a memory location to Rn or Rn
to a memory location using direct addressing
MOV R3, 65H (Move data at RAM location 65 to R3)
MOV 45H, R2 (Move data at R2 to RAM location 45)
4
Data transfer instructions . . . Contd.
6. Move the contents of memory location to another
memory location using direct and indirect addressing
MOV 47H, 65H (Move data at RAM
location 65 to RAM location 47)
MOV 45H, @R0 (Move data at RAM location
addressed by R0 to RAM location 45)
7. Move the contents of an external memory to A or A
to an external memory
MOVX A. @DPTR
5
Data transfer instructions . . . Contd.
8. Push and Pop instructions
PUSH 6 Increment SP (SP=SP+1) then, data at RAM
location 06 is moved to stack memory addressed by SP.
POP 7 Data at stack memory addressed by SP is moved
to RAM location 07 then decrement SP (SP=SP‐1).
9. 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 Exchange the data at A and register R3
6
Arithmetic instructions
▪ Addition
Add the contents of A with immediate data with or without
carry.
ADD A, #45H (A = A + 45)
ADDC A, #OB4H (A = A + 45 + CY Flag)
Add the contents of A with register Rn with or without carry.
ADD A, R5 (A = A + Data at R5)
ADDC A, R2 (A = A + Data at R2+ CY Flag)
Add the contents of A with contents of memory with or
without carry using direct and indirect addressing
ADD A, 51H (A = A + Data at RAM location 51)
7 ADDC A, 75H (A = A + Data at RAM location 75 + CY Flag)
Arithmetic instructions
▪ Subtraction
Subtract the contents of A with immediate data with carry.
SUBB A, #45H (A = A ‐ 45 ‐ CY Flag)
Subtract the contents of A with register Rn with carry.
SUBB A, R5 (A = A ‐ Data at R2 ‐ CY Flag)
Subtract the contents of A with contents of memory with
or without carry using direct and indirect addressing
SUBB A, 51H (A = A - Data at RAM location 51 ‐ CY Flag)
8
Arithmetic instructions
▪ Multiplication
MUL A B
This instruction multiplies two 8 bit unsigned numbers
which are stored in A and B registers.
After multiplication the lower byte of the result
will be stored in accumulator and higher byte of result
will be stored in B register.
9
Arithmetic instructions
▪ Division
DIV A B
This instruction divides the 8 bit unsigned number
which is stored in A by the 8 bit unsigned number
which is stored in B register.
After division the result will be stored in
accumulator and remainder will be stored in B
register.
10
Arithmetic instructions
▪ Increment: increments the operand by one.
INC A
INC Rn
▪ Decrement: decrements the operand by one.
DEC A
DEC Rn
11
Logical Instructions
▪ Logical AND
ANL destination, source
ANL A,#DATA eg:‐ ANL A,#09 (A = A and value 09 )
ANL A, Rn eg:‐ ANL A,R1 (A = A and Data at R1 )
▪ Logical OR
ORL destination, source
ORL A,#DATA eg:‐ ORL A,#09 (A = A or value 09 )
ORL A, Rn eg:‐ ORL A,R1 (A = A or Data at R1 )
12
Logical Instructions
▪ Logical Ex‐OR
XRL destination, source
XRL A,#DATA eg:‐ XRL A,#09 (A = A xor value 09 )
XRL A,Rn eg:‐ XRL A,R1 (A = A xor Data at R1 )
▪ Logical NOT
CPL complements operand
CPL A inverts all the bits of A.
CPL C inverts Carry flag.
CPL bit address Eg:‐ CPL 07
▪ SWAP A – Swap the upper nibble and lower nibble of A.
13
Rotate Instructions
▪ RR A
Rotate the accumulator right.
▪ RL A
Rotate the accumulator left.
▪ RRC A
Rotate the accumulator right through carry.
▪ RLC A
Rotate the accumulator left through carry.
14
Branch (JUMP) Instructions
▪ Relative Jump
▪ Short Absolute Jump
▪ Long Absolute Jump
15
Relative Jump
▪ Jump is to a range of bytes within +127d or ‐128d
from the instruction following jump.
▪ Relative Jump Instructions:
a. Unconditional jump:
SJMP <relative address>
▪ b. Conditional jump:
Bit level Jump Instructions:
JC <relative address> Jump if CY = 1
JNC <relative address> Jump if CY = 0
JB bit, <relative address> if bit = 1, jump
JNB bit, <relative address> if bit = 0, jump
16
Relative Jump
CJNE @R1,#24h,LABEL ;3 bytes 2 cycles CJNE A,#10h,LABEL ;3 bytes 2 cycles CJNE A,60h,LABEL ;3 bytes 2 cycles CJNE R6,#12h,LABEL ;3 bytes 2 cycles
CJNE @R1,#24h,LABEL ;3 bytes 2 cycles CJNE A,#10h,LABEL ;3 bytes 2 cycles CJNE A,60h,LABEL ;3 bytes 2 cycles CJNE R6,#12h,LABEL ;3 bytes 2 cycles
▪ Byte level Jump Instructions:
CJNE <destination byte>, <source byte>, <relative address>
Compare the two bytes of and if these are not equal
then jump to the relative address specified.
• CJNE A, 56H, <relative address>
• CJNE A, #56H, <relative address>
• CJNE @R0, #56H, <relative address>
• CJNE R5, #56H, <relative address>
17
Relative Jump
▪ Byte level Jump Instructions:
DJNZ Rn <relative address>
Decrement the data at register Rn and jump to the relative
address specified if the decremented value not equal to 0.
JZ <relative address>
Jump to the relative address specified if A equal to 0.
JNZ <relative address>
Jump to the relative address specified if A not equal to 0.
18
Short Absolute Jump
▪ In 8051, 64 K byte of program memory space is
divided into 32 pages of 2K byte each.
▪ Jump is to a range of bytes within the 2K byte from
the instruction following jump.
Example of short absolute jump: ‐
AJMP < address > ;
19
Long absolute jump
▪ Applications that need to access the entire program
memory from 0000H to FFFFH use long absolute
jump.
▪ Jump is to any location among the 64 K memory.
LJMP <address> ;
20
Addressing modes
1. Immediate addressing.
MOV A,#30H
ADD A, #83
2. Register addressing.
MOV A, R0
ADD A, R6
3. Direct addressing.
MOV A, 30H
21
Addressing modes
4. Indirect addressing.
Here the address of location is provided by register
R0 or R1.
MOV A,@R0
5. Indexed addressing.
This addressing mode is used for accessing data
from look up tables stored in on-chip ROM.
Here the address of memory is indexed, i.e. added
to form the actual address of memory.
22 MOVC A,@A+DPTR
Data types
▪ The 8051 has only one data type
▪ It has 8 bits
▪ It can be positive or negative
23
Directives / pseudocodes
▪ DB – Define Byte
Data can be in any format.
Eg: DATA1 DB 24
Eg: DATA2 DB 47H
▪ EQU – used to define constants without occupying
memory
Eg: COUNT EQU 88
--------
MOV R4, #COUNT
24
Directives / pseudocodes
▪ ORG
Used to specify the starting address of program
or data.
▪ END
Used to indicate the end of the source file.
25
Example programs
26
Example programs
CLR C
MOV A, #E7H
ADD A, #8DH
MOV R6, A
MOV A, #3CH
ADDC A, #3BH
MOV R7,A
27
Example program – BCD addition
28
An application of DIV – example program
29
Example program
Read P1. If the value is 45H, send 99H to P2. Otherwise,
P2 stays cleared.
30
Example program
31
Example program
32
8051 machine cycle
▪ Each 8051 instruction requires several clock cycles or
machine cycles.
▪ The machine cycle is determined by the frequency of
the crystal oscillator connected to the system.
▪ In the original 8051, one machine cycle = 12 oscillator
periods
33
8051 machine cycle
34
8051 machine cycle
35
Delay calculation
36
37
Clocks per machine cycle for various 8051 versions
38
Machine cycles of instructions
39
Program
40
Program
41
Program
(a)
(b)
or
42
Programming the 8051 in C
▪ Compilers produce Hex codes that can be downloaded
onto the 8051 ROM.
▪ Why C programming?
Assembly language programming is tedious.
It is easier and less time consuming to write programs in C.
Use of code available in libraries is possible.
C code is portable to other microcontrollers with little or
no modification.
▪ Hex code produced by assembly language is much
smaller than that produced by C.
43
Data types in 8051 C
▪ Unsigned char
Since 8051 is an 8-bit microcontroller, we need a data type
with 8 bits.
44
Program
45
Data types in 8051 C
▪ Signed char
When sign is needed for data. (The default of char in C is
signed char) The values 1, FFH, 2,
FEH, 3, FDH, 4, FCH
can be read at P1.
46
Data types in 8051 C
▪ Unsigned int
16-bit data type used for memory addresses and variables
with values greater than 255.
▪ Signed int
▪ Sbit
For accessing single bit of the bit-addressable registers.
▪ Bit
Used for accessing bits of bit-addressable memory, 20 – 2FH.
▪ Sfr
To access byte sized SFRs
47
Example program
48
Time delay in 8051 C
▪ Two ways to create time delay
▫ Using a loop
▫ Using 8051 timers
▪ In creating a time delay using loops, these factors are
to be kept in mind
▫ 8051 version – clock cycles/MC are different
▫ Crystal frequency which determines the clock frequency
▫ Choice of compiler
▪ For these reasons, the exact delay has to be
measured using an oscilloscope.
49
Program
DS89C420 with
XTAL = 11.0592 MHz
50
I/O programming in 8051 C
51
I/O programming in 8051 C
52
I/O programming in 8051 C
53
Program
54
Program
55