The 8051 microcontroller has a total of 111 instructions, which are classified into five categories according to their functions:
1. Data transfer instructions (28 instructions)
2. Arithmetic operation instructions (24 instructions)
3. Logical operation and shift instructions (25 instructions)
4. Bit operation instructions (17 instructions)
5. Control transfer instructions (17 instructions)
Data transfer instructions
Data transfer instructions: transfer the source operand to the target address specified by the instruction lock
Mnemonics | Function | use |
---|---|---|
MOV | Access internal RAM, access special function registers | MOV A, Rn; (Rn) → A, which means to transfer the content in Rn to A |
MOVX | Accessing External RAM | MOVX A, @DPTR; ((DPTR)) → A MOVX @DPTR, A; (A) → (DPTR) MOVX A, @Rn; ((Rn)) → A MOVX @Rn, A; (A) → (Rn) |
MOVC | Accessing program memory | MOVC A, @A+DPTR; ((A)+(DPTR))→A MOVC A, @A+PC; ((A)+(PC))→A |
XC | Byte Swap | XCH A, Rn; (A) → Rn, (Rn) → A XCH A, direct; (A) → direct, (direct) → A XCH A, @Rn; (A) → (Rn), (Rn) → A |
XC | Nibble Swap | XCHD A, @Rn; the upper 4 bits remain unchanged, the lower 4 bits are swapped; (A) 3-0→(Rn) 3-0, ((Rn)) 3-0→A 3-0 |
PUSH | Push Operation | PUSH direct; (SP)+1 → SP, (direct) → (SP) |
POP | Pop operation | POP direct; (direct) → (SP), (SP) + 1 → SP |
Arithmetic instructions
There are 24 arithmetic operation instructions, including four basic arithmetic operation instructions: addition, subtraction, multiplication, and division. There are eight mnemonics used in arithmetic instructions: ADD, ADDC, INC, SUBB, DEC, DA, MUL, and DIV.
1. Ordinary addition instruction (ADD)
ADD | A,Rn | ;A←(A)+(Rn) The data in the accumulator is added to the data in the register and sent to the accumulator |
ADD | A, direct | ;A←(A)+(direct) The accumulator is added to the data in the direct addressing unit and sent to the accumulator |
ADD | A, @Ri | ;A←(A)+((Ri)) The data in the accumulator and the indirect addressing unit are added and sent to the accumulator |
ADD | A, #data | ;A←(A)+data The data in the accumulator is directly added to the immediate value and sent to the accumulator |
2. Addition instruction with carry
ADDC | A,Rn | ;A←(A)+(Rn)+(Cy) |
ADDC | A, direct | ;A←(A)+(direct)+(Cy) |
ADDC | A, @Ri | ;A←(A)+((Ri))+(Cy) |
ADDC | A, #data | ;A←(A)+data+(Cy) |
3. Add 1 instruction
INC | A | ;A←(A)+1 Data in accumulator (A)+1 |
INC | R | ;Rn←(Rn)+1 Register data +1 |
INC | direct | ;direct←(direct)+1 Data in direct addressing unit +1 |
INC | @Ri | ;Ri←((Ri))+1 Data in indirect addressing unit +1 |
INC | DPTR | ;DPTR←(DPTR)+1 data pointer+1 |
4. Subtraction instruction with borrow
SUBB | A,Rn | ;A←(A)-(Rn)-(Cy) |
SUBB | A, direct | ;A←(A)-(direct)-(Cy) |
SUBB | A, @Ri | ;A←(A)-((Ri))-(Cy) |
SUBB | A, #data | ;A←(A)-data-(Cy) |
5. Subtract 1 instruction
DEC | A | ;A←(A)-1 Data in accumulator (A) -1 |
DEC | R | ;Rn←(Rn)-1 Register data -1 |
DEC | direct | ;direct←(direct)-1 Data in direct addressing unit -1 |
DEC | @Ri | ;Ri←((Ri))-1 Data in indirect addressing unit -1 |
6. Multiplication Instructions
MUL | AB | ;(A)×(B)→ BA |
7. Division Instructions
DIV | AB | ;(A)/(B)→ A(quotient)B(remainder) |
Logical operation instructions
Logical operation instructions include: logical operation instructions and shift instructions.
1. Accumulator A clear and invert instructions
CLR | A | ; Clear accumulator A to "0" |
CPL | A | ; Invert the accumulator A bit by bit |
2. Logic and instructions
ANL | A,Rn | ;A←(A)∧(Rn) The accumulator and register data are ANDed and sent to the accumulator |
ANL | A, direct | ;A←(A)∧(direct) The accumulator and the data of the direct addressing unit are ANDed and sent to the accumulator |
ANL | A, @Ri | ;A←(A)∧(Ri) The accumulator and the indirect addressing unit data are ANDed and sent to the accumulator |
ANL | A, #data | ;A←(A)∧data The accumulator and the immediate value are ANDed and sent to the accumulator |
ANL | direct, A | ;direct←(direct)∧(A) The accumulator and the data of the direct addressing unit are ANDed and sent to the direct addressing unit |
ANL | direct,#data | ;direct←(direct)∧data The data in the direct addressing unit is ANDed with the immediate value and sent to the direct addressing unit |
3. Logic or instruction
ORL | A,Rn | ;A←(A)∨(Rn) The accumulator and register data are ORed and sent to the accumulator |
ORL | A, direct | ;A←(A)∨(direct) The accumulator and the direct addressing unit data are ORed into the accumulator |
ORL | A, @Ri | ;A←(A)∨(Ri) The accumulator and the indirect addressing unit data are ORed and sent to the accumulator |
ORL | A, #data | ;A←(A)∨data The accumulator and the immediate data are ORed into the accumulator |
ORL | direct, A | ;direct←(direct)∨(A) The accumulator is ORed with the data of the direct addressing unit and sent to the direct addressing unit |
ORL | direct,#data | ;direct←(direct)∨data The data in the direct addressing unit is ORed with the immediate value and sent to the direct addressing unit |
4. Logical XOR instruction
XRL | A,Rn | ;A←(A)⊕(Rn) The accumulator and register data are different or sent to the accumulator |
XRL | A, direct | ;A←(A)⊕(direct) The accumulator and the directly addressed unit data are different or sent to the accumulator |
XRL | A, @Ri | ;A←(A)⊕(Ri) The data in the accumulator and the indirect addressing unit are different or sent to the accumulator |
XRL | A, #data | ;A←(A)⊕data The accumulator is different from the immediate value or is sent to the accumulator |
XRL | direct, A | ;direct←(direct)⊕(A) The accumulator and the directly addressed unit have different data or are sent to the directly addressed unit |
XRL | direct,#data | ;direct←(direct)⊕data The data in the direct addressing unit is different from the immediate value or is sent to the direct addressing unit |
5. Shift instructions
RL | A | ; The content of accumulator A is shifted left by 1 bit |
RLC | A | ; The content of accumulator A is circularly shifted left by 1 bit with the carry flag |
RR | A | ; The content of accumulator A is rotated right by 1 bit |
RRC | A | ; The content of accumulator A is shifted right by 1 bit with the carry flag |
Circular left shift instruction example diagram: RL A A7←A6←A5←A4←A3←A2←A1←A0 ↓ →---------------------------------------------↑
Example of circular right shift instruction with carry: RRC A
CY→A7→A6→A5→A4→A3→A2→A1→A0
↑ --------------------------------------------------←↓
Bit operation instructions
The 89C51 microcontroller has a rich set of bit operation instructions that can complete operations such as transmission, calculation, and control transfer with bit variables as the object.
1. Bit data transmission instructions
MOV | C, bit | ;Cy←(bit) Send the content of the direct address bit to the accumulator CY |
MOV | bti, C | ;bit→(Cy) transfer the contents of CY to the directly addressed bit |
Note: There is no direct transfer instruction between two addressable bits. If you want to complete this transfer, you can use CY as a medium for indirect transfer.
Example: To transfer the number at 30H to 20H, execute the following instructions:
MOV C, 30H;
MOV 20H, C;
2. Bit variable modification instructions
CLR | bit | ;bit←0 clear the bit address |
CPL | C | ;Cy←(Cy inverted) invert the bit accumulator CY |
CPL | bit | ;bit←(bit inversion) invert the bit address |
SETB | C | ;Cy←1 Set the bit accumulator CY to 1 |
SETB | bit | ;bit←1 Set the address bit to 1 |
**3. Bit variable logic instructions**
ANL | C, bit | ;Cy←(Cy)∧(bit) The bit accumulator CY is ANDed with the bit address and sent to bit CY |
ORL | C, bit | ;Cy←(Cy)∨(bit) The bit accumulator CY is ORed with the bit address and sent to bit CY |
Control transfer instructions
The function of control transfer instructions is to transfer the control program from the original sequential execution address to other instruction addresses. This transfer can be completed through program jump, subroutine call, subroutine return, etc.
1. Unconditional transfer instruction
The function of the unconditional transfer instruction is: when the program executes the unconditional transfer instruction, the program will unconditionally transfer to the address provided by the instruction.
instruction | illustrate |
---|---|
JMP rel | By default, this is equivalent to SJMP rel |
SJMP rel | Rel is an 8-bit signed number, and the transfer range is -128~+127 of the current PC value, a total of 256 units |
AJMP addr11 | addr11 is sent to PC10~PC0, while PC15~PC11 remain unchanged, the transfer range is 2KB |
LJMP addr16 | addr16 is loaded into PC, transfer range 64KB |
2. Conditional transfer instructions
The conditional transfer instruction is an instruction that transfers according to a certain condition.
When the condition is met, it transfers to a new address (label).
If the condition is not met, the next instruction is executed in sequence.
Classification | instruction | illustrate |
---|---|---|
Accumulator A is judged as 0 and transferred | JZ rel | (A) = 0 transfer to PC+rel for execution, otherwise execute sequentially |
Accumulator A is judged as 0 and transferred | JNZ rel | (A) ≠ 0, transfer to PC+rel for execution, otherwise execute sequentially |
Bit state transition | JB bit,rel | Bit = 1 transfer to PC+rel for execution, otherwise sequential execution |
Bit state transition | JNB bit,rel | bit ≠ 1 transfer to PC+rel for execution, otherwise execute sequentially |
Bit state transition | JBC bit,rel | If bit = 1, transfer to PC+rel for execution, and set this bit to 0, otherwise, execute sequentially. |
Bit state transition | JC rel | Carry bit CY = 1 transfer to PC + rel for execution, otherwise sequential execution |
Bit state transition | JNC rel | Carry bit CY ≠ 1 transfer to PC+rel for execution, otherwise sequential execution |
3. Compare transfer instructions
CJN | A, direct, rel | ;(A) ≠ (direct) transfer |
CJN | A,#data,rel | ;(A) ≠ data transfer |
CJN | Rn, #data, rel | ;(Rn) ≠ data transfer |
CJN | @Ri, #data,rel | ;((Ri)) ≠ data transfer |
4. Loop transfer instructions
DJNZ | Rn,rel | ; Register Rn is decremented by 1 and is not 0. Loop transfer |
DJNZ | direct, rel | ; Direct addressing unit direct minus 1 is not 0 loop transfer |
Instruction time calculation
It takes a certain amount of time for the microcontroller to execute each instruction. Assuming that it takes time T to execute an instruction, it takes time N×T to loop the instruction N times. In actual programming, we often use the instruction DJNZ to achieve the effect of software demonstration.
Example: Assuming that a DJNZ instruction takes 2us and a MOV instruction takes 1us, how long does it take in total to execute the following program?
MOV R7, #200; Execute once DL1: MOV R6, #250; Execute 200 times DL2: DJNZ R6, DL2; Execute 200×250 times DJNZ R7, DL1; Execute 200 times 1234
From the above, it can be inferred that the running time of the entire program is
T = 201×1+200×251×2=100601us=100.601s
Previous article:2: The first 51 single-chip microcomputer assembly experiment
Next article:IV: Output application of IO port
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- 【micropython】Adding software timer in STM32
- EEWORLD University ---- ST MEMS sensor application demonstration video
- Analog Discovery 2 review
- What is the purpose of connecting a 10uF capacitor after the op amp output?
- Who needs WiFi 7?
- Composting Fermentation Control Based on GD32E231
- Grounding of Switching Regulators
- EEWORLD University ---- Basic knowledge of TID
- Commonly used algorithms for drones - Kalman filter (10)
- If the test posture is not up to standard and the result is deviated, you will have to work overtime!