8051
8051
8051
Curtis A. Nelson
Engr 355
microcomputers
Microcontrollers and microprocessors are different in three
main aspects: hardware architecture, applications, and
instruction set features
Hardware architecture: A microprocessor is a single chip
CPU while a microcontroller is a single IC contains a CPU
and much of remaining circuitry of a complete computer
(e.g., RAM, ROM, serial interface, parallel interface, timer,
interrupt handling circuit)
Applications: Microprocessors are commonly used as a
CPU in computers while microcontrollers are found in
small, minimum component designs performing control
oriented activities
8051
Today over fifty companies produce variations of the
8051.
MCS-51
8051 belongs to MCS-51 family of microcontrollers
MCS-51 was developed by Intel but other
manufacturers (e.g., Siemens, Philips) are second
sources of this family.
Summary of features of the standard 8051
Memory
8051 implements a separate memory space for programs
microcontrollers
Bits can be set, cleared, ANDed, ORed etc, with a single
instruction
8051 ports are bit-addressable, simplifying the interface
to single bit inputs and outputs
The 8051 contains 210 bit-addressable locations
128 of these locations are at addresses 20H to 2FH and
the rest are in the special function registers
10
Register Banks
The bottom 32 locations of internal memory contain the
register banks
8051 supports 8 registers R0 to R7 and after a system reset
(default) the registers are at address 00H to 07H
MOV A, R5: reads the content of address 05H into the
accumulator
MOV A,05H will do the same thing
The active register bank may be altered by changing the
register bank select bits in the Program Status Word (PSW)
Idea of register banks permits fast and effective context
switching
11
12
13
14
user
Register Bank Select Bits (RS0 and RS1): determine
the active register bank
Example: the following instructions enable register
bank 3 and move the content of R7 (address 1FH) to
the accumulator
SETB RS1
SETB RS0
MOV A,R7
15
Results greater than 127 or less than 128 will set OV bit
When unsigned numbers are added OV can be ignored
Example: What is the OV and the content of accumulator after
the following instruction sequence:
MOV R7, #FFH
MOV A, #0FH
ADD A,R7
Answer: OV=0, ACC=0EH
16
17
B Register
B register or accumulator B at address F0H is used
18
Stack Pointer
Stack pointer (SP) is an 8-bit register at address 81H
It contains the address of the data item currently on top of the
stack.
Stack operations include pushing data on the stack and
popping data off the stack
Pushing increments SP before writing the data
Popping from the stack reads the data and decrements the SP
8051 stack is kept in the internal RAM
Depending on the initial value of the SP, stack can have
different sizes
Example: MOV SP,#5FH
On 8051 this would limit the stack to 32 bytes since the
uppermost address of on chip RAM is 7FH.
19
20
Instruction Set
8051 instructions have 8-bit opcode
There are 256 possible instructions of which 255 are
implemented
Some instructions have one or two additional bytes for
data or address
There are 139 1-byte instructions, 92 2-byte
instructions, and 24 3-byte instruction
Where does the data for an instruction come from?
Addressing modes
21
Addressing Modes
There are eight addressing modes available in the 8051:
Register
Direct
Indirect
Immediate
Relative
Absolute
Long
Indexed
22
Register Addressing
8051 has access to eight working registers (R0 to R7)
Instructions using register addressing are encoded using the
Example: MUL AB
23
Direct Addressing
Direct addressing can access any on-chip memory
location
Example: ADD A,55H
Example: MOV P1, A
24
Indirect Addressing
How is a variable identified if its address is determined or modified
while a program is running?
Example:
Loop:
25
Immediate Addressing
When the source operand is a constant rather than a variable,
26
Immediate Addressing
With one exception, all instructions using immediate
addressing use 8-bit data
Exception: when initializing the data pointer, a 16-bit
constant is required
Example: MOV DPTR, #8000H
27
Relative Addressing
Relative addressing is used with certain jump instructions
Relative address (offset) is an 8-bit signed value (-128 to 127)
28
Absolute Addressing
Absolute addressing is only used with ACALL and
AJMP
The 11 least significant bits of the destination address
comes from the opcode and the upper five bits are
the current upper five bits in the program counter
(PC).
The destination is in the same 2K (211) of the source
29
Long Addressing
Long addressing is used only with the LCALL and LJMP
instructions
These 3-bytes instructions include a full 16-bit
destination address as bytes 2 and 3
The full 64K code space is available
The instruction is long and position dependent
Example: LJMP, 8AF2H
Jumps to memory location 8AF2H
30
Indexed Addressing
Indexed addressing uses a base register (either the
31
Instruction Types
8051 instructions are divided among five groups:
Arithmetic
Logical
Data transfer
Boolean variable
Program branching
32
Arithmetic
Since different addressing modes are available, an
A,7FH
A,@R0
A,R7
A,#35H
33
Arithmetic
MOV A,R7
CLR C
SUBB A, R6
MOV R7,A
Clearing the flag is necessary because the only form of
subtraction in 8051 is SUBB (subtract with borrow). The
operation subtracts from the accumulator source byte and carry
bit.
Curt Nelson Engr355
34
Arithmetic
Any memory location can be incremented or
35
Arithmetic
MUL AB: multiplies 8 bit unsigned values in A and B
36
Arithmetic
For BCD arithmetic, ADD and ADDC must be followed
by a DA A (decimal adjust) operation to ensure the
result is in range for BCD.
Note: ADDC simultaneously adds accumulator, the
variable and the carry flag.
37
Arithmetic
Example: Two 4-digit BCD numbers are in internal
38
Logical Instructions
8051 logical instructions perform Boolean operations on
bytes of data on a bit-by-bit basis .
39
Logical Instructions
Rotate instructions (RL A, RR A) shift the accumulator
one bit to the left or right. For a left rotation, MSB rolls
into LSB position. For a right rotation, LSB rolls into
MSB position.
RLC A and RRC A perform similar operations on
concatenation of carry flag and accumulator.
Example: A=00H and C=1. After RRC A, what will be in A
and C?
A=80H and C=0
40
Logical Instructions
Illustrate two ways to rotate contents of accumulator
three positions to the left. Discuss each method in
terms of memory and execution speed
RL A
RL A
RL A
SWAP A
RR A
All instruction are 1-byte, 1-cycle. So first solution uses
three bytes of memory and takes three CPU cycles and
second solution uses only two bytes of memory and
executes in two cycles
41
Logical Instructions
Example: write an instruction sequence to reverse the
bits in the accumulator.
MOV R7,#8
LOOP: RLC A
XCH A,B
RRC A
XCH A,B
DJNZ R7,LOOP
XCH A,B
42
43
44
16-bit addresses uses all Port 2 for high-byte and this port
cannot be used for I/O
45
Look-Up Tables
MOVC loads the accumulator with a byte from code (program)
memory
The address of the byte fetched is the sum of the original
unsigned 8-bit accumulator contents and the content of a 16bit register (either the data pointer or PC). In the latter case,
the PC is incremented to the address of the following
instruction before being added to the accumulator
MOVC A, @A+DPTR
MOVC A,@A+PC
46
Look-Up Tables
47
Boolean Instructions
8051 contains a complete Boolean processor for single-bit
operations.
All bit accesses use direct addressing
Bits may be set or cleared in a single instruction
Example: SETB P1.7
CLR P1.7
Carry bit in PSW is used as a single-bit accumulator for Boolean
operations.
Bit instructions that refer to carry bit as C are assembled as carryspecific instructions
Carry also has a mnemonic representation (CY) which can be used
in connection with non-carry-specific instructions.
Example:
CLR C
CLR CY
Both do the same. First one is 1 byte and the second one is 2-bytes
48
Boolean Instructions
Example: Compute the logical AND of the input signals on
MOV C, P1.0
ANL C,P1.1
MOV P1.2,C
SJMP LOOP
(1 cycle)
(2 cycle)
(2 cycle)
(2 cycle)
49
Branching Instructions
There are three versions of JMP instruction: SJMP, LJMP and
AJMP.
SJMP instruction specifies destination address as a relative
offset. This instruction is 2 bytes and jump distance is limited
to -128 to 127.
LJMP specifies the destination address as a 16-bit constant.
The destination address can be anywhere in the 64K program
memory space
AJMP specifies the destination address as an 11-bit constant.
Destination must be within a 2K block of AJMP.
In all cases, programmer specifies the destination address to
the assembler (using label or a 16-bit constant) and the
assembler puts destination address into correct format.
50
51
Contents
02H
07H
3CH
52
them in the PC
Jumping in or out of a subroutine any other way usually
fouls up the stack and causes the program to crash
53
Conditional Jump
The 8051 offers a variety of conditional jump instructions
JZ and JNZ tests the accumulator for a particular condition
DJNZ (decrement and jump if not zero) is a useful instruction for
building loops
integers. If the first one is less than the second one, the carry is set
54
Summary
8051 overview
Hardware
Instruction set
55