Skip to content

Electronics · Ch 11 — Microcontroller

The 8051 Microcontroller — Architecture, Addressing Modes and Instruction Set

11.1

The 8051 Microcontroller — Architecture, Addressing Modes and Instruction Set

The microprocessor

A microprocessor is the general-purpose Central Processing Unit (CPU) of a digital computer. By itself it is not a complete computer — it only does the processing and needs external memory and I/O to become useful. Its internal block diagram (Figure 11.1.1) contains an Arithmetic and Logic Unit (ALU), a Program Counter (PC), a Stack Pointer (SP), one or more working registers, a clock-timing circuit and interrupt circuits.

Function of each block:

  • Arithmetic and Logic Unit (ALU): performs arithmetic operations and takes logical decisions.
  • Accumulator: stores a number; when a second number arrives it adds the two and stores the sum. It can also sense a signal, and clear or complement its contents.
  • Working register(s): a short-term store of one word, often a group of flip-flops.
  • Program Counter (PC): holds the address of a byte in memory and points to the next instruction to be fetched and executed.
  • Stack Pointer (SP): holds an address in internal RAM; used with certain opcodes to store and retrieve data quickly.
  • Clock circuit: generates the clock pulses that synchronise all internal operations.
  • Interrupt circuits: break the normal program flow and later allow it to resume from the same point.

The microcontroller and its blocks

A microcontroller is a true "computer on a chip". It has every feature of a microprocessor and, in addition, on-chip ROM, RAM, parallel I/O, serial I/O, timers/counters and a clock circuit. Its block diagram (Figure 11.1.2) shows these blocks: ALU; accumulator and register(s); internal RAM (holds the programs the user loads); stack pointer (holds an internal-RAM address); timer/counter (counts internal or external pulses); internal ROM (program memory, holds the fixed program); program counter; I/O port circuits (connect the chip to the outside world); interrupt circuits; and clock circuits.

Comparison between microprocessor and microcontroller

The two devices differ chiefly in what is on the chip and what the designer must add externally. The book's Table 11.1.1 sets them side by side (see the comparison table below).

The 8051 microcontroller

The 8051 is an 8-bit microcontroller with 128 bytes of RAM, 4 KB of on-chip ROM, two timers, one serial port and four 8-bit ports, all on a single chip. Two other members of the same family are the 8052 and the 8031. Its key feature quantities are collected in Table 11.1.2 (see below).

Architecture of the 8051

The 8051 block diagram (Figure 11.1.3) brings together the features that make it a microcontroller: internal ROM and RAM, I/O ports with programmable pins, timers/counters, serial data communication, a program counter, an ALU, working registers and clock circuits. In more detail the 8051 has:

  • a 16-bit Program Counter;
  • 4 KB of internal ROM or EPROM;
  • an 8-bit CPU with registers A (accumulator) and B;
  • 128 bytes of internal RAM;
  • 32 I/O pins arranged as four 8-bit ports, P0-P3;
  • two 16-bit timer/counters, T0 and T1;
  • two external and three internal interrupt sources;
  • oscillator and clock circuits;
  • the control registers TCON, TMOD, SCON, PCON, IP and IE;
  • a CISC (Complex Instruction Set Computer) architecture.

The 40-pin DIP pin configuration is shown in Figure 11.1.4, where pin numbers are printed inside the package and pin names outside. Many Port-3 pins carry alternate functions (for example RXD/TXD for the serial port on P3.0/P3.1, the external-interrupt inputs INT0/INT1 on P3.2/P3.3, the timer inputs T0/T1 on P3.4/P3.5, and the WR/RD strobes on P3.6/P3.7), while Port-0 and Port-2 double as the multiplexed address/data bus.

Note

The printed textbook describes the 32 I/O pins as "48-bit ports"; this is a misprint. The 8051 actually has four 8-bit ports (4 × 8 = 32 pins), namely P0, P1, P2 and P3. The correct description is used throughout this material.

Addressing modes

Because the CPU can keep data in registers, in memory or in an external source, an instruction can reach its data in several ways. The rules that describe how an operand is located are called addressing modes. In 8051 instructions the destination address is written first, followed by the source. The 8051 has five addressing modes: (1) register, (2) direct, (3) immediate, (4) indirect, and (5) base register plus index register; the first four are the ones commonly used. There are 28 opcodes that simply copy data from a source to a destination; the three main data-move opcodes are MOV destination,source; PUSH source / POP destination; and XCH destination,source. The MOV family moves data within the four physical parts of 8051 memory — internal RAM, internal Special Function Registers (SFRs), external RAM, and internal/external ROM — using the opcodes MOV, MOVX, MOVC, PUSH/POP and XCH.

1. Register addressing mode — registers A, DPTR, R0-R7, the accumulator and the carry bit appear as part of the opcode mnemonic, as the source or the destination. Moves happen between A and R0-R7, e.g. MOV A,R0 (copy R0 into A), MOV R2,A (copy A into R2), ADD A,R5 (add R5 to A) and MOV R5,A (save A in R5). The three least-significant bits of the opcode select which register is used (Table 11.1.3 shows this for MOV A,R7). A MOV never alters the source; it only copies.

2. Direct addressing mode — the whole 128 bytes of internal RAM and the SFRs can be addressed directly by a single-byte address. Internal RAM uses addresses 00H-7FH; the SFRs occupy 80H-FFH. The instruction MOV A,direct carries an opcode byte followed by the 8-bit direct address, and the on-chip memory map is shown in Figure 11.1.5. RAM addresses 00H-1FH are also the four banks of eight working registers R0-R7, so, for example, R2 of bank 2 can be reached in register mode as R2 or in direct mode as address 12H. The SFR addresses are listed in Table 11.1.4 and the register-bank direct addresses in Table 11.1.5. Worked instructions include MOV R0,40H, MOV 56H,A, MOV 80H,A (the same as MOV P0,A, because P0 is address 80H), MOV A,4 (the same as MOV A,R4) and MOV 0F0H,R2 (the same as MOV B,R2).

3. Indirect addressing mode — a register acts as a pointer to the data. For data inside the CPU only R0 and R1 may be used as pointers (R2-R7 cannot), and when used this way they must be preceded by the @ sign. Examples: MOV A,@R0 (copy the RAM byte whose address is in R0 into A), MOV @R1,B (copy B into the RAM byte whose address is in R1) and MOV @R1,90H. Figure 11.1.6 shows the internal 128 bytes of RAM (the upper 128 bytes, 80H-FFH, exist only on the 8032/8052).

4. Immediate addressing mode — the data itself is made part of the opcode, so it is immediately available within the instruction. A # sign marks immediate data, e.g. MOV A,#25H, MOV R4,#62, MOV DPTR,#4562H (16-bit) and MOV R7,#06H (8-bit). When the 8051 executes an immediate move, the program counter automatically advances to the byte(s) following the opcode.

5. Base register plus index register addressing mode — an effective address is formed by adding an index register to a base register (used with instructions such as MOVC A,@A+DPTR), giving a flexible way to reach look-up tables held in code memory.

Note

Throughout this chapter the printed book very often typesets the hexadecimal digit 0 (zero) as the capital letter "O" — for example it prints "ORG OH" for ORG 0H, and "OF1H" for 0F1H. It also prints the Special Function Registers IE (Interrupt Enable) and IP (Interrupt Priority) as "1E" and "1P" in its SFR table. These are print errors: hexadecimal numbers use the digit 0, and the standard register names are IE and IP. The correct forms are used here.

The 8051 instruction set

When a program runs it often has to hand control to a different location, so the 8051 provides a rich instruction set — 111 instructions in all — grouped into four categories: 1. data transfer, 2. arithmetic, 3. logical and 4. program-branching instructions. Every instruction is written as a mnemonic followed by a destination operand and a source operand (the "8051 instruction format").

Data-transfer instructions (MOV, MOVX, MOVC, PUSH, POP and the exchange instructions XCH/XCHD) move data about. They do not affect the PSW flags, except that a MOV or POP written directly to PSW can change the flags. Flags are 1-bit registers that record the result of certain instructions; they are gathered inside the Program Status Word (PSW) and Power Control (PCON) registers so they can be addressed conveniently. The stack instructions PUSH and POP name a direct address and move data to or from an internal-RAM area called the stack; the Stack Pointer (SP) holds the RAM address involved. A PUSH add first increments SP and then copies the byte at add into the RAM location pointed to by SP; a POP add copies the byte from the RAM location in SP back to add and then decrements SP (Figure 11.1.7). The exchange instructions move data both ways at once and always involve register A (for example XCH A,Rn, XCH A,add and XCH A,@R1); every addressing mode except immediate may be used. The complete list is Table 11.1.6. …

Definition 1Microprocessor

A microprocessor is the general-purpose Central Processing Unit (CPU) of a digital computer. It performs the processing but is not a complete computer by itself — it must be given external …

Definition 2Microcontroller

A microcontroller is a complete computer on a single chip. It contains a CPU together with on-chip ROM, RAM, parallel and serial I/O, timers/counters, and interrupt and clock circuits, so it can run a fixed appli …

Figure 3Block diagram of a microprocessor showing its ALU, accumulator over a working register, program counter, stack pointer, clock circuit and interrupt circuit as separate blocks.
Fig. 3 — Block diagram of a microprocessor showing its ALU, accumulator over a working register, program counter, stack pointer, clock circuit and interrupt circuit as separate blocks.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Depicts the internal organisation of a microprocessor CPU as separate blocks — ALU, accumulator over working register, program counter, stack pointer, clock circuit and interrupt circuit — making the point that a microprocessor is only the processing core and needs external memory and I/O. Matches …

Figure 4Block diagram of a microcontroller showing the ALU, accumulator and registers, internal RAM and ROM, stack pointer, timer/counter, two I/O ports, interrupt and clock circuits and the program counter all on one chip.
Fig. 4 — Block diagram of a microcontroller showing the ALU, accumulator and registers, internal RAM and ROM, stack pointer, timer/counter, two I/O ports, interrupt and clock circuits and the program counter all on one chip.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows the microcontroller as a single chip carrying every block a small computer needs — ALU, accumulator and register(s), internal RAM, stack pointer, timer/counter, internal ROM, program counter, I/O ports, interrupt circuits and clock circuits. Matches the t …

Table 5Comparison between microprocessor and microcontroller
MicroprocessorMicrocontroller
Contains ALU, program counter, stack pointer, working registers, interrupt circuits and a clock-timing circuit.Contains ALU, PC, SP, register, ROM, RAM, parallel I/O, serial I/O, counters and a clock circuit.
A designer using a general-purpose microprocessor must add RAM, ROM, I/O ports and a timer externally to make it functional.All are embedded together on the chip, so the designer cannot add any external memory, I/O or timer.
Most microprocessors have many opcodes for moving data from external memory to the CPU.Microcontrollers have one or two.
A microprocessor may have one or two types of bit-handling instruction.Microcontrollers have many.
The microprocessor is concerned with rapid movement of code and data from external addresses to the chip.The microcontroller is concerned with rapid movement of bits within the chip.
Table 6Features of the 8051
FeatureQuantity
ROM4 KB
RAM128 bytes
Timer2
I/O pins32
Serial port1
Interrupt sources6
Figure 7The 8051 block diagram showing the CPU, interrupt control, 4K-byte ROM, 128-byte RAM, timers, oscillator, bus control, four I/O ports P0-P3 and the serial port around a common internal bus.
Fig. 7 — The 8051 block diagram showing the CPU, interrupt control, 4K-byte ROM, 128-byte RAM, timers, oscillator, bus control, four I/O ports P0-P3 and the serial port around a common internal bus.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows the internal architecture of the 8051: the CPU and interrupt control, 4 KB ROM, 128 bytes RAM, Timer 0 and Timer 1, the oscillator, bus control, the four I/O ports P0-P3 (also the address/data bus) and the serial port (TXD/RXD), all tied to one internal bus. Matches the …

Figure 8Pin configuration of the 40-pin DIP 8051, with Port-1 pins 1-8, Port-3 with its alternate functions, reset and XTAL pins on the left, and Port-0, Port-2 and control pins such as EA, ALE and PSEN on the right.
Fig. 8 — Pin configuration of the 40-pin DIP 8051, with Port-1 pins 1-8, Port-3 with its alternate functions, reset and XTAL pins on the left, and Port-0, Port-2 and control pins such as EA, ALE and PSEN on the right.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows the 8051 as a 40-pin DIP: the left side carries P1.0-P1.7, RST, Port-3 (with alternate functions RXD, TXD, INT0, INT1, T0, T1, WR, RD), XTAL2, XTAL1 and GND; the right side carries Vcc, Port-0 (AD0-AD7), EA/Vpp, ALE/PROG, PSEN and Port-2 (A8-A15). Pin numbers are printed inside the package and names outside. Mat …

Definition 9Addressing mode

An addressing mode is the way an instruction specifies where its operand is located. Because data may sit in a register, in memory or in an external source, the addressing mode defines how that source is reached. The 8051 has five modes: register, direct, immediate, …

Table 10Opcode bit layout of the MOV A,R7 instruction

The single-byte opcode for MOV A,R7. The five most-significant bits are the opcode part and the three least-significant bits select the register:

Bitb7b6b5b4b3b2b1b0
Value11101111
Figure 11On-chip memory map used by 8051 direct addressing, showing the Special Function Registers from 80H to FFH stacked above the lower 128 bytes of on-chip RAM from 00H to 7FH.
Fig. 11 — On-chip memory map used by 8051 direct addressing, showing the Special Function Registers from 80H to FFH stacked above the lower 128 bytes of on-chip RAM from 00H to 7FH.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows the memory a direct address can reach: the Special Function Registers occupy 80H-FFH and sit above the lower 128 bytes of on-chip RAM, which run 00H-7FH. Matches the textbook's Figu …

Table 12SFR addresses of the 8051
SFRDirect address (hex)Bit addresses (hex)
A0E0H0E0-0E7
B0F0H0F0-0F7
DPL82H—
DPH83H—
IE0A8H0A8-0AF
IP0B8H0B8-0BF
P080H80-87
P190H90-97
P20A0H0A0-0A7
P30B0H0B0-0B7
PCON87H—
PSW0D0H0D0-0D7
SBUF99H—
SCON98H98-9F
SP81H—
TCON88H88-8F
TMOD89H—
TH08CH—
TL08AH—
TH18DH—
TL18BH—
Table 13Direct addresses of the four banks of working registers
BankR0R1R2R3R4R5R6R7
000H01H02H03H04H05H06H07H
108H09H0AH0BH0CH0DH0EH0FH
210H11H12H13H14H15H16H17H
318H19H1AH1BH1CH1DH1EH1FH
Figure 14Internal 128 bytes of 8051 RAM addressed by indirect addressing, noting that the upper 128 bytes (80H-FFH) exist only on the 8032/8052.
Fig. 14 — Internal 128 bytes of 8051 RAM addressed by indirect addressing, noting that the upper 128 bytes (80H-FFH) exist only on the 8032/8052.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows the internal 128 bytes of RAM that register-indirect addressing (@R0/@R1) can reach, and marks the upper 128 bytes (80H-FFH) as present only on the 8032/8052, with the SFR space not reachable by indirect addressing. Matches the textb …

Figure 15Memory map of the 8051 internal RAM divided into register banks 0-3 (00H-1FH), bit-addressable RAM (20H-2FH) and scratch-pad RAM (30H-7FH).
Fig. 15 — Memory map of the 8051 internal RAM divided into register banks 0-3 (00H-1FH), bit-addressable RAM (20H-2FH) and scratch-pad RAM (30H-7FH).

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Shows how the 128 bytes of 8051 internal RAM are organised: the four register banks R0-R7 occupy 00H-1FH (bank 1, 08H-0FH, doubles as the default stack), the bit-addressable area is 20H-2FH, and the general-purpose scratch-pad RAM is 30H-7FH. This underpins register and direct addressing. Matches the textbook's ch …

Figure 16Diagram of PUSH and POP on the 8051 stack, showing the stack pointer incremented before each PUSH writes a byte into internal RAM and decremented after each POP reads it back.
Fig. 16 — Diagram of PUSH and POP on the 8051 stack, showing the stack pointer incremented before each PUSH writes a byte into internal RAM and decremented after each POP reads it back.

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

Illustrates stack operation: on a PUSH the stack pointer is first incremented and the byte is written into internal RAM; on a POP the byte is read from the RAM location in SP and the stack pointer is then decremented. Matches the textbo …

Table 178051 data-transfer instructions
MnemonicDescriptionByteOscillator period
MOV A,RnMove register to accumulator112
MOV A,directMove direct byte to accumulator212
MOV A,@RiMove indirect RAM to accumulator112
MOV A,#dataMove immediate data to accumulator212
MOV Rn,AMove accumulator to register112
MOV Rn,directMove byte to register224
MOV direct,directMove direct byte to direct324
MOV DPTR,#data16Load data pointer with a 16-bit constant324
MOV A,@A+DPTRMove code byte relative to DPTR to accumulator124
MOV A,@A+PCMove code byte relative to PC to accumulator124
MOVX A,@RiMove external RAM (8-bit address) to accumulator124
MOVX @Ri,AMove accumulator to external RAM (8-bit address)124
MOVX @DPTR,AMove accumulator to external RAM (16-bit address)124
PUSH directPush direct byte onto stack224
POP directPop direct byte from stack224
XCH A,RnExchange register with accumulator112
XCH A,@RiExchange indirect RAM with accumulator112
XCHD A,@RiExchange low-order nibble of indirect RAM with accumulator112
Table 188051 arithmetic instructions
MnemonicDescriptionByteOscillator period
ADD A,RnAdd register to accumulator112
ADD A,directAdd direct byte to accumulator212
ADD A,@RiAdd indirect RAM to accumulator112
ADD A,#dataAdd immediate data to accumulator212
ADDC A,RnAdd register to accumulator with carry112
ADDC A,directAdd direct byte to accumulator with carry212
ADDC A,@RiAdd indirect RAM to accumulator with carry112
ADDC A,#dataAdd immediate data to accumulator with carry212
SUBB A,RnSubtract register from accumulator with borrow112
SUBB A,directSubtract direct byte from accumulator with borrow212
SUBB A,@RiSubtract indirect RAM from accumulator with borrow112
SUBB A,#dataSubtract immediate data from accumulator with borrow212
INC AIncrement accumulator112
INC RnIncrement register112
INC directIncrement direct byte212
INC @RiIncrement indirect RAM112
DEC ADecrement accumulator112
DEC RnDecrement register112
DEC directDecrement direct RAM212
DEC @RiDecrement indirect RAM112
Table 198051 logical instructions
MnemonicDescriptionByteOscillator period
ANL A,RnAND register to accumulator112
ANL A,directAND direct byte to accumulator212
ANL A,@RiAND indirect RAM to accumulator112
ANL A,#dataAND immediate data to accumulator212
ANL direct,AAND accumulator to direct byte212
ANL direct,#dataAND immediate data to direct byte324
ORL A,RnOR register to accumulator112
ORL A,directOR direct byte to accumulator212
ORL A,@RiOR indirect RAM to accumulator112
ORL A,#dataOR immediate data to accumulator212
ORL direct,AOR accumulator to direct byte212
ORL direct,#dataOR immediate data to direct byte324
XRL A,RnExclusive-OR register to accumulator112
XRL A,directExclusive-OR direct byte to accumulator212
XRL A,@RiExclusive-OR indirect RAM to accumulator112
XRL A,#dataExclusive-OR immediate data to accumulator112
XRL direct,AExclusive-OR accumulator to direct byte212
XRL direct,#dataExclusive-OR immediate data to direct byte324
CLR AClear accumulator112
CPL AComplement accumulator112
RL ARotate accumulator left112
RLC ARotate accumulator left through the carry112
RR ARotate accumulator right112
Table 208051 program-branching (jump and call) instructions
MnemonicDescription
ACALL addr11Absolute subroutine call
LCALL addr16Long subroutine call
RETReturn from subroutine
AJMP addr11Absolute jump
LJMP addr16Long jump
SJMP relShort jump (relative address)
JMP @A+DPTRJump indirect relative to DPTR
JZ relJump if accumulator is zero
JNZ relJump if accumulator is not zero
CJNE A,direct,relCompare direct byte to accumulator and jump if not equal
CJNE A,#data,relCompare immediate to accumulator and jump if not equal
CJNE Rn,#data,relCompare immediate to register and jump if not equal
CJNE @Ri,#data,relCompare immediate to indirect and jump if not equal
DJNZ Rn,relDecrement register and jump if not zero
DJNZ direct,relDecrement direct byte and jump if not zero
NOPNo operation