Assembly language for PIC 8-bit microcontrollers[Copy link]
In order for a microcontroller to complete a basic task, the task must be broken down into some specific steps, and then it must be required to execute each step one by one, and it must also be commanded. This command is called an "instruction" in microcontroller terminology. The ordered set of all instructions required to complete a task is called a "program". These instructions must be placed in the program memory of the microcontroller in advance, one by one, and when the microcontroller is running, the CPU in the chip takes out the instructions from the program memory one by one in order, executes the instructions, and completes the execution of the relevant instructions to complete the given task. Different types of microcontrollers have different sets of commands (the so-called "instruction systems"). The instruction system of the PIC microcontroller is completely different from that of the 51 series. The PIC16F84 microcontroller has an instruction system consisting of more than 30 instructions. Each instruction consists of 14 bits, which are 0 and 1 in binary code. If you want to make the B0 bit of port B of 16F84 microcontroller output high level to light up a light-emitting diode LED, while the rest of the bits of port B remain low, you need to make the microcontroller execute the following instructions (machine code): 11000000000000 00000001100110 11000000000001 00000010000110 10100000000100Earlier technicians used such binary codes to write programs. The above program looks like a mystery and is difficult to understand, but it can fully command the operation of the microcontroller. Because the microcontroller is actually a complex digital logic circuit. We all know that for a digital circuit to work, high and low levels must be input accordingly. For positive logic, high level is 1 and low level is 0. The 0 and 1 that appear in different digits of the above instruction sequence can complete various operations after decoding, and gradually complete the tasks to be performed by the single-chip microcomputer, such as lighting an LED. Although the writing methods of the above instructions are completely oriented to the single-chip microcomputer, they are used to directly instruct the single-chip microcomputer how to operate. Therefore, this instruction composed of 0 and 1 is called machine language. In fact, this instruction set composed of binary code is not only difficult to read, but also difficult to use for programming. Because the program is often not executed sequentially from beginning to end, sometimes it is necessary to transfer to other units in the middle to execute a section of the program and then return. The instructions are stored in each unit of the memory one by one. Therefore, if you want to transfer, you need to specify which unit to transfer to, that is, you need to write the address of the unit. But when writing a program, how long the program is and which units it should be placed in are all unknown, so how can you specify which unit to transfer to? Because using machine language makes the program difficult to write and read, a new language form, assembly language, came into being later. It is more convenient to write programs in this language and it is easier to read. However, just like machine language, different types of microcontrollers have completely different assembly languages. Just like people in different regions have different dialects. In assembly language, transfer addresses are represented by symbols. Now, let's rewrite the program written in machine language into a program composed of assembly language: movlw B'00000000' tris PORT B movlw B'00000001' movwf PORT B fin: goto fin The instructions listed above are actually composed of English abbreviations and some numbers. For example, the movlw in the first instruction is the abbreviation of move Literal to w, which means to move to the working register W as is, and "as is" is the number '00000000' in the quotation marks. The B before the quotation marks indicates that the following is a binary number. The second sentence is to copy the number in W to the tri-state control register of port B to set port B as output, then send 00000001 into W and copy it to port B; finally, execute an infinite loop statement to keep the state of port B unchanged. From the outside of the microcontroller, the 6th pin (i.e. B0) of the 16F84 microcontroller is maintained at a high level to light up the LED. From the above example, it can be seen that assembly language is much easier to understand than machine language. At the same time, the last sentence of the self-loop is also a transfer statement, and the transfer destination is the unit address where this sentence is located. If you use machine language, it is difficult to mark the specific address, but the assembly language mnemonic fin can be used instead.