4379 views|0 replies

1379

Posts

0

Resources
The OP
 

The logic and instructions of assembly language in single chip microcomputer [Copy link]

Logical AND instruction

ANL A,Rn; A and the value in Rn are bitwise ANDed, and the result is sent to A.

ANL A,direct; A and the value in direct are bitwise ANDed, and the result is sent to A.

ANL A,@Ri; A and the value in the indirect addressing unit @Ri are bitwise ANDed, and the result is sent to A.

ANL A,#data; A and the immediate data are bitwise ANDed, and the result is sent to A.

ANL direct,A; The value in direct and the value in A are bitwise ANDed, and the result is sent to direct.

ANL direct,#data; The value in direct and the immediate data are bitwise ANDed, and the result is sent to direct.

The key to these instructions is to know what logical AND is. The logical AND here refers to bitwise AND. For

example: 71H and 56H are ANDed together. The two numbers are written in binary form:

(71H) 01110001

(56H) 00100110.

The result is 00100000, which is 20H. From the above formula, we can see that if one of the two values involved in the operation is 0, the result of this bit is 0. If both are 1, the result is 1.

If you understand the operation rules of logical AND, the result will come out naturally. See the comments after each instruction

. Let's take some examples below.

MOV A, #45H; (A) = 45H

MOV R1, #25H; (R1) = 25H

MOV 25H, #79H; (25H) = 79H

ANL A, @R1; 45H and 79H are bitwise ANDed, and the result is 41H in A (A) = 41H

ANL 25H, #15H; the value in 25H (79H) and 15H are ANDed, and the result is (25H) = 11H)

ANL 25H, A; the value in 25H (11H) and the value in A (41H) are ANDed, and the result is (25H) = 11H

After knowing the function of the logical AND instruction, the functions of the logical OR and logical XOR are very simple. The logical OR is the bitwise "OR", that is, "1" is 1, and all "0" is 0. For example : 10011000 or 01100001 results in 11111001. XOR is a bitwise XOR, with the same value being "0" and the different value being "1". For example:

10011000 XOR 01100001 results in 11111001. For all OR instructions, ANL is replaced with ORL in the AND instruction, while XOR instruction replaces ANL with XRL. OR instructions: ORL A,Rn; the values in A and Rn are bitwise "OR"ed, and the result is sent to AORL A,direct; A and the value in the indirect addressing unit @Ri are bitwise "OR"ed, and the result is sent to AORL A,#data; A and the value in direct are bitwise "OR"ed, and the result is sent to AORL A,@Ri; A and the immediate data are bitwise "OR"ed, and the result is sent to AORL direct,A; the value in direct and the value in A are bitwise "OR"ed, and the result is sent to directORL direct,#data; the value in direct and the immediate data are bitwise "OR"ed, and the result is sent to direct. XOR instruction: XRL A,Rn; the values in A and Rn are bitwise "XORed", and the result is sent to AXRL A,direct; the values in A and direct are bitwise "XORed", and the result is sent to AXRL A,@Ri; A and the value in the indirect addressing unit @Ri are bitwise "XORed", and the result is sent to AXRL A,#data; A and immediate data are bitwise "XORed", and the result is sent to AXRL direct,A; the value in direct and the value in A are bitwise "XORed", and the result is sent to directXRL direct,#data; the value in direct and the immediate data are bitwise "XORed", and the result is sent to direct. Exercise: MOV A, #24H MOV R0, #37H ORL A, R0 XRL A, #29H MOV 35H, #10H ORL 35H, #29H MOV R0, #35H ANL A, @R0 IV. Control transfer instructions Unconditional transfer instructions Short transfer instructions AJMP addr11 Long transfer instructions LJMP addr16 Relative transfer instructions SJMP rel The above three instructions are quite different if you want to analyze them carefully, but when you are a beginner, you can ignore so much and understand them all as: JMP label, that is, jump to a label. In fact, we have already come into contact with the LJMP label in the previous routines, and we also know how to use it. The same is true for AJMP and SJMP. So what is the difference between them? It is that the jump range is different. For example, for a long jump, LJMP can jump 64K at a time (of course, it doesn't matter if it is close). AJMP can only jump a maximum distance of 2K, while SJMP can only jump a maximum distance of 256. In principle, all places where SJMP or AJMP is used can be replaced by LJMP. Therefore, when you are a beginner, you can use LJMP when you need to jump, except for one occasion. What occasion? First, let's understand AJMP. AJMP is a two-byte instruction, which means that this instruction itself occupies two units of the memory (ROM). LJMP is a three-byte instruction, which means that this instruction occupies three units of the memory (ROM). The following is the fourth jump instruction. The purpose of the indirect transfer instruction JMP @A+DPTR is also to jump. Where does it jump to? This cannot be simply determined by the label. Let's start with a practical example. MOV DPTR, #TAB; send the address represented by TAB to DPTR MOV A, R0; get the value from R0 (see the following description) MOV B, #2 MUL A, B; multiply the value in A by 2 (see the following description) JMP A, @A+DPTR; jump to TAB:AJMP S1 ; Jump table AJMP S2 AJMP S3









































































































Open in new window

Open in new window

Open in new window

  Application background introduction: In the development of single-chip microcomputers, keyboards are often used, as shown in the 9-key keyboard above. Our requirement is: when the function keys A………..G are pressed, different functions are performed. If this is expressed in the language of programming, it means: pressing different keys will execute different program segments to perform different functions. How to achieve this?

  See Figure 2. The previous program reads the key value. For example, the key value obtained after pressing the "A" key is 0, the value obtained after pressing the "B" key is "1", and so on. Then it jumps according to different values. For example, if the key value is 0, it will go to S1 for execution, and if it is 1, it will go to S2 for execution. . . . How to achieve this function?

  Let's start from the bottom of the program. There are several AJMP statements. These AJMP statements are finally stored in the memory like this (see Figure 3), that is, each AJMP statement occupies two memory spaces and is stored continuously. The address stored in AJMP S1 is TAB. We don't need to know what TAB is. We will leave it to the assembler to calculate.

  Let's look at the execution process of this program: After the first sentence MOV DPTR, #TAB is executed, the value in DPTR is TAB. The second sentence is MOV A, R0. We assume that R0 is the key value obtained by the key processing program. For example, if the A key is pressed, the value in R0 is 0, and if the B key is pressed, the value in R0 is 1, and so on. Now let's assume that the B key is pressed. After executing the second instruction, the value in A is 1. And according to our analysis, after pressing B, the S2 program should be executed. Let's see if this is the case? The third and fourth instructions multiply the value in A by 2, that is, after executing the fourth instruction, the value in A is 2. Next, JMP @A+DPTR is executed. Now the value in DPTR is TAB, and after A+DPTR is TAB+2. Therefore, after executing this sentence, it will jump to the address TAB+2 and continue to execute. Let's see what is placed in the address TAB+2? It is the instruction AJMP S2. Therefore, the AJMP S2 instruction is executed immediately, and the program will jump to S2 and continue to execute, which meets our requirements.

Please analyze the situation after pressing the keys "A", "C", "D", etc.

  In this way, we use JMP @A+DPTR to achieve the requirement of jumping to the corresponding program segment to execute after pressing a key. Let me ask you another question, why do we need to multiply by 2 after obtaining the key value? If all the instructions below the routine are replaced with LJMP, that is:

LJMP S1, LJMP S2, etc., can this program still be executed correctly? If not, how should it be changed?
This post is from MCU
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

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