Ordinary MCU Teaching——Lecture 10 MCU Instructions (IV)

Publisher:转眼人老Latest update time:2015-01-23 Source: laogu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Ordinary MCU Teaching——Lecture 10 MCU Instructions (IV)

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

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号