1. Addition instruction without carry bit
ADD A,#DATA; Example: ADD A,#10H
ADD A,direct; Example: ADD A,10H
ADD A,Rn; Example: ADD A,R7
ADD A,@Ri ; Example: ADD A,@R0
Purpose: Add the value in A to the value after it, and the final result will be returned to A.
Example: MOV A, #30H
ADD A,#10H
After executing this instruction, the value in A is 40H.
Practice the following questions on your own
MOV 34H,#10H
MOV R0,#13H
MOV A,34H
ADD A,R0
MOV R1,#34H
ADD A,@R1
2. Addition instruction with carry bit
ADDC A,Rn
ADDC A,direct
ADDC A,@Ri
ADDC A,#data
Purpose: Add the value in A to the value after it, and add the value in the carry position C.
Note: Since the 51 microcontroller is an 8-bit machine, it can only perform 8-bit mathematical operations, but the range of 8-bit operations is only 0-255, which is not enough in actual work, so it is necessary to expand it. Generally, two 8-bit mathematical operations are combined into a 16-bit operation. In this way, the range of numbers that can be expressed can reach 0-65535. How to combine? In fact, it is very simple. Let's look at an example of a decimal number:
66+78.
We don't care about the process of adding these two numbers, but in fact we do it like this: first do 6+8 (low position), then do 6+7, which is the high position. We do two additions, but we don't deliberately divide it into two additions when we do it, or we don't realize that we do two additions. The reason why we divide it into two additions is that these two numbers exceed the range that can be expressed by a single digit (0-9).
When doing low-bit addition, a carry is generated. When we do it, we click on the appropriate position, and then add this point when doing high-bit addition. The same is true when doing 16-bit addition in the computer. Do the low 8 bits first. If the addition of two numbers generates a carry, we also need to "click" to make a mark. This mark is the carry bit C in PSW. When doing high-bit addition, this C is added. Example: 1067H+10A0H, first do 67H+A0H=107H, and 107H obviously exceeds 0FFH, so the final value stored in A is 7, and 1 goes to the CY bit in PSW. In other words, CY is equivalent to 100H. Then do 10H+10H+CY, the result is 21H, so the final result is 2107H.
3. Subtraction instruction with borrow
SUBB A, Rn
SUBB A,direct
SUBB A,@Ri
SUBB A,#data
Assume (each H, (R2) = 55H, CY = 1, after executing instruction SUBB A, R2, the value in A is 73H.
Note: There is no subtraction instruction without borrow. If you need to do a subtraction instruction without borrow (when doing the first subtraction), just clear CY.
4. Multiplication instruction
MUL AB
The function of this instruction is to multiply the two 8-bit unsigned numbers in A and B. The result of multiplying two numbers is generally large, so the final result is expressed by a 16-bit number, with the upper 8 bits placed in B and the lower 8 bits placed in A. When the product is greater than FFFFFH (65535), OV is set to 1 (overflow), otherwise OV is 0, and CY is always 0.
Example: (A) = 4EH, (B) = 5DH, execute the instruction
After MUL AB, the product is 1C56H, so 1CH is placed in B and 56H is placed in A.
5. Division instruction
DIV AB
The function of this instruction is to divide the 8-bit unsigned number in A by the 8-bit unsigned number in B (A/B). Division usually results in decimals, but decimals cannot be directly expressed in computers. It uses the concept of quotient and remainder that we used when we were elementary school students and had not yet come into contact with decimals. For example, 13/5, the quotient is 2 and the remainder is 3. After the division, the quotient is placed in A and the remainder is placed in B. CY and OV are both 0. If the value in B is 00H before the division, that is, the divisor is 0, then OV=1.
6. Increment Instruction
INC A
INC Rn
INC direct
INC@Ri
INC DPTR
The purpose is very simple, that is, to add 1 to the value in the target. Example: (A) = 12H, (R0) = 33H, (21H) = 32H, (34H) = 22H, DPTR = 1234H. Execute the following instructions:
INC A (A) = 13H
INC R2 (R0) = 34H
INC 21H (21H) = 33H
INC @R0 (34H) = 23H
INC DPTR (DPTR) = 1235H
The results are shown above.
Note: From the results, INC A and ADD A, #1 are similar, but INC A is a single-byte, single-cycle instruction, while ADD #1 is a double-byte, double-cycle instruction, and INC A will not affect the PSW bit, such as (A) = 0FFH, after INC A (A) = 00H, and CY remains unchanged. If it is ADD A, #1, then (A) = 00H, and CY must be 1. Therefore, the add 1 instruction is not suitable for addition. In fact, it is mainly used for counting, address increase, etc. In addition, addition instructions are all centered on AOne of the numbers must be placed in A, and the result of the operation must also be placed in A, while the objects of the add 1 instruction are much wider, which can be registers, memory addresses, indirect addressing addresses, etc.
7. Subtract 1 instruction
DEC A
DEC RN
DEC direct
DEC @Ri
It is similar to the plus 1 instruction, so I won’t say more.
Combined training:
MOV A,#12H
MOV R0,#24H
MOV 21H,#56H
ADD A,#12H
MOV DPTR,#4316H
ADD A,DPH
ADD A,R0
CLR C
SUBB A,DPL
SUBB A,#25H
INC A
SETB C
ADDC A,21H
INC R0
SUBB A,R0
MOV 24H,#16H
CLR C
ADD A,@R0
First write out the results of each step, then build the above questions and run them in software simulation, observing the changes in the contents of registers and related units to see if they are the same as your expected results.
Previous article:Ordinary MCU Teaching——Lecture 11 MCU Instructions (V)
Next article:Detailed explanation of the 0 character receiving routine using the serial port control MSComm in Delphi
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- How is the 0-10V/4-20mA input and output circuit of the inverter usually implemented?
- Two questions about PLL configuration registers
- 【Reading Month】Talk about good books and get gifts. Tell us which book you like best.
- Engineering Safety Black Box-Submission
- 【Chuanglong TL570x-EVM】Review 04 - CAN Test
- What is the difference between a voltage transformer and a current transformer? Aren't they both transformers? Don't they both have a primary side and a secondary side?
- Is there any expert who can tell me how to use the upgrade function of blue_nrg2?
- Hefei recruits 4 mid- and senior-level FPGA
- 5v relay
- MSP FRAM MCUs Reduce Volatility for the IoT