Usage analysis of various assembly language instructions for PIC microcontrollers

Publisher:ArtisticSoulLatest update time:2019-11-30 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have been using ST microcontrollers since I started working. Although I have a general understanding of other microcontrollers, I have never looked at them seriously. I happened to have nothing to do in recent days, so I decided to familiarize myself with PIC microcontrollers. So I wanted to transfer what I saw on the Internet or in this book and give it to beginners for reference.


PIC instruction system

There are three levels of PIC 8-bit microcontrollers, each with a corresponding instruction set. The basic PIC series chip has 33 instructions, each with a 12-bit word length; the intermediate PIC series chip has 35 instructions, each with a 14-bit word length; the advanced PIC series chip has 58 instructions, each with a 16-bit word length. Its instructions are backward compatible.

Usage analysis of various assembly language instructions for PIC microcontrollers

1. PIC assembly language instruction format

The assembly language instructions of the PIC series microcontrollers are the same as the assembly language of the MCS-51 series microcontrollers. Each assembly language instruction consists of four parts, and its writing format is as follows:

Label opcode mnemonic operand1, operand2; comment

The instruction format is described as follows: The four parts of the instruction are separated by spaces, which can be one or more spaces, to ensure that the PC can recognize the instructions during cross-assembly.


1. Labels have the same function as MCS-51 series microcontrollers. Labels represent the symbolic address of instructions. When the program is assembled, the specific value of the instruction memory address has been assigned. The symbolic address (i.e. label) used in assembly language is convenient for viewing and modification, especially for the representation of instruction transfer addresses. Labels are optional in the instruction format and are only required when referenced by other statements. In the absence of a label, one or more spaces must be reserved before the instruction mnemonic before writing the instruction mnemonic. The instruction mnemonic cannot occupy the position of a label, otherwise the mnemonic will be mishandled as a label by the assembler.


When writing a label, the first character must be a letter or a half-width underscore "—", which can be followed by English and numeric characters, colons (:), and symbols, and can be combined in any way. In addition, labels cannot be represented by opcode mnemonics and register codes. Labels can also occupy a single line.


2. Operation code mnemonic This field is a mandatory item for instructions. This item can be an instruction mnemonic, or it can be composed of pseudo instructions and macro commands. Its function is to compare the "instruction operation code mnemonic" with the "operation code table" one by one during cross assembly, and find out the corresponding machine code one by one.


3. The operand consists of the data value of the operand or the data or address value represented by the symbol. If there are two operands, the two operands are separated by a comma (,). When the operand is a constant, the constant can be binary, octal, decimal or hexadecimal. It can also be a defined label, string and ASCII code. When it is specifically expressed, it is stipulated that the letter "B" is prefixed before the binary number, such as B10011100; the letter "O" is prefixed before the octal number, such as O257; the letter "D" is prefixed before the decimal number, such as D122; and the letter "H" is prefixed before the hexadecimal number, such as H2F. Here, the default system of the PIC 8-bit microcontroller is hexadecimal. Add Ox before the hexadecimal number, such as H2F can be written as Ox2F.


The operands of the instruction are also optional.

The PIC series, like the MCS-51 series 8-bit microcontrollers, has an addressing method, that is, the source or destination of the operand. Because the PIC series microcontrollers use a reduced instruction set (RISC) architecture, their addressing methods and instructions are few and simple. Its addressing methods can be divided into four types according to the source of the operand: immediate addressing, direct addressing, register indirect addressing, and bit addressing. Therefore, the operands in the instructions of the PIC series microcontrollers often have related register symbols. Relevant addressing examples can be found at the end of this article.


4. Comments are used to explain the program and make it easier for people to read the program. Use a semicolon (;) to separate the comment from other parts. When the assembler detects a semicolon, the characters after it are no longer processed. It is worth noting that when using a subroutine, the entry and exit conditions of the program and the functions and effects that the program should complete should be stated.


2. Clear instruction (4 in total)

1. Register clear instruction

Example: CLRW; register W is cleared

Description: This instruction is very simple, where W is the working register of the PIC microcontroller, equivalent to the accumulator A in the MCS-51 series microcontrollers, and CLR is the abbreviation of the English word Clear.

2. Watchdog timer clear instruction.

Example: CLRWDT; Clear the watchdog timer (if assigned, clear the prescaler at the same time)

Note: WDT is the abbreviation of Watchdog Timer. CLR see the above description. Note that these two instructions have no operands.

3. Register f clear instruction. Instruction format: CLRF f

Example: CLRF TMRO; clear TMRO

Note: In the PIC series 8-bit microcontrollers, the symbol F (or f) is often used to represent various registers and the serial address of F. The value of F varies according to different models of the PIC series, generally Ox00~Ox1F/7F/FF. TMRO stands for timer/counter TMRO, so CLRF clears the register and uses direct addressing to directly give the register TMRO to be accessed.

4. Bit clear instruction. Instruction format BCF f, b

Example: BCF REG1, 2; clear the D2 bit of register REG1

Note: BCF is the abbreviation of Bit Clear F. The F in the instruction format is the same as above; the symbol b represents the bit number (or bit address) of a certain 8-bit data register F in the PIC chip, so the value of b is 0 to 7 or D0 to D7. REG in the example is the abbreviation of Register. The 2 in the example represents b=2 in the instruction format, that is, D2 of register REG1.

Through the above four clear instruction formats and examples, it can be explained that when learning the instructions of the PIC series 8-bit microcontroller, you should first understand the mnemonic meaning (function) of the instruction, and then its expression method. Beginners do not need to memorize the instructions, the important thing is to understand and practice.


3. Instructions for Bytes, Constants, and Control Operations

1. Transfer immediate data to working register W instruction

Instruction format: MOVLW k; k represents a constant, an immediate value, and a label

Note: MOVLW is the abbreviation of Move Literal to w

Example: MOVL 0x1E; constant 30 sent to W

2. I/O port control register TRIS setting instruction

Instruction format; TRIS f

Description: TRIS f is the abbreviation of Load TRIS Register. Its function is to send the content of working register W to I/O port control register f. When W=0, the corresponding I/O port is set as output; W=1, the I/O port is set as input.

Example: MOVLW 0x00; send 00H to W

TRIS RA; Set PIC RA port as output

MOVLW 0xFF ; put FFH into W

TRIS RB; Set PIC RB port as input

Description: These are some commonly used instructions in PIC assembly language, that is, the statements for setting a certain I/O port (here, RA and RB) as input or output. It can be seen that when reading instructions, one should fully understand the function of the statement format and read it in conjunction with the previous and next instructions.

3. Send the contents of register W to register f (the contents of register W remain unchanged) instruction

Instruction format: MOVWF f

Note: MOVWF is the abbreviation of Move W to f

Example: MOVLW 0x0B; send 0BH and then send W

MOVWF 6; send W content to RB port

Explanation: The first instruction 0x0B (constant 11) is sent to the working register W. The second instruction sends the constant 11 in W to register F6. Looking up the table, F6 is port RB, so PORT_B (port B) = 0BH = D11

4. Register f transfer instruction

Instruction format: MOVF f, d

Note: MOVF is the abbreviation of Move f. F represents a register in PIC. The d in the instruction specifies: when d=0, the content of f is sent to W; when d=1, the content of f is sent to the register.

Example: MOVF 6,0; send the contents of port RB to W

MOVWF 8; RB port content is sent to f8

Explanation: The 6 in the first instruction represents register f=6. Looking up the register table, f=6 is port RB; 0 represents d=0, indicating that the selected target is register W. The 8 in the second instruction represents register f=8. So the result of the two instructions is to send the content of port RB to f8. As for the content of f8, additional instructions should be added at the beginning of the assembly language, which is omitted here.

5. No operation instructions

Instruction format: NOP

Note: NOP is the abbreviation of No Operation. NOP has no operand, so it is called no operation. Executing the NOP instruction only increases the program counter PC by 1, so it takes one machine cycle.

Example: MOVLW 0xOF; send OFH to W

MOVWF PORT_B ;W content is written to port B

NOP; No operation

MOVF PORT_B, W; read operation

Note: These three instructions are an example of continuous operation on port B of the I/O port. The purpose is to ensure that there is a stable time between writing and reading when the content written to port B is to be read out. Therefore, the no operation instruction NOP is added.

6. Unconditional jump instruction

Instruction format: GOTO k

Description: When this instruction is executed, it will be transferred to the specified address (jump). The k in the instruction is often associated with the label in the program.

Example: See instruction No. 9

[1] [2]
Reference address:Usage analysis of various assembly language instructions for PIC microcontrollers

Previous article:Introduction to the basic knowledge of PIC microcontroller programming
Next article:Analysis of the working principle of PIC microcontroller 4×4 column keyboard

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号