2992 views|0 replies

1379

Posts

0

Resources
The OP
 

MCU Instructions (IV) [Copy link]

1. Addition instruction without carry bit

ADD A,#DATA ;例:ADD A,#10H

ADD A,direct ;例:ADD A,10H

ADD A,Rn ;例:ADD A,R7

ADD A,@Ri ;DO:ADD A,@R0

Purpose: Add the value in A to the value after it, and the final result is returned to A.

Origin: 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 digit), then do 6+7, which is high digit. We do two additions, but we don't deliberately do it in two additions, or we don't realize that we do it twice. The reason why we do it in two additions is because 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 at 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 Instructions

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 Instructions

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 a decimal, 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 0V=1.

6. Add 1 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 instruction:

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 based on A - one 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 more extensive, which can be registers, memory addresses, addresses of indirect addressing, etc.

7. Subtract 1 instruction

DEC A

DEC RN

DEC direct

DEC @Ri

It is similar to the add 1 instruction, so I won’t say more.

Comprehensive exercises:

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.

This post is from MCU
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list