第3章 PIC单片机指令系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第3章 指令系统
3.1 3.2 3.3 3.4 汇编语言 寻址方式 PIC18F452指令系统 程序设计举例


3.1
汇编语言
• 指令:是 指令:是CPU CPU根据人的意图来执行某种操作的命令。

根据人的意图来执行某种操作的命令。

• 程序设计语言:是实现人机交换信息的基本工具,分为机 器语言、汇编语言和高级语言。

• 机器语言:用二进制编码表示每条指令 机器语言:用二进制编码表示每条指令,是计算机能 ,是计算机能直接 直接 识别和执行的语言。

识别和执行 的语言。

• 汇编语言:是用助记符、符号和数字 汇编语言:是用助记符、符号和数字等来表示指令的程序 等来表示指令的程序 设计语言。

它与机器语言指令是一一对应的。

设计语言。

它与机器语言指令是一一对应的。

? 汇编语言编写的程序能否直接被计算机执行? • 高级语言:面向问题和计算过程的语言,可通用于各种不 同的计算机。

用户编程时不必了解所用计算机的指令系统。




Why study assembly language?
• Many critical programs are written in assembly language • To better understand the functions and architecture of a computer system • To better understand the working of high level languages • To design efficient programs • A tool for writing system programs • Embedded systems development
– Mixed languages programming
3


• PIC18F452 PIC18F452汇编语言指令格式 汇编语言指令格式 标记段: 标记段 :操作码 [目的 目的操作数 操作数][, ][,源 源操作数 操作数][ ][; ;注释 注释] ]
MOVLW BTFSS BCF GOTO 0x7F WREG, 7 WREG, 0 Stop ;load test data ;clear bit 0
Stop:
• 其中操作码 其中操作码是必不可少的。

是必不可少的。

• 标记除了保留关键字,例如操作码或寄存器名称,标记可 以任意命名,标记中可以包含字母、数字、下划线和问号, 但必须以字母A 但必须以字母 A-Z或下划线开头,标记通常以冒号结束, 但冒号可有可无。

wait _again 4Me Me: : btfss sum,7 sum,7 ; wait is a label decf loop_cnt,F ; _again is a label Start&Stop Start&Stop: : Addlw Addlw: : not permited


操作码(Opcode 操作码( Opcode) )段必须包含正确的微控制器操作码。

操作码 段还可以包含伪指令 段还可以包含 伪指令( (Dircctive Dircctive) ),伪指令是专门用于汇编 器或连接器的特殊指令。

操作码段还可以包含宏名称。

宏 器或连接器的特殊指令。

操作码段还可以包含宏名称。

宏 (Macro)是用户预先定义的一组指令,用唯一的关键字或名 (Macro) 是用户预先定义的一组指令,用唯一的关键字或名 称标识。

操作码可以大写,也可以小写,但大多数操作码都 采用大写形式,这样可以避免混淆字母l 采用大写形式,这样可以避免混淆字母 l和数字 和数字1 1。

操作数 (Operand) (Operand)是被操作码操作的内容,位于操作码右边。

是被操作码操作的内容,位于操作码右边。

操作数可能没有, 操作数可能没有 ,也可能有多个,其间用逗号隔开。

注释(Comment)是分号后面的内容。

分号后面直到该行末尾 注释(Comment) 是分号后面的内容。

分号后面直到该行末尾 的内容都将被忽略而不产生任何有效代码。

每一行注释都必 须以分号开头,因为汇编语言不像其他语言一样可以进行连 续多行注释。

好的编程习惯应该是对代码块注释,而不是对 每一行代码单独注释。

在代码段前后各保留一个空行,也是 一种很好的编程习惯。

注意如何使用空行分隔代码段,以及 如何使用星号对语句进行注释。

注释很重要,因为汇编语言 晦涩难懂,注释可以帮助理解代码段的用途。




• 伪指令 伪指令( (Dircctive Dircctive) ) 伪指令是一类特殊的汇编器指令,它可能产生机器代码, 也可能不产生机器代码。

主要有以下几类: •目标文件伪指令 •控制伪指令 •列表伪指令 •数据伪指令


3.2 寻址方式
一、寻址方式:如何 式:如何找到存放操作数的地址,把操作数提取 出来的方法。

操作数的存放位置:片内、外R 外RAM/ROM, OM,SFR,工作寄存器等 二、寻址方式的种类: 1、寄存器立即寻址 2、寄存器直接寻址 3、寄存器间接寻址 4、寄存器位寻址


1.寄存器立即寻址 在这种寻址方式中, 式中,操作数或其中之一(另一个是 W) 是 立即 数, 数 , 随 指令码 携带 , 而无 需再 到其他 地方 去寻找。

指令中的操作数为8-位常数. 使用立即数寻址方式的指令有: movlw, movlw , addlw addlw, , retlw retlw, , etc. 16H H 如: ADDLW 16
1414 -位 立即寻址指令 OP CODE k k k k k k k k


2.寄存器直接寻址
• 8-位 直接从指令中获取 • 4-位从‘BSR ’寄存器中获取 如:ANDWF 0x10, 0, 1
BSR 寄存器
N N N N f f f f
16-位指令
OP CODE … f f f f f f f
4-位来自 BSR 寄存器
f f
f f
8-位来自指令字
f f f f f f f f
1212-位 有效寄存器地址


3.寄存器间接寻址
这是一种比较特别的寻址方式,是通过两个寄存器INDF和FSR 的组合而实现功能的。

操作数是FSR寄存器内容位指针,所指向寄 存器单元的内容。


• 1616-位 间接地址来自 FSR (选择寄存器).
8-位 FSRXH 寄存器
x x x
8-位 FSRXL 寄存器
f f f f f f f f
x
f
f
f
f
4-位来自 FSRXH 寄存器
f f f f
8-位来自 FSR
f f f f f f f f
12-位寄存器地址 有效的 12


MOVLW 3 MULLW0x64 LFSR 0,0x010 MOVFF PRODL, POSTINC0 MOVFF PRODH, INDF0
;place 3 into W ;multiply by 100 ;address location 0x010 with FSR0 ;save low product, increment FSR0 ;save high product at 0x011
0x010 0x011 0xFEA 0xFE9 0xFE2 0xFE1 0xFDA 0xFD9
2C 01 00 10
File Select Register 0 High (FSR0H) File Select Register 0 Low (FSR0L) File Select Register 1 High (FSR1H) File Select Register 1 Low (FSR1L) File Select Register 2 High (FSR2H) File Select Register 2 Low (FSR2L)


4.位寻址 这是PIC单片机的特色之一, 一,它可以对所有的RAM数 据存储器进行位寻址,即置位和清零等。

等。

WREG, ,0 如:BCF WREG ;clear bit 0;


3.3 PIC18F452指令系统
字节指令 位操作指令 控制指令 立即数指令 间接寻址指令 表指令 p218 p214 p214 p215 p216 p216


核心助记符
助记符 ADD SUB AND IOR XOR INC DEC 功能说明 相加 相减 相与 相或 相异或 加1 减1 助记符 MOV RL RR CLR COM RET BTF 功能说明 传送 左移 右移 清零 取反 返回 测试


指令系统补充字符说明
a RAM 存取位 a = 0: 存取 RAM 内的 RAM 地址 (忽略 BSR 寄存器) 目标寄存器选择位; d = 0: 结果保存至 WREG, d = 1: 结果保存至文件寄存器 f。

8 位寄存器文件地址 (0x00 到 0xFF)
d
f


指令通用格式


指令通用格式




立即数指令
• 操作数为常量或静态数据. • 立即数指令的操作数除了LFSR外只有一个操作数. • 大多数指令需要用到WREG.


Op-code ADDLW
Operand1 Literal
Operand2
Examples ADDLW 0x20 ADDLW .100 ANDLW 0x0F ANDLW .15 ANDLW 0b00001111 IORLW 0x80 IORLW 1
Comments Add 0x20 to W Add 100 to W AND 0x0F with W AND 15 decimal with W AND 00001111 binary with W IOR 0x80 with W IOR a 1 with W Load FSR0 with 0x123 Load FSR2 with 0x010 Load BSR with 2 Load BSR with 0 Load W with 3 Load W with 0x34 Multiply W by 100 Multiply W by 2 Return with W = 2 Return with W = 0x2A Subtract W from 5 Subtract W from 19 Exclusive-OR 4 with W Exclusive-OR 0xF0 with W
ANDLW
Literal
IORLW
Literal
LFSR MOVLB
FSR register number Literal
Literal
LFSR 0, 0x123 LSFR 2, 0x10 MOVLB 2 MOVLB 0 MOVLW 3 MOVLW 0x34 MULLW .100 MULLW 2 RETLW 2 RETLW 0x2A SUBLW 5 SUBLW .19 XORLW 4 XORLW 0xF0
MOVLW
Literal
MULLW
Literal
RETLW
Literal
SUBLW
Literal
XORLW
Literal


Example 3-1
MOVLW0x7F;load W with 0x7F
ADDLW1;add 1 to W
Stop:GOTO Stop;stop here
Example 3-2
MOVLW6;move 6 into W
SUBLW5;subtract W (6) from 5 Stop:GOTO Stop
A B T 000011000AND
( T = A ∙ B )
X X X X X X X X
AND 0 0 0 0 1 1 1 1
1
1
1
0 0 0 0 X X X X
Cleared to zero
Inclusive Inclusive--OR
A B T 000( T = A + B )
X X X X X X X X
IOR 0 0 0 0 1 1 1 1
0011
11
111
X X X X 1 1 1 1
Set to one
Exclusive Exclusive--OR
A B T 000101( T = A + B )
X X X X X X X X
EOR 0 0 0 0 1 1 1 1
01
11
1X X X X X X X X
Toggled
Example 3-3
MOVLW0x1F
ANDLW0xFC;clear bits 0 and 1
IORLW0xC0;set bits 6 and 7 Stop:GOTO Stop
Example 3-4
MOVLW0x90
XORLW0xE0;invert left 3 bits Stop:GOTO Stop
位操作指令
•用于操作一个字节中的某一位.
•可以设置set (1), 清除clear (0), 测试test, 和取反complement (toggle).
•测试test可用于条件语句.
Instructio n Operand
1
Operand
2
Operand
3
Examples Comment
BCF Register Bit #a-bit BCF WREG, 7
BCF 0x10, 1, 0
BCF 0x10, 1
ACCESS Clear bit 7 of W
Clear bit 1 of access bank register 0x10
Clear bit 1 of access bank
BSF Register Bit #a-bit BSF WREG, Set bit 1 of W
1
BTFSC Register Bit #a-bit BTFSC
WREG, 2If bit 2 of W is 0, then skip the next instruction
BTFSS Register Bit #a-bit BTFSS
WREG, 7If bit 7 of W is 1, then skip the next instruction
BTG Register Bit #a-bit BTG WREG,
4
Toggle (invert) bit 4 of W
Example
MOVLW0x1F
BCF WREG, 0;clear bit 0
BCF WREG, 1;clear bit 1
BSF WREG, 6;set bit 6
MOVLW0x7F
BTFSS WREG, 7;
BCF WREG, 0;clear bit 0 Stop: GOTO Stop
Example 3-7
Main:
MOVLW0x7F;program all ports as digital
MOVWF ADCON1
MOVLW0x00
MOVWF TRISB;Port B is output
MOVLW0xFF
MOVWF TRISA;Port A is input
Main1:;main program loop
MOVLW0x05
BTFSS PORTA, 0;test Pushbutton
MOVLW0x03;and skip this if Pushbutton is up
MOVWF PORTB;change Port B and the LEDs
GOTO Main1;repeat
字节指令
•指令集中最多的指令类型
•操作数为字节表示的寄存器(通用或特殊寄存器).
•常用工作寄存器(WREG),寄存器地址(寄存器文件)•操作数可以使用访问区或其他区的寄存器.
Instruction Operand1Operand2Oper
Examples Comment
and3
ADDWF reg d-bit a-bit ADDWF 0x10, W, 0Add W and access bank location 0x10, store result in W ADDWFC reg d-bit a-bit ADDWFC 0x10, 0, 0Add W with Carry and access bank location 0x10, store result
in W
ANDWF reg d-bit a-bit ANDWF 0x10, 0, 1AND W with BSR bank location 0x10, store result in W CLRF reg a-bit CLRF WREG Clear W to 0x00
COMF Reg d-bit a-bit COMF WREG One’s complement W
CPFSEQ reg A-bit CPFSEQ 0x10, 0Compare W with access bank location 0x10 and skip the next
instruction if they are equal
CPFSGT reg a-bit CPFSGT 0x12, 0Compare W with access bank location 0x12 and skip the next
instruction if contents of access bank location 0x12 is greater
than WREG
CPFSLT reg a-bit CPFSLT 0x13, 1Compare W with BSR bank location 0x13 and skip the next
instruction of contents of BSR bank location 0x13 is less than
W
DECF reg d-bit a-bit DECF WREG Subtract 1 from W
DECFSZ reg d-bit a-bit DECFSZ WREG Decrement W and skip the next instruction if the result is zero DCFSNZ Reg d-bit a-bit DCFNSZ 0x10, 1, 0Decrement access bank location 0x10 and skip the next
instruction if the result is not zero
INCF reg d-bit a-bit INCF WREG Increment W
INCFSZ reg d-bit a-bit INCFSZ WREG Increment W and if the result is zero skip the next instruction INFSNZ reg d-bit a-bit INFSNZ WREG Increment W and if the result is not zero skip the next
instruction
IORWF reg d-bit a-bit IORWF 0x10, 0, 0Inclusive-OR W with access bank location 0x10 and store the
result in access bank location 0x10
MOVF reg d-bit a-bit MOVF 0x10, 0, 0Copy access bank location 0x10 into W
MOVFF Reg source Reg dest MOVFF WREG, 0x130Copy W into location 0x130
NEGF reg a-bit NEGF WREG Two’s complement (negate) W
RLCF reg d-bit a-bit RLCF WREG Rotate left W through Carry RLNCF reg d-bit a-bit RLNCF WREG Rotate left W without Carry RRCF reg d-bit a-bit RRCF WREG Rotate right W through Carry RRNCF reg d-bit a-bit
RRNCF WREG Rotate right W without Carry
SETF WREG
Places an 0xFF into W
MOVWF reg a-bit MOVWF 0x10, 0Copy W into access bank location 0x10
MULWF reg a-bit MULWF 0x10, 0
Multiply W with access bank location 0x10, store product in PRODL and PRODH
SETF
reg
a-bit
SETF 0x11, ACCESS
Place an 0xFF into access bank location 0x11
SUBFWB reg d-bit a-bit SUBFWB 0x10, 0, 0Subtract with borrow access bank location 0x10 from W and store result in access bank location 0x10
SUBWF reg d-bit a-bit SUBWF 0x10, 0, 0Subtract W from access bank location 0x10 and store result in access bank location 0x10
SUBWFB reg d-bit a-bit SUBWFB 0x10, 0, 0Subtract W and borrow from access bank location 0x10 and store result in access bank location 0x10SWAPF reg d-bit a-bit
SWAPF WREG Swap nibbles in W
TSTFSZ
reg
d-bit
TSTFSZ WREG
Skip the next instruction if W is zero
XORWF reg d-bit a-bit XORWF 0x10, 0, 0
Exclusive-OR W with access bank location 0x10 and store the result in access bank location 0x10
Perform Multiplication by Shift
Left Operations
Multiply the 3-byte number store at 0x00…0x02 by 8
movlw0x03; set loop count to 3
loop bcf STATUS, C, A; clear the C flag
rlcf0x00, F, A; shift left one place
rlcf0x01, F, A;“
rlcf0x02, F, A;“
decfsz WREG,W,A; have we shifted left three places yet?
goto loop; not yet, continue
36
Perform Division by Shifting to
the Right
Divide the 3-byte number stored at 0x10…0x12 by 16
movlw0x04; set loop count to 4
loop bcf STATUS, C, A; shift the number to the right 1 place rrcf0x12, F, A;“
rrcf0x11, F, A;“
rrcf0x10, F, A;“
decfsz WREG,W,A; have we shifted right four places yet?
goto loop; not yet, continue
37
Example 3-8
MOVLW0xFF;store 0x01FF
MOVWF0x10;into 0x10 and 0x11
MOVLW0x01
MOVWF0x11
MOVLW0x03;store 0x0203
MOVWF0x12;into 0x12 and 0x13
MOVLW0x02
MOVWF0x13
MOVF0x10, W, ACCESS;add 0x10 and 0x12
ADDWF0x12, W, ACCESS
MOVWF0x14;save result at 0x14
MOVF0x11, W, ACCESS;add 0x11 and 0x13 with carry
ADDWFC 0x13, W, ACCESS
MOVWF0x15;save result at 0x15
Stop:GOTO Stop
Example 3-9
UDATA
Num1L RES1;variables reserved by name in UDATA Num1H RES1
Num2L RES1
Num2H RES1
AnsL RES 1
AnsH RES1
Main:
MOVLW0xff;Store 0x01ff
MOVWF Num1L;into Num1
MOVLW0x01
MOVWF Num1H
MOVLW0x03;Store 0x0203
MOVWF Num2L;into Num2
MOVLW0x02
MOVWF Num2H
MOVF Num1L;add low parts
ADDWF Num2H, W
MOVWF AnsL;save result AnsL
MOVF Num1H;add high parts with carry
ADDWFC Num2H, W
MOVWF AnsH;save result at AnsH
Stop:GOTO Stop
Example 3-10
MOVLW0xFF;Store 0x01FF
MOVWF Num1L;into Num1
MOVLW0x01
MOVWF Num1H
MOVLW0x03;Store 0x0203
MOVWF Num2L;into Num2
MOVLW0x02
MOVWF Num2H
MOVF Num1L;subtract low parts
SUBWF Num2H, W
MOVWF AnsL;save result AnsL
MOVF Num1H;subtract high parts with borrow
SUBWFB Num2H, W
MOVWF AnsH;save result at AnsH
Stop:GOTO Stop
Example 3-11
UDATA_ACS
Num RES1;register in UDATA access area Main:
MOVLW 4;load test value
MOVWF Num;save 1x at 0x10
BCF STATUS, 0;clear carry
RLCF WREG;WREG x 2
BCF STATUS, 0
RLCF WREG;WREG x 4
ADDWF Num, 0;WREG x 5
Stop:GOTO Stop
Example 3-12
UDATA_ACS
Num RES1;register in UDATA access area Main:
MOVLW5
MOVWF NUM;save the 5 in 0x10
MOVLW4;load test value
MULWF NUM;multiply by 5
MOVFF PRODL, WREG;get 8-bit product into WREG Stop:GOTO Stop
Example 3-13
MOVLW .100;load WREG with 100 decimal
BCF STATUS, 0;divide by 8
RRCF WREG
BCF STATUS, 0
RRCF WREG
BCF STATUS, 0
RRCF WREG
BTFSC STATUS, 0;round result
INCF WREG
Stop:GOTO Stop
Example 3-14
Main:
MOVLW0x00;program ports A & B
MOVWF TRISB
MOVLW0xFF
MOVWF TRISA
MOVLW0x11
MOVWF PORTB;start Port B at 0x11
Main1:
BTFSS PORTA, 4
RLNCF PORTB;rotate if pushbutton down
GOTO Main1;repeat
程序控制指令
•利用程序控制指令可以依据条件修改程序执行流程.•可以用在IF或WHILE类型的程序中.
Instruction Operand1Operand2Examples Comment
BC n1BC AGAIN Branch to AGAIN if carry is 1
BN n1BN WOW Branch to WOW if negative
BNC n1BNC FLOP Branch to FLOP if carry is 0
BNN n1BNN POSITIVE Branch to POSITIVE if not negative BNOV n1BNOV BIG Branch to BIG if no overflow
BNZ n1BNZ MORE Branch to MORE if not zero
BOV n1BOV NOW Branch to NOW if an overflow BRA n2BRA WINK Branch to WINK unconditionally BZ n1BZ BOSTON Branch to BOSTON if zero
CALL n3S CALL HOME Invoke a function called HOME CLRWDT CLRWDT Clear watchdog timer
DAW DAW Decimal adjust contents of WREG
GOTO n3GOTO ERIE An unconditional jump to ERIE
NOP NOP No operation
POP POP Removes the return address at the top of
the stack
PUSH PUSH Places the address of the next instruction
on the top of the stack
RCALL n2RCALL 6
Relative CALL to PC + 2 + 2 * 6 ahead
in the program
RESET RESET Software reset
RETFIE S RETFIE 1Return from interrupt with shadow
registers loaded into registers and
interrupts enabled
RETLW L RETLW 6Return from a function with 6 in WREG
RETURN S RETURN 1Return from a function with shadow
registers loaded into registers
SLEEP SLEEP Enter standby mode
Example 3-15
MOVLW 4;WREG ←4
SUBWF0x10, 0;WREG ← 0x10 -WREG
BNZ NOPE;Branch NOPE if not zero
MOVLW6
BRA DONE1;Branch DONE1 nconditionally
NOPE:
MOVLW9
DONE1:
MOVWF0x10
Data sheet P46 P52 about status register
49
LFSR 0, 0X20;address 0x20 with FSR0
MOVLW 0x3C MOVWF 5
Loop:
DCFSNZ 5;check counter BRA
NotFound
MOVLW 0x0D 50
;while count is not zero SUBWF POSTINC0, 0BNZ
Loop ;if not yet found
FoundIt:
; ******code for 0x0D found*****NotFound:
; ****code for 0x0D not found****Data sheet P46 P52 about status register。

相关文档
最新文档