1. The statement format of assembly language:
label opcode (instruction mnemonic) operand; comment
(label) (opcode) (operand) (comment)
2. Common pseudo-instructions
a.EQU——Symbol name assignment pseudo-instruction
format: symbol name EQU nn
b.ORG——Pseudo instruction format for defining the program starting address
: ORG nnnn
c.END——Program end pseudo instruction
format: END
d.LIST——List option pseudo-instruction
format: LIST [optional, optional, ...]
e.INCLUDE: Call in external program file pseudo-instruction
format: INCLUDE "file name"
2. Branching Program Structure
——A few explanations on the use of instructions in the program:
(1) For any logical operation (AND, OR, XOR) and arithmetic operation (addition, subtraction) that requires two numbers to participate, one of the operands must be placed in W in advance. Special attention should be paid to the subtraction instruction used here. The subtrahend should be placed in W in advance. In other words, the number placed in W in advance is used as the subtrahend in the operation, and the number in the register is used as the minuend.
(2) A conditional jump instruction often needs to be followed by an unconditional jump instruction to achieve long-distance transfer and program branching.
(3) The instruction system of the PIC microcontroller does not have a dedicated stop instruction, which can be achieved by using an unconditional jump instruction GOTO to jump to itself.
3. PIC microcontroller instructions
consist of three basic types of instructions:
a. Byte operation instructions
b. Bit operation instructions
c. Immediate and control operation instructions
For byte operation instructions, f——> file register identifier, d——> target register identifier
Description: The target identifier specifies the storage location of the operation result:
d=0 The operation result is stored in the W register
d=1 The operation result is stored in the specified file register. The default value of d is 1
4. Instruction Set
5. Examples
1 ;--------------------------------------------------------
2
3; Sequential program structure
4; Take out the lower 4 bits of the 20H unit and store them in 21H, and take out the upper 4 bits and store them in 22H
5; Key points: ANDLW and SWAPF
6
7;---------------------------------------------------------
8 MOVF 20H,0 ; Send the content of 20H unit to W
9 ANDLW 0FH ;W high 4 bits cleared, low 4 bits remain unchanged
10 MOVWF 21H ; send the lower 4 bits after splitting to 21H
11 SWAPF 20H,0 ; Swap the high and low nibble of the 20H unit content and send it to W
12
13 ANDLW 0FH ; Clear the high four bits of W to 0 and keep the low four bits unchanged
14 MOVWF 22H ; send the split high four bits to unit 22H
15
16
17;--------------------------------------------------------
18
19 ;Branch program structure
20; Two numbers are stored in the 20H and 21H units in the RAM. Find the larger number and store it in the 22H unit.
21 ; Key points: Subtract two numbers and determine the value of flag C
twenty two
twenty three ;---------------------------------------------------------
24 STATUS EQU 03H ; define the STATUS register address as 03H
25 C EQU 0 ; define the carry/borrow flag C in STATUS to get the address 0
26 MOVF 20H 0 ; Send the content of unit 20H to W
27 SUBWF 21H 0 ; Subtract the content in W from the content in 21H, and store the result in W
28 BTFSS STATUS, C; if C=1, no borrow, then the number in unit 21H is large, jump to F21BIG
29 GOTO F20BIG ; If C=0, there is a borrow, and the number in unit 20H is larger, then jump to F20BIG
30
31 F21BIG MOVF 21H,0 ; store the content of 21H into W register
32 MOVWF 22H; then transfer it to unit 22H
33 GOTO STOP ; skip the following two instructions to the end of the program
34
35 F20BIG MOVF 20H,0 ; store the content of 20H into W register
36 MOVWF 22H ; then transfer it to unit 22H
37
38 STOP GOTO STOP; Task completed, shutdown, stay where you are
39
40
41 ;--------------------------------------------------------
42
43 ;Loop program structure
44 ; In the data memory, all 50 cells starting from address 30H are written to 00H
45 ;Key points: The indirect addressing register FSR is used as an address pointer
46
47;---------------------------------------------------------
48 COUNT EQU 20H; Designate unit 20H as the loop count counter (i.e. loop variable)
49 FSR EQU 04H ; define FSR register address as 04H
50 INDF EQU 00H ; Set the INDF register address to 00H
51 MOVLW D50 ; send the initial value of the counter 50 to W
52 MOVWF COUNT ; Transfer 50 to the counter (as the operation value of the loop variable)
53 MOVLW 30H ; send 30H (starting address) to W
54 MOVWF FSR ; then transfer 30H to register FSR (used as address pointer)
55
56 NEXT CLRF INDF ; Clear the unit specified by the FSR content as the address to 0
57 INCF FSR,1 ;Address pointer content plus 1, pointing to the next unit
58 DECFSZ COUNT,1 ;Decrement the count value by 1. If the result is 0, skip to the next instruction to STOP
59 GOTO NEXT ; Jump back and execute the next loop
60 STOP GOTO STOP ; After the loop ends, execute this statement to stop the machine.
61
62;--------------------------------------------------------
63
64 ;Subroutine structure
65; The largest number of 3 is placed in the 40H unit
66
67;---------------------------------------------------------
68 STATUS EQU 03H
69 C EQU 00H
70 X EQU 20H
71 Y EQU 21H
72 Z EQU 22H
73 ;--------------------------------------------------------
74
75 ; Main program
76
77 ;---------------------------------------------------------
78
79 MAIN MOVF 30H,0
80 MOVWF X
81 MOVF 21H,0
82 MOVWF Y
83 CALL SUB
84 MOVF Z,0
85 MOVWF X
86 MOVF 32H,0
87 MOVWF Y
88 CALL SUB
89 MOVF Z,0
90 MOVWF 40H
91 STOP GOTO STOP
92 ;--------------------------------------------------------
93
94 ; Subroutine: (input parameters: X and Y, output parameter: Z)
95
96 ;---------------------------------------------------------
97 SUB MOVF X,0 ; send the contents of X to W
98 SUBWF Y,0 ; Subtract the content of W from the content of Y, and store the result in W
99 BTFSS STATUS, C; If C=1, no borrow occurs, execute the next line, otherwise jump
100 GOTO X_BIG
101
102 Y_BIG MOVF Y,0 ; Send the data in Y to W
103 MOVWF Z ; then save it to Z
104 GOTO THEEND ; skip the following two to the end
105
106 X_BIG MOVF X,0 ; send the data in X to W
107 MOVWF Z ; then save it to Z
108 THEEND RETURN ; Subroutine returns
Previous article:Compare/Capture/PWM (CCP) Module
Next article:【PIC Microcontroller】MPLAB X IDE Quick Start Guide
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- DSPF28335 source code about SPWM
- How to write printf macro switch
- Start with a Routine
- Share a reference book on servo motor drive
- Is FPGA a big deal in the field of artificial intelligence?
- 【GD32F310G-START】Voltage meter
- C language algorithm to calculate the age of beautiful women
- LOTO virtual oscilloscope about trigger sensitivity function
- [ST60 short-distance test] Part 2: Communication rate test
- PT4115 cannot be completely shut down