MCS-51 microcontroller command system (3)

Publisher:RadiantBeautyLatest update time:2022-12-12 Source: zhihuKeywords:MCS-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Instructions are divided into 7 addressing modes according to the addressing mode, direct addressing (direct), immediate addressing (#20H), register addressing (Rn), register indirect addressing (@Ri), relative addressing (rel) , register index addressing (@A+PC), bit addressing (ACC.1)


There are five types of instructions according to their functions: transfer instructions, arithmetic operation instructions, logical operation and shift instructions, control transfer instructions, and bit operation instructions. Its instructions have three major attributes: functional attributes, space attributes (number of bytes used by the instruction station), and time attributes (number of machine cycles for instruction execution).


1. Data transfer instructions (28):

The most frequently used type of instructions, general format:

MOV ,

It belongs to the nature of "copying" rather than "moving". Data transfer instructions do not affect the flag bits: Cy, Ac and OV, but affect the parity flag bit P.


1. Internal RAM data transfer instructions (16)

(1) Instructions with the accumulator as the destination operand (4)

MOV A,Rn; (Rn)→A, n=0~7

MOV A,@Ri ; ((Ri))→A,i=0,1

MOV A,direct ;(direct)→A

MOV A,#data ; #data→A

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

MOV Rn,A ; (A)→Rn,n=0~7

MOV Rn,direct ;(direct)→Rn,n=0~7

MOV Rn,#dat ; #data→Rn,n=0~7

Function: Send the content of the source operand to a register among R0~R7 in the current set of working register areas.

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

MOV direct,A ; (A)→direct

MOV direct,Rn;(Rn)→direct, n=0~7

MOV direct1,direct2;

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

MOV direct,#data; #data→direct

Function: Send the source operand to the storage unit pointed by the direct address. direct refers to the address of the internal RAM or SFR.

(4) Instructions with register indirect address as destination operand (3 items)

MOV @Ri,A ;(A)→((Ri)),i=0,1

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

MOV @Ri,#data ; #data→((Ri))

(5) 16-digit transmission command (1)

MOV DPTR,#data16 ; #data16→DPTR

The only 16-bit data transfer instruction, the upper 8 bits of the immediate number are sent to DPH, and the lower 8 bits of the immediate number are sent to DPL.


2. Stack operation instructions (2)

A LIFO-Last In First Out (LIFO-Last In First Out) area can be set in the MCS-51 internal RAM called a stack. The stack pointer SP points to the top position of the stack. push instruction

PUSH direct

First add 1 to the stack pointer SP, and then send the contents of direct to the internal RAM unit indicated by the stack pointer SP.

Pop instruction

POP direct

The contents of the top of the stack (internal RAM unit) indicated by SP are sent to the direct byte unit, and the stack pointer SP is decremented by 1.


3. Accumulator A and external data memory transfer instructions (4)

MOVX A,@DPTR ;((DPTR))→A, read external RAM/IO

MOVX A,@Ri ;((Ri))→A, read external RAM/IO

MOVX @DPTR,A;(A)→((DPTR)), write external RAM/IO

MOVX @Ri,A ;(A)→((Ri)), write external RAM/IO

Function: Read a byte in external RAM memory or I/O, or write a byte of data in A to external RAM memory or I/O.

Note: RD* or WR* signal is valid.

Using DPTR indirect addressing, the upper 8-bit address (DPH) is output from the P2 port, and the lower 8-bit address (DPL) is output from the P0 port.

Using Ri (i=0,1) indirect addressing, 256 units of off-chip RAM can be addressed. Ri content is output from P0 port.

"X" after MOV indicates that the microcontroller is accessing off-chip RAM memory or I/O.


4. Program ROM data transfer instructions and table lookup instructions (2 items)

Instructions for reading data tables in program memory use base register plus index register indirect addressing.

(1)MOVC A,@A+PC

Using PC as the base address register, the content of A is used as an unsigned integer and is added to the content in PC (the starting address of the next instruction) to obtain a 16-bit address. The content of the program storage unit pointed to by the address is sent to the accumulation Device A.

Note: PSEN* signal is valid.

Advantages: Without changing the status of the special function register and PC, the constants in the table can be taken out according to the content of A.

Disadvantages: The table can only be stored within the 256 units after the table lookup instruction. The size of the table is limited, and the table can only be used by a program.

(2)MOVC A,@A+DPTR

Using DPTR as the base address register, the content of A is added as an unsigned number to the content of DPTR to obtain a 16-bit address. The content of the program memory unit pointed by the address is sent to the accumulator A. The execution result of this instruction is only It is related to the contents of pointer DPTR and accumulator A, and has nothing to do with the address where the instruction is stored and the address where the constant table is stored. Therefore, the size and location of the table can be arranged arbitrarily in the 64K program memory, and one table can be shared by each program block.

The two instructions are to add C after MOV. "C" is the first letter of CODE, which means code.


5. Data exchange instructions (4 items)

(1) Integer byte data exchange instructions (3 items)

XCH A,Rn

XCH A,direct

XCH A,@Ri

(2) Nibble exchange instruction: exchange the lower 4 bits of the accumulator with the lower 4 bits of the internal RAM.

XCHD A,@Ri


2. Arithmetic operation instructions (24 items)

Single-byte addition, subtraction, multiplication, and division instructions are all for 8-bit binary unsigned numbers. The execution result affects the three flags Cy, Ac, and OV. However, the increment and decrement instructions do not affect the above flags.

1. Addition instructions (4 items)

ADD A,Rn;(A)+(Rn)→A, n=0~7

ADD A,direct ;(A)+(direct)→A

ADD A,@Ri ;(A)+((Ri))→A,i=0,1

ADD A,#data ; (A)+#data→A

One addend always comes from accumulator A, while the other addend can be obtained by different addressing modes. The result is always placed in A. When using addition instructions, pay attention to the impact of the operation results in accumulator A on each flag bit:

(1) If there is a carry in bit 7, set "1" carry flag Cy, otherwise clear "0" Cy

(2) If there is a carry in bit 3, set "1" auxiliary carry flag Ac, otherwise clear "0" Ac (Ac is a bit in the PSW register)

(3) If there is a carry in bit 6 but not in bit 7, or there is a carry in bit 7 but not in bit 6, the overflow flag bit OV is set to "1", otherwise it is cleared to "0" OV. The state of the overflow flag OV is only meaningful when adding signed numbers. When two signed numbers are added, OV=1, indicating that the addition operation exceeds the valid range of signed numbers that accumulator A can represent.


2. Addition instructions with carry (4 items)

The flag Cy participates in the operation, so the three numbers are added.

ADDC A,Rn;(A)+(Rn)+C→A, n=0~7

ADDC A,direct ;(A)+(direct)+C→A

ADDC A,@Ri ;(A)+(Ri)+C→A,i=0,1

ADDC A,#data ;(A)+#data+C→A


3. Add 1 command (5 items)

INC A

INC Rn ;n=0~7

INC direct

INC @Ri ;i=0,1

INC DPTR

Does not affect any flags in PSW. The fifth instruction, INC DPTR, is a 16-bit increment instruction. The instruction first performs an operation of adding 1 to the content of the lower 8-bit pointer DPL. When an overflow occurs, the operation of adding 1 to the content of DPH does not affect the status of the flag Cy.


4. Decimal adjustment instructions (1 item)

Used to modify the content of the BCD code decimal number addition operation result.

Command format: DA A

After two BCD codes are added in binary, this command must be adjusted to obtain the correct sum of the compressed BCD codes. The principle of addition of binary numbers does not apply to the addition of decimal numbers, and sometimes incorrect results will be produced.


Cause of error and adjustment method:

The BCD code only uses 10 of them, leaving 6 unused codes. (1010, 1011, 1100, 1101, 1110, 1111) are invalid codes. Whenever the result enters or skips the invalid code encoding area, the result is wrong. The adjustment method is to add 6 to the result, which is the so-called decimal adjustment.

The correction method should be:

(a) The lower 4 bits of the accumulator are greater than 9 or the auxiliary carry bit Ac=1, then the lower 4 bits are corrected by adding 6.

(b) If the high 4 bits of the accumulator are greater than 9 or the carry bit Cy=1, then the high 4 bits are corrected by adding 6.

(c) The high 4 bits of the accumulator are 9, and the low 4 bits are greater than 9, then the high 4 bits and low 4 bits are corrected by adding 6 respectively.

Specifically, it is automatically implemented by executing the command: DA A.


5. Subtraction instructions with borrow (4 items)

SUBB A,Rn; (A)-(Rn)- Cy→A, n=0~7

SUBB A,direct; (A)-(direct)- Cy→A

SUBB A,@Ri;(A)-((Ri))- Cy→A, i=0,1

SUBB A,#data ;(A)-#data - Cy→A

Subtract the value of the specified variable and carry flag Cy from the contents of accumulator A, and the result is stored in accumulator A.

If bit 7 needs to be borrowed, set "1" Cy, otherwise clear "0" Cy;

If bit 3 needs to be borrowed, set "1" Ac, otherwise clear "0" Ac;

If bit 6 needs to be borrowed but bit 7 does not need to be borrowed, or bit 7 needs to be borrowed and bit 6 does not need to be borrowed, set the overflow flag OV to "1", otherwise clear OV to "0".


6. Subtract 1 command (4 items)

DEC A; (A)-1→A

DEC Rn; (Rn)-1→Rn, n=0~7

DEC direct; (direct)-1→direct

DEC @Ri; ((Ri))-1→(Ri), i=0,1

The decrement instruction does not affect the flag bit.


7. Multiplication instructions (1)

MUL AB;A×B→BA

If the product is greater than 255, set the overflow flag OV to "1"


8. Division instruction (1 item)

DIV AB; A/B→A (quotient), remainder→B

If the content of B is "0" (that is, the divisor is "0"), the contents of A and B storing the result are uncertain, and the "1" overflow flag OV is set.


3. Logical operation instructions (25)

1. Simple logical operation instructions (2 items)

(1) CLR A

The function is to clear accumulator A to "0". Does not affect Cy, Ac, OV and other signs.

[1] [2]
Keywords:MCS-51 Reference address:MCS-51 microcontroller command system (3)

Previous article:MCS-51 microcontroller command system (4)
Next article:MCS-51 microcontroller command system (2)

Recommended ReadingLatest update time:2024-11-16 12:24

Notes on assembly operators based on the MCS-51 kernel
Data transfer instructions: MOV accesses internal data storage MOVX accesses external data storage MOVC accesses ROM storage XCH Whole byte exchange XCHD Nibble swap (lower four bits) PUSH Set the contents pointed to by the stack pointer register SP (push the word onto the stack) POP pops the word out of the stack; SW
[Microcontroller]
Design of data collection and information analysis system based on LabVIEW and MCS-51 microcontroller
1. LabVIEW part design 1.1VISA Introduction LabVIEW provides a powerful VISA library. VISA (Virtual Instrument Software Architecture) - virtual instrument software specification, is the general name for the standard I/O function library and related specifications used for instrument programming. The VISA library resid
[Test Measurement]
Design of data collection and information analysis system based on LabVIEW and MCS-51 microcontroller
Interrupt sources and interrupt vectors of mcs-51 microcontroller
1. External interrupt 0, 1; caused by the level signals of pins /INT0, /INT1 respectively.  2. Timer/ Counter 0, 1; caused by the overflow of T0, T1 respectively.  3. Serial port sending, receiving; caused by sending a byte or receiving a byte.  There are 5 interrupt sources in total.  4. Timer/Counter 2; caused b
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号