Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Click the blue text to follow us

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

1. Overview of the Microcontroller Instruction System

1. Instruction Overview
Instructions: Commands executed by the CPU based on human intention to perform certain operations.
Instruction System: The set of all instructions that a computer can execute.
Program: A sequence of instruction operations compiled according to human requirements.
This chapter mainly introduces the assembly language instruction system of the 80C51 microcontroller.

Programming Language: A set of rules for writing programs.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System
Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Three Types of Programming Languages:

Machine Language, Assembly Language, High-Level Language

Machine Language: A language represented by binary codes for each instruction, directly recognizable and executable by the computer.

Assembly Language: A programming language that represents instructions using mnemonics, symbols, and numbers. It corresponds one-to-one with machine language instructions.

2. Instruction Format and Representation
80C51 Assembly Language Instruction Format
[Label:] Opcode [Destination Operand][, Source Operand][; Comment]

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

3. Common Symbols in Instructions

(1)Rn(n=0~7) The currently selected working registers R0~R7

(2)Ri(i=0,1) The currently selected registers R0, R1 used as address pointers

(3)#data 8bit immediate value

(4)#data16 16bit immediate value

For example: MOV DPTR, #data16

(5)direct 8bit direct address of internal RAM unit (including SFR)

For example: MOV direct, #data ; #datadirect

(6)addr11

11bit destination address, used in ACALL and AJMP instructions

(7)addr16

16bit destination address, used in LCALL and LJMP instructions

(8)rel 8-bit address offset in two’s complement form

(9) bit bit address for direct addressing of internal RAM or SFR

(10)@ Symbol representing indirect addressing register

(11) / Bitwise NOT operation, for example: ANL C, /P1.2

(12)(×) Content in “×” is data; × is the address

(13)(( ×)) Content in the indirect addressing unit of “×”, the content in “×” is the address

For example: if the data in cell 58H is 36H, the content of R0 is 58H

INC 58H ; (58H)+1 58H

DEC @R0 ; ( (R0) )-1 (R0)

(14) Indicates that the content on the left side of the arrow is transferred to the unit on the right side of the arrow

4. The number of bytes and cycles of instructions

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

2. Addressing Modes of the 80C51 Microcontroller

Addressing mode: The method of finding the operand or operand address in the instruction.
The 80C51 microcontroller has seven addressing modes:
1. Immediate Addressing
The operand is in the instruction, after the opcode. Denoted by “#”.
The operand can be 8 bits or 16 bits.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

2. Direct Addressing
Direct addressing: The address of the operand’s storage unit is given directly in the instruction. The operand part of the instruction is the address of the operand.
The direct addressing mode can access 128 units of internal RAM and all SFRs. For SFRs, both their addresses and names can be used.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

3. Register Addressing

The operand is in a certain register.

The registers used can be: R0~R7, A, B, DPTR, etc.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

4. Register Indirect Addressing
The content in the register gives the address of the operand.
The 80C51 specifies that: R0, R1, and DPTR can be used as indirect addressing registers.
(1) R0 and R1 access the low 128B of internal RAM or the low 256B of external RAM
For example: if (R0)=65H, (65H)=47H
MOV A, @R0 ; (65H) A, the value in A is 47H
MOV @R0, #3AH ; 3AH 65H
MOVX A, @R0 ; External RAM’s (65H) A

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

(2) DPTR accesses the 64KB space of external RAM
For example: if (DPTR)=2000H
MOVX @DPTR, A ; Aexternal RAM’s 2000H unit
MOVX A, @DPTR ; external RAM’s 2000H unit A
5. Indexed Addressing
Indexed addressing: Using the content of a certain register as a base address, adding an address offset to this base address forms the actual operand address.
In the 80C51, DPTR or PC is used as the index register, and the content of A is the address offset.
Indexed addressing can only access program memory, with a range of 64KB.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

6. Relative Addressing
Relative addressing: The actual transfer address is generated by adding the current PC value to the offset rel specified in the instruction.
Relative addressing only appears in relative transfer instructions.
The current PC value refers to the PC value after executing the relative instruction; the address of the relative transfer instruction is called the source address; the address after the transfer is called the destination address.
Destination address = Source address + Relative transfer instruction byte count + rel
rel is a signed number represented in two’s complement.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

7. Bit Addressing

Bit addressing: The operand of the instruction using bit addressing is a certain bit in an 8-bit binary number, and the bit address is given in the instruction. The bit address is represented in the instruction using bit.

For example: CLR bit

MOV ACC.0, 11H ; ACC.0←(11H)

Two methods of representing bit addresses:

Directly use the bit address, such as D3H;

Directly use the register name plus bit number, such as PSW.3.

Bit addressing area:

The 128 bits in the 16 units of internal RAM from 20H to 2FH;

Byte addresses that can be divided by 8 in SFR.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

3. Introduction to Instruction Classification of the 80C51 Microcontroller

By byte count: 49 single-byte instructions, 46 double-byte instructions, and 16 three-byte instructions.
By execution time: 64 single machine cycle instructions, 45 double-cycle instructions, and 2 four-cycle instructions (multiplication, division instructions)
By function: The instruction system of the 80C51 is divided into five categories:
Data transfer instructions (29 instructions)
Arithmetic operation instructions (24 instructions)
Logical operation and shift instructions (24 instructions)
Control transfer instructions (17 instructions)
Bit operations or Boolean operations (17 instructions)

Data Transmission Instructions

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Internal RAM data transfer instruction group

(1) Instructions with A as the destination operand (4 instructions)

MOV A, Rn ; (Rn)A

MOV A, direct ; (direct)A

MOV A, @Ri ; ((Ri))A

MOV A, #data ; dataA

For example: MOV A, R2

MOV A, 30H

MOV A, @R0

MOV A, #36H

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

(2) Instructions with Rn as the destination operand (3 instructions)

MOV Rn, A ; (Rn) Rn

MOV Rn, direct ; (direct) Rn

MOV Rn, #data ; data Rn

For example: MOV R0, A

MOV R3, 30H

MOV R7, #36H

MOV R1, #30

MOV R6, #01101100B

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

(3) Instructions with direct address as the destination operand (5 instructions)

MOV direct, A ; (A)direct

MOV direct, Rn ; (Rn)direct

MOV direct, direct ; (source direct) destination direct

MOV direct, @Ri ; ((Ri))direct

MOV direct, #data ; datadirect

For example: MOV 30H, A

MOV P1, R2

MOV 38H, 60H

MOV TL0, @R1

MOV 58H, #36H

(4) Instructions with indirect address as the destination operand (3 instructions)

MOV @Ri, A ; (A)(Ri)

MOV @Ri, direct ; (direct)(Ri)

MOV @Ri, #data ; data(Ri)

For example: MOV @R0, A

MOV @R1, 36H

MOV @R1, #48

MOV @R0, #0D6H

(5) Sixteen-bit data transfer instruction (1 instruction)

MOV DPTR, #data16 ; dataHDPH, dataLDPL

For example: MOV DPTR, #2368H

MOV DPTR, #35326

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

1. Data transfer instructions between accumulator A and external RAM (4 instructions)

MOVX A, @Ri ; ((Ri))A, and set /RD=0

MOVX A, @DPTR ; ((DPTR))A, and set /RD=0

MOVX @Ri, A ; (A)à(Ri), and set /WR=0

MOVX @DPTR, A ; (A)à(DPTR), and set /WR=0

Note:

Instructions 2 and 4 use DPTR as a 16-bit address pointer for external RAM, with an addressing range of 64KB;
Instructions 1 and 3 use R0 or R1 as the low 8-bit address pointer, sent out by port P0, with an addressing range of 256B (the high 8 bits are determined by P2).

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

2. Program memory data transfer instruction group (2 instructions)

MOVC A, @A+DPTR ; ((A)+(DPTR))A

MOVC A, @A+PC ; ((A)+(PC))A

Program memory can only be read, not written;

These two instructions are commonly used for table lookup operations, both are single-byte instructions;

For the instruction based on DPTR, the addressing range is the entire ROM’s 64KB space; for the instruction based on PC, it can only read a certain unit within 256 address units starting from the current MOVC instruction, because the PC value is relatively fixed during program execution.

3. Stack operation instructions (2 instructions)

PUSH direct ; first (SP)+1SP, then (direct)(SP)

POP direct ; first ((SP)) direct, then (SP)-1SP

For example: PUSH 0E0H ; actually (A)(SP)

POP 05H ; actually ((SP))R5

Note: The operand must be a direct address, and cannot use register names.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

4. Exchange Instructions (5 instructions)

(1) Full byte exchange instructions

XCH A, Rn ; (A) (Rn)

XCH A, direct ; (A) (direct)

XCH A, @Ri ; (A) (((Ri)))

(2) Half-byte exchange instructions

Low half-byte exchange instructions

XCHD A, @Ri ; (A0~3) (((Ri)0~3))

For example: if (A)=36H, (R1)=65H, (65H)=42H

XCHD A, @R1 ;

Execution result: (A)=32H, (65H)=46H

Accumulator A high and low half-byte exchange instructions

SWAP A ; (A0~3) (A4~7)

For example: if (A)=36H

SWAP A ; (A)=63H

Basics and Applications of Microcontrollers | 04 80C51 Instruction System
Tips
Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Utilize English abbreviations to memorize instruction functions

For example: MOV is the abbreviation for MOVE, the general format of the instruction is:

MOV ,

Illegal instructions cannot be used for programming

Note that the following instructions are illegal instructions:

MOV Rn, Rn

MOV @Ri, @Ri

MOV Rn, @Ri

MOV #data, A

Arithmetic Operation Instructions
Includes addition, subtraction, multiplication, and division operations; the first operand is generally A; usually affects the flags CY, AC, OV, and P.
A total of 24 instructions, divided into seven small categories.
1. Carry-free addition instructions (4 instructions)
ADD A, Rn ; (A)+(Rn)A
ADD A, direct ; (A)+(direct)A
ADD A, @Ri ; (A)+((Ri))A
ADD A, #data ; (A)+#dataàA
2. Carry-inclusive addition instructions (4 instructions)
ADDC A, Rn ; (A)+(Rn)+CYA
ADDC A, direct ; (A)+(direct)+CY A
ADDC A, @Ri ; (A)+((Ri))+CY A
ADDC A, #data ; (A)+#data+CY A
3. Borrowing subtraction instructions (4 instructions)
SUBB A, Rn ; (A)-CY-(Rn)A
SUBB A, direct ; (A)-CY-(direct)A
SUBB A, @Ri ; (A)-CY-((Ri))A
SUBB A, #data ; (A)-CY-#data A
4. Multiplication and division instructions (2 instructions)
Multiplication instruction
MUL AB ; (A)×(B)B15~8, A7~0
Explanation: (1) This is unsigned multiplication;
(2) If the result’s B≠0, then OV=1;
If B=0, then OV=0; CY=0.
Division instruction
DIV AB ; (A)/(B)’s quotientA, remainderB
Explanation: (1) This is unsigned division;
(2) If the divisor B=0, then OV=1;
If B≠0, then OV=0; CY=0.
5. Increment instructions (5 instructions)
INC A ; (A)+1 A
INC Rn ; (Rn)+1 Rn
INC direct ; (direct)+1 direct
INC @Ri ; ((Ri))+1 ((Ri))
INC DPTR ; (DPTR)+1DPTR
Explanation: This type of instruction does not affect flags CY, AC, and OV
6. Decrement instructions (4 instructions)
DEC A ; (A)-1 A
DEC Rn ; (Rn)-1 Rn
DEC direct ; (direct)-1 direct
DEC @Ri ; ((Ri))-1 ((Ri))
Explanation: This type of instruction does not affect flags CY, AC, and OV
7. Decimal adjustment instruction (1 instruction)
DA A ; Adjust the contents of the accumulator to BCD (compressed) code
Explanation:
(1) This instruction follows ADD or ADC instructions, adjusting the sum in A to BCD code, and the two operands of ADD or ADC are BCD codes;
(2) Adjustment method:
If (A0~3)>9 or AC=1, then (A0~3)+6(A0~3);
If (A4~7)>9 or CY=1, then (A4~7)+6(A4~7);
(3) Effect on flags: If result A>99, then CY=1; does not affect OV.
Basics and Applications of Microcontrollers | 04 80C51 Instruction System
Tips
Basics and Applications of Microcontrollers | 04 80C51 Instruction System

1. Grasp the similarities of various instructions for comparative learning

2. Pay attention to the effect of instruction execution on the Program Status Word (PSW)

3. Be aware of the restrictions on using instructions

Logical Operation Instructions

Includes AND, OR, XOR, clear, complement, shift, etc. These instructions generally do not affect the flags CY, AC, and OV, with a total of 24 instructions.

1. Clear accumulator A to 0 and complement instructions (2 instructions)

(1) Clear accumulator A instruction

CLR A ; 0 A

Explanation: Only affects flag P.

(2) Complement accumulator A instruction (bitwise complement)

CPL A ; (/A) A, equivalent to 0FFH – A A

Explanation: Does not affect flags.

For example: (A)=56H CPL A ; Result is 0A9H

2. Shift instructions (4 instructions)

(1) Accumulator A rotates left

RL A ;

(2) Accumulator A rotates right

RR A ;

(3) Accumulator A rotates left with carry

RLC A ;

(4) Accumulator A rotates right with carry

RRC A ;

Explanation:

(1) Each instruction only moves one bit at a time;

(2) Left shifting one bit is equivalent to multiplying by 2; right shifting one bit is equivalent to dividing by 2;

(3) Carry bit shifting affects flags CY and P.

3. Logical AND instructions (6 instructions)

ANL A, Rn ; (A)∧(Rn)A

ANL A, direct ; (A)∧(direct)A

ANL A, @Ri ; (A)∧((Ri))A

ANL A, #data ; (A)∧#data A

ANL direct, A ; (direct)∧(A)direct

ANL direct, #data ; (direct)∧#data direct

Explanation:

  • The destination operand can only be A or direct;

  • The first four instructions only affect flag P; the last two do not affect flags;

  • Logical AND operation mainly serves as a masking function, allowing certain bits to be ANDed with “0”, while others are ANDed with “1”.

4. Logical OR instructions (6 instructions)

ORL A, Rn ; (A)∨(Rn)A

ORL A, direct ; (A)∨(direct)A

ORL A, @Ri ; (A)∨((Ri))A

ORL A, #data ; (A)∨#dataA

ORL direct, A ; (direct)∨(A)direct

ORL direct, #data ; (direct)∨#datadirect

Explanation:

(1) The destination operand can only be A or direct;

(2) The first four instructions only affect flag P; the last two do not affect flags.

(3) OR operation is often used to set certain bits to 1, commonly serving a “merging function”.

5. Logical XOR instructions (6 instructions)

XRL A, Rn ; (A)∨(Rn)A

XRL A, direct ; (A)∨(direct)A

XRL A, @Ri ; (A)∨((Ri))A

XRL A, #data ; (A)∨#dataA

XRL direct, A ; (direct)∨(A)direct

XRL direct, #data ; (direct)∨#datadirect

Explanation:

(1) The destination operand can only be A or direct;

(2) The first four instructions only affect flag P; the last two do not affect flags.

(3) XOR operation is commonly used to complement certain bits. XORing a register’s value with 0FFH can achieve the function of complementing that register.

Bit Operation Class Instructions

Includes bit variable transfer, logical operations, control transfer, etc., with a total of 17 instructions, divided into 4 small categories.Only some instructions affect the CY flag.

Methods of representing bit addresses:

Directly use the bit address, such as: D4H

Use special function register name plus bit number, such as: PSW.4

Use bit name, such as: RS1

Use named bit addresses defined by bit, such as: SUB bit RS1, MM bit 02H

1. Bit data transfer instructions (2 instructions)

MOV C, bit ; (bit)C

MOV bit, C ; (C)bit

For example: MOV C, TR0

MOV 08H, C ; Temporarily store CY content

MOV C, 10H ; Send 10H bit to CY

MOV 5AH, C ; Send CY content to 5AH

MOV C, 20H ; Restore CY content

2. Bit modification instructions (6 instructions)

(1) Bit clear to 0 instruction

CLR C ; 0C

CLR bit ; 0(bit)

For example: CLR TR0

(2) Bit set to 1 instruction

SETB C ; 1C

SETB bit ; 1(bit)

For example: SETB TR0

SETB 06H ;

(3) Bit complement instruction

CPL C ; (/C)C

CPL bit ; (/bit)bit

For example: CPL TR0

CPL EA

3. Bit logical operation instructions (4 instructions)

(1) Bit logical AND instruction

ANL C, bit ; (C)∧(bit)C

ANL C, /bit ; (C)∧(/bit)C

For example: ANL C, P1.0

ANL C, /P1.2

(2) Bit logical OR instruction

ORL C, bit ; (C)∨(bit)C

ORL C, /bit ; (C)∨(/bit)C

For example: ORL C, P1.0

ORL C, /P1.2

4. Bit conditional transfer instructions (5 instructions)

(1) Conditional transfer instructions based on C value

JC rel ; (PC)+2PC

; If (C)=1, then (PC)+relPC

; If (C)=0, then continue executing downwards

JNC rel ; (PC)+2PC

; If (C)=0, then (PC)+relPC

; If (C)=1, then continue executing downwards

For example: JC NEXT1

JNC FIRST

(2) Conditional transfer instructions based on bit value

JB bit, rel ; (PC)+3PC

; If (bit)=1, then (PC)+relPC

; If (bit)=0, then continue executing downwards

JNB bit, rel ; (PC)+3PC

; If (bit)=0, then (PC)+relPC

; If (bit)=1, then continue executing downwards

For example: JB BA, NEXT1

JNB TI, $

(3) Conditional transfer instructions that clear bit value to 0

JBC bit, rel ; (PC)+3PC

; If (bit)=1, then (PC)+relPC, 0bit

; If (bit)=0, then continue executing downwards

Basics and Applications of Microcontrollers | 04 80C51 Instruction System
Tips
Basics and Applications of Microcontrollers | 04 80C51 Instruction System

1. Clarify the limitations of relative transfer instructions

Relative transfer instructions fall into two categories: One category is relative transfer instructions within a 2KB range (such as AJMP addr11 instructions), where the target transfer address generated after instruction execution must be within the same 2KB range as the address in the program counter PC; the other category includes transfer instructions containing rel in the instruction code.

2. Calling instructions include long calling instructions and short calling instructions.

Long calling instructions can call any subroutine within a 64KB range;

Short calling instructions can only call subroutines whose starting addresses are within the same 2KB range as the starting address of the short calling instruction.

3. Calling instructions and return instructions must be used in pairs.

Basics and Applications of Microcontrollers | 04 80C51 Instruction System

Long press the image to follow

Discover more exciting content

WeChat ID : Mechanical-knowledge

Leave a Comment