3: 51 assembly instruction system

Publisher:zdf1966Latest update time:2019-08-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

MnemonicsFunctionuse
MOVAccess internal RAM, access special function registersMOV A, Rn; (Rn) → A, which means to transfer the content in Rn to A
MOVXAccessing External RAMMOVX A, @DPTR; ((DPTR)) → A
MOVX @DPTR, A; (A) → (DPTR)
MOVX A, @Rn; ((Rn)) → A
MOVX @Rn, A; (A) → (Rn)
MOVCAccessing program memoryMOVC A, @A+DPTR; ((A)+(DPTR))→A 
MOVC A, @A+PC; ((A)+(PC))→A
XCByte SwapXCH A, Rn; (A) → Rn, (Rn) → A 
XCH A, direct; (A) → direct, (direct) → A 
XCH A, @Rn; (A) → (Rn), (Rn) → A
XCNibble SwapXCHD 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
PUSHPush OperationPUSH direct; (SP)+1 → SP, (direct) → (SP)
POPPop operationPOP 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)

ADDA,Rn;A←(A)+(Rn) The data in the accumulator is added to the data in the register and sent to the accumulator
ADDA, direct;A←(A)+(direct) The accumulator is added to the data in the direct addressing unit and sent to the accumulator
ADDA, @Ri;A←(A)+((Ri)) The data in the accumulator and the indirect addressing unit are added and sent to the accumulator
ADDA, #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

ADDCA,Rn;A←(A)+(Rn)+(Cy)
ADDCA, direct;A←(A)+(direct)+(Cy)
ADDCA, @Ri;A←(A)+((Ri))+(Cy)
ADDCA, #data;A←(A)+data+(Cy)

3. Add 1 instruction

INCA;A←(A)+1 Data in accumulator (A)+1
INCR;Rn←(Rn)+1 Register data +1
INCdirect;direct←(direct)+1 Data in direct addressing unit +1
INC@Ri;Ri←((Ri))+1 Data in indirect addressing unit +1
INCDPTR;DPTR←(DPTR)+1 data pointer+1

4. Subtraction instruction with borrow

SUBBA,Rn;A←(A)-(Rn)-(Cy)
SUBBA, direct;A←(A)-(direct)-(Cy)
SUBBA, @Ri;A←(A)-((Ri))-(Cy)
SUBBA, #data;A←(A)-data-(Cy)

5. Subtract 1 instruction

DECA;A←(A)-1 Data in accumulator (A) -1
DECR;Rn←(Rn)-1 Register data -1
DECdirect;direct←(direct)-1 Data in direct addressing unit -1
DEC@Ri;Ri←((Ri))-1 Data in indirect addressing unit -1

6. Multiplication Instructions

MULAB;(A)×(B)→ BA

7. Division Instructions

DIVAB;(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

CLRA; Clear accumulator A to "0"
CPLA; Invert the accumulator A bit by bit

2. Logic and instructions

ANLA,Rn;A←(A)∧(Rn) The accumulator and register data are ANDed and sent to the accumulator
ANLA, direct;A←(A)∧(direct) The accumulator and the data of the direct addressing unit are ANDed and sent to the accumulator
ANLA, @Ri;A←(A)∧(Ri) The accumulator and the indirect addressing unit data are ANDed and sent to the accumulator
ANLA, #data;A←(A)∧data The accumulator and the immediate value are ANDed and sent to the accumulator
ANLdirect, A;direct←(direct)∧(A) The accumulator and the data of the direct addressing unit are ANDed and sent to the direct addressing unit
ANLdirect,#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

ORLA,Rn;A←(A)∨(Rn) The accumulator and register data are ORed and sent to the accumulator
ORLA, direct;A←(A)∨(direct) The accumulator and the direct addressing unit data are ORed into the accumulator
ORLA, @Ri;A←(A)∨(Ri) The accumulator and the indirect addressing unit data are ORed and sent to the accumulator
ORLA, #data;A←(A)∨data The accumulator and the immediate data are ORed into the accumulator
ORLdirect, A;direct←(direct)∨(A) The accumulator is ORed with the data of the direct addressing unit and sent to the direct addressing unit
ORLdirect,#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

XRLA,Rn;A←(A)⊕(Rn) The accumulator and register data are different or sent to the accumulator
XRLA, direct;A←(A)⊕(direct) The accumulator and the directly addressed unit data are different or sent to the accumulator
XRLA, @Ri;A←(A)⊕(Ri) The data in the accumulator and the indirect addressing unit are different or sent to the accumulator
XRLA, #data;A←(A)⊕data The accumulator is different from the immediate value or is sent to the accumulator
XRLdirect, A;direct←(direct)⊕(A) The accumulator and the directly addressed unit have different data or are sent to the directly addressed unit
XRLdirect,#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

RLA; The content of accumulator A is shifted left by 1 bit
RLCA; The content of accumulator A is circularly shifted left by 1 bit with the carry flag
RRA; The content of accumulator A is rotated right by 1 bit
RRCA; 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

MOVC, bit;Cy←(bit) Send the content of the direct address bit to the accumulator CY
MOVbti, 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

CLRbit;bit←0 clear the bit address
CPLC;Cy←(Cy inverted) invert the bit accumulator CY
CPLbit;bit←(bit inversion) invert the bit address
SETBC;Cy←1 Set the bit accumulator CY to 1
SETBbit;bit←1 Set the address bit to 1

**3. Bit variable logic instructions**

ANLC, bit;Cy←(Cy)∧(bit) The bit accumulator CY is ANDed with the bit address and sent to bit CY
ORLC, 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.

instructionillustrate
JMP relBy default, this is equivalent to SJMP rel
SJMP relRel is an 8-bit signed number, and the transfer range is -128~+127 of the current PC value, a total of 256 units
AJMP addr11addr11 is sent to PC10~PC0, while PC15~PC11 remain unchanged, the transfer range is 2KB
LJMP addr16addr16 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.

Classificationinstructionillustrate
Accumulator A is judged as 0 and transferredJZ rel(A) = 0 transfer to PC+rel for execution, otherwise execute sequentially
Accumulator A is judged as 0 and transferredJNZ rel(A) ≠ 0, transfer to PC+rel for execution, otherwise execute sequentially
Bit state transitionJB bit,relBit = 1 transfer to PC+rel for execution, otherwise sequential execution
Bit state transitionJNB bit,relbit ≠ 1 transfer to PC+rel for execution, otherwise execute sequentially
Bit state transitionJBC bit,relIf bit = 1, transfer to PC+rel for execution, and set this bit to 0, otherwise, execute sequentially.
Bit state transitionJC relCarry bit CY = 1 transfer to PC + rel for execution, otherwise sequential execution
Bit state transitionJNC relCarry bit CY ≠ 1 transfer to PC+rel for execution, otherwise sequential execution

3. Compare transfer instructions

CJNA, direct, rel;(A) ≠ (direct) transfer
CJNA,#data,rel;(A) ≠ data transfer
CJNRn, #data, rel;(Rn) ≠ data transfer
CJN@Ri, #data,rel;((Ri)) ≠ data transfer

4. Loop transfer instructions

DJNZRn,rel; Register Rn is decremented by 1 and is not 0. Loop transfer
DJNZdirect, 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

[1] [2]
Reference address:3: 51 assembly instruction system

Previous article:2: The first 51 single-chip microcomputer assembly experiment
Next article:IV: Output application of IO port

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号