"MCU tutorial 11: MCU arithmetic operation instructions
Single chip microcomputer addition instruction without carry
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 its subsequent value, and the final result will be returned to a.
Example: mov a, #30h
ADD A,#10H
After this instruction is executed, the value in a is 40H.
Practice the following questions by yourself
MOV 34H,#10H
MOV R0,#13H
MOV A,34H
ADD A,R0
MOV R1,#34H
ADD A,@R1
Add instruction with carry
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 carry C.
Note: since 51 single chip microcomputer is an 8-bit machine, it can only do 8-bit mathematical operations, but the range of 8-bit operations is only 0-255, which is not enough in practical work. Therefore, it is necessary to expand. Generally, two 8-bit mathematical operations are combined to form a 16 bit operation. In this way, the range of numbers that can be expressed can reach 0-65535. How to merge? Actually, it's very simple. Let's look at a routine of hexadecimal numbers:
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), and then do 6 + 7, which is high. We added twice, but we didn't deliberately divide it into two additions, or we didn't realize that we added twice. The reason to do it twice is that these two numbers exceed the norm (0-9) that can be expressed by a single digit.
Carry occurs when doing low order. When we do it, we click at the appropriate position, and then add this point when doing high-order addition. The same is true when adding 16 bits in the computer. First, do the lower 8 bits. If the addition of two numbers produces a carry, also "click" to make a mark. This mark is the carry C, which is in PSW. In high-order addition, this C is added. For example: for 1067h + 10a0h, 67h + a0h = 107h first, and 107h obviously exceeds 0ffh. Therefore, 7 is finally saved in a, and 1 reaches the CY bit in PSW. In other words, cy is equivalent to 100h. Then do 10h + 10h + cy, and the result is 21h, so the final result is 2107h.
Single chip microcomputer subtraction instruction with borrow
SUBB A,Rn
SUBB A,direct
SUBB A,@Ri
SUBB A,#data
Let (each h, (R2) = 55h, cy = 1, after executing the command sub A, R2, the value in a is 73h.
Note: there is no single chip microcomputer subtraction instruction without borrow. If it is necessary to do the subtraction instruction without borrow (when subtracting for the first time), just clear cy.
Multiplication instruction
MUL AB
The function of this MCU instruction is to multiply the two 8-bit unsigned numbers in a and B. the result of the multiplication is generally large, so the final result is expressed by a 16 bit number, in which the high 8 bits are placed in B and the low 8 bits are placed in a. When the product is greater than fffffh (65535), 0V is set to 1 (overflow), otherwise ov is 0 and CY is always 0.
For 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.
Division instruction
DIV AB
The function of this MCU instruction is to divide the 8-bit unsigned number in a by the 8-bit unsigned number in B (A / b). Generally, there will be decimals in division, but it can't express decimals directly in the computer. It uses the concepts of quotient and remainder used when we primary school students haven't been exposed to decimals. For example, 13 / 5, its quotient is 2 and the remainder is 3. In addition, the quotient is placed in a and the remainder in B. CY and ov are both 0. If the value in B before division is 00h, that is, the divisor is 0, then 0V = 1.
Add 1 instruction
INC A
INC Rn
INC direct
INC @Ri
INC DPTR
The purpose is very simple. It is to add 1 to the value in the following target. For 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 is #1 similar to add a, 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. For example, (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 addition and so on. In addition, addition instructions take a as the core �&# 0 One of the numbers must be placed in a, and the operation result must also be placed in a. the objects added with class 1 instructions are much wider, which can be registers, memory addresses, inter address addresses, etc.
Minus 1 instruction
Minus 1 instruction
DEC A
DEC RN
DEC direct
DEC @Ri
Similar to the plus 1 instruction, I won't say more.
Comprehensive exercise:
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 operation results of each step, then build the above topics into and run them in the software simulation, and observe whether the changes of the contents of registers and relevant units are the same as their expected results.
Single chip microcomputer logic operation instructions
Logic operation of accumulator a of single chip microcomputer:
CLR A ; Clear the value in a to 0, and the single cycle single byte instruction has the same effect as mov A and #00h.
CPL A ; Reverse the value in a bit by bit
RL A ; Move the value in a logically to the left
RLC A ; The value in a is logically shifted to the left by adding the carry bit
RR A ; Logically shift the value in a to the right
RRC A ; The value in a is logically shifted to the right by adding the carry bit
SWAP A ; Exchange the high and low 4 bits of the value in a.
For example: (a) = 73h, execute CPL a as follows:
73h is binary and 01110011,
Bit by bit inversion is 10001100, that is 8CH.
RL a sends the 7th bit of the value in (a) to bit 0, bit 0 to bit 1, and so on.
Example: if the value in a is 68H, execute RL a. 68H is converted into binary and 01101000. Move it according to the above figure. 01101000 is converted to 11010000, i.e. d0h.
RLC A is to shift the value in (a) with carry bit (c).
Example: if the value in a is 68H and the value in C is 1, RLC A is executed
After 1 01101000, the result is 0 11010001, that is, the value of C carry becomes 0 and (a) becomes d1h.
RR A and RRC a will not talk more. Please refer to the above two routines to practice by yourself.
Swap a is to exchange the high and low 4 bits of the value in a.
For example: (a) = 39H, then after executing swap a, the value in a is 93h. How did it happen to be so back and forth? Because this is a hexadecimal number, each 16 digit number represents four binary digits. Note that if this is the case: (a) = 39, followed by no h, after executing swap a, it is not (a) = 93. To convert it into binary and calculate again: 39 into binary is 10111, that is, 00010111, the high four bits are 0001, the low four bits are 0111, and after exchange is 01110001, that is, 71h, that is, 113.
Practice, known (a) = 39H, write the results of each step after executing the following MCU instructions
CPL A
RL A
CLR C
RRC A
SETB C
RLC A
SWAP A
Through the previous study, we have mastered a considerable part of MCU instructions. We may be tired of these boring MCU instructions. Let's relax and do an experiment.
Experiment 5:
ORG 0000H
LJMP START
ORG 30H
START:
MOV SP,#5FH
MOV A,#80H
LOOP:
MOV P1,A
RL A
LCALL DELAY
LJMP LOOP
delay:
mov r7,#255
d1: mov r6,#255
d2: nop
nop
nop
nop
djnz r6,d2
djnz r7,d1
ret
END
First, let's write the program into the chip, install it into the experimental board and have a look at the phenomenon.
What we see is a phenomenon of dark spot flow. Let's analyze it.
The previous ORG 0000H, LJMP start and org 30h will be analyzed later. Starting from start, MOV SP, #5fh, this is the initialization stack. It doesn't matter whether there is this sentence in this program or not. However, we slowly start to contact formal programming, and I'll slowly cultivate habits for you.
Mov a, #80h, send the number 80h to a. what are you doing? I don't know. Look down.
MOV P1,A。 Send the value in a to P1 port. At this time, the value in a is 80h, so the value sent out is 80h, so the value of P1 port is 80h, that is, 10000000B. Through the previous analysis, we should know that P1 at this time. 7. The connected LED is not bright, while other LEDs are bright, so a "dark spot" is formed. Continue to see that RL a, RL A is to move the value in a to the left, calculate, what is the result after the move? By the way, it is 01h, that is 00000001b. In this way, it should be connected to P1. The LED on 0 is not on, but others are on. From the phenomenon, the "dark spot" flows to the back. Then call the delay program, which we are familiar with. Let the "dark spot" and "dark" for a while. Then it is transferred to loop (LJMP loop). Please calculate which of the following lights is off..... By the way, it should be connected to P1. 1. The upper light doesn't work. In this way, the phenomenon of "dark spot flow" is formed.
Question:
How to realize the flow of highlights?
How to change the direction of flow?
answer:
1. Change the initial value in a to 7FH.
2. Change RL a to RR a., Read the full text, technical section
About ARM7 S3C4510B μ Clinux migration issues
Design scheme of multi loop charge discharge controller
Interrupt service routine jump of ARM embedded system
Design of LED dimming engine based on 8-bit MCU
How to display Chinese characters, ASCII characters and color graphics on LCD“
Our other product: