instruction formats for the pic series. rom encoding instructions are encoded in binary in rom. the...
TRANSCRIPT
![Page 1: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/1.jpg)
Instruction formats
for the PIC series
![Page 2: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/2.jpg)
ROM encoding
Instructions are encoded in binary in ROM.
The instructions are fixed format, each occupying 14 bits.
The division of the bits into fields is flexible.
![Page 3: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/3.jpg)
byte oriented register ops
when d=0 destination = W reg
d=1 destination = reg[f]
f= 7 bit register selector
opcode d f ( reg num )
067813
![Page 4: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/4.jpg)
Bit oriented operations
b = 3bit number identifying a bit in an 8 bit register
f = 7 bit number identifying one of 128 possible registers
opcode b f
013 10 9 7 6
![Page 5: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/5.jpg)
Literal ops
These generally operate on the w register The second operand is a value specified in
the instruction W:= w op k
opcode K=Literal value
07813
![Page 6: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/6.jpg)
Control transfer
Used for call or goto K is a 11 bit literal that specifies an address
in the program memory
opcode K
0101113
![Page 7: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/7.jpg)
Example binary instructions
We will look at the binary layout of some of the instructions in the instructionset before going on to look at the way these are accessed in assembler
Goto 6101 000 0000 0110
Binary forgoto
Binary for 6
![Page 8: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/8.jpg)
Remember about registersGeneral registers 128 of them
Working or W register
Program counter or PC
0
127
![Page 9: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/9.jpg)
Add 3 to W
Add literal instruction Value to add = 3
11 1110 0000 0011
![Page 10: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/10.jpg)
Add register 33 to w
00 111 0 010 0001
Add regDestinationIs W
Register numberIs 33
W := W + Reg[33]
![Page 11: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/11.jpg)
Add w to register 3300 111 1 010 0001
Add regDestinationIs reg 33
Register numberIs 33
Reg[33] := w + reg[33]
This is what Differs fromLast Instruction
![Page 12: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/12.jpg)
Disadvantages of binary
Hard to remember Very hard to remember for complex
instructionsets Allows no variation in word lengths
between different processor models ( PICs come with 12, 14 and 16 bit instructions )
![Page 13: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/13.jpg)
Assembler
Replaces each binary instruction with a line of text
Opcodes replaced by mnemonic names Operands specified as symbolic labels or
decimal or hex numbers Software package translates to binary
![Page 14: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/14.jpg)
Assembler process
![Page 15: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/15.jpg)
What assembler looks likestart clrw main movlw 0x35 movwf mulplr ; test 0x35 times 0x2D movlw 0x2D movwf mulcnd
call_m call mpy_S ; The result is in file ; registers H_byte & L_byte ; and should equal 0x0951
labels
opcodes
Operands
comments
Comments start with ;
![Page 16: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/16.jpg)
A simple example
What we want to compute is
Y:= x+5
We must associate these variables x,y with registers.
We must select machine instructions that will perform the calculation
![Page 17: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/17.jpg)
Assign variables X assume it is 8 bit
integer We will put it in
register 20h We will put Y in
register 21h
General registers
Specialregisters
0
20h
7f
Register bank
![Page 18: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/18.jpg)
Analyse possible data flows
1. W := x2. W:=w+53. Y:=w
YES
1. W:=52. W:=w+x3. Y:=w
YES
W:=xY:=wY:=y+5
NO, cant add5 to y
![Page 19: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/19.jpg)
MOVLW 5 ; w:=5
ADDWF 20h,0 ; w:= w + reg[20h]
MOVWF 21h ; y:=w
Y:=x+5
0 indicates w is dest
![Page 20: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/20.jpg)
Outline the instructions
We will now look at the instructionset of the PIC processor.
There are 35 instructions
![Page 21: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/21.jpg)
Register addition
ADDWF f,d
Add reg[f] to w and store in either w or reg[f] depending on d,
if d=0 then store in w, else in reg[f]
If reg[24h] =6 and w=4 then
ADDWF 24h,1
Sets reg[24h] to 10
![Page 22: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/22.jpg)
Addition of a constant
ADDLW const
Add const to w and store in w
If w=4 then
ADDLW 24h
Sets w to 28h
![Page 23: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/23.jpg)
andwf
ANDWF f,d
And reg[f] with w and store in either w or reg[f] depending on d,
if d=0 then store in w, else in reg[f]
If W = 0001 1111 and reg[20h]= 1111 0100
ANDWF 20h,0
will set w to 0001 0100
![Page 24: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/24.jpg)
ANDLW
ANDLW const
And const with w and store in w
If W = 0001 1111 and const= 6=0000 0110
ANDLW 6
will set w to 0000 0110
![Page 25: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/25.jpg)
Clear registers
Clrf f set reg[f] to zero• Eg CLRF 40 ; reg[40]:=0
Clrw set w register to zero
![Page 26: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/26.jpg)
MOVE OPERATIONS
MOVFW f Moves contents of register f to the W register
MOVWF f Moves the W reg to register f
MOVLW const Moves the literal constant to the W register
Last two letters are memonics FW,WF,LW
![Page 27: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/27.jpg)
NOP
NOP stands for NO oPeration It is an opcode that does nothing Its binary pattern is
00000 0 0000 0000
This is similar to MOVWF whose pattern is
00000 1 FFFF FFFF
Destination bit
Destination register
Destination=w
![Page 28: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/28.jpg)
subtractions
This can be done by using complement or subtract operations, subtract is not strictly needed
COMF f,dThis sets either reg[f] or w to –reg[f]For example if x is in reg[32] and y in
reg[33] then x:=x-y would be done byCOMF 33,0 ; w:=-yADDWF 32,1 ; x:=x+w
![Page 29: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/29.jpg)
Suppose we want reg[32]:=reg[33]-reg[40]Initial values 10 7 4Binary 00001010 00000111 00000100Code Values manipulated
Comf 40, 0 ; 00000100 →11111011+1 →11111100 →w
Addwf 33,0 ; 00000111 11111100 +
→w
Movwf 32 ; → reg[32]
Complement continued
000000111
Carry bit
00000011
![Page 30: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/30.jpg)
SUBWF
Subtract w from f
SUBWF f,d
This has two forms SUBWF f,0 ; w:= reg[f]-w SUBWF f,1 ; reg[f]:= reg[f]-w
Comf 33,0 ; w:=-yAddwf 32,1 ; x:=x+w
MOVFW 33 ;w:=y
SUBWF 32,1;x:=x-w
Insteadof
![Page 31: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/31.jpg)
Decrement register
Decf f, d
Decrements reg[f] and stores result in either reg[f] or w depending on d
DECF 50,1
Subtracts 1 from register 50
DECF 50,0
Sets w := reg[50] -1
![Page 32: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/32.jpg)
Decrement and skip
DECFSZ f,dMeaning of f, and d fields as beforeIf the result of decrementing is zero, skip the next
instructionTop: ;some instructions DECFSZ 38,1
GOTO Top ; some other instructions
Reg[38] holds the number of times to go round loop
![Page 33: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/33.jpg)
Incrementing
INCF and INCFSZ work like DECF and DECFSZ except that they increment
In this case you would load a negative number into your count register and count up towards zero.
Alternatively, count up, and skip when the result would have been 256.
Incfsz 50,1; means reg[50] := reg[50]+1
if reg[50] is 0 then skip next instruction
![Page 34: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/34.jpg)
Inclusive or
IORWF f,d
Example
If w=1100 0001 and reg[40]=0001 0001
IORWF 40,0
Will set w= 1101 0001 1100000100010001 or 11010001
![Page 35: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/35.jpg)
Inclusive or Literal
IORLW const
Example
If w=1100 0001
IORLW 7
Will set w= 1100 0111 1100000100000111 or 11000111
![Page 36: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/36.jpg)
Exclusive or
XORWF f,d
Example
If w=1100 0001 and reg[40]=0001 0001
XORWF 40,0
Will set w= 1101 0000 1100000100010001 xor 11010000
![Page 37: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/37.jpg)
Exclusive or Literal
XORLW const
Example
If w=1100 0001
XORLW 7
Will set w= 1100 0110 1100000100000111 xor 11000110
![Page 38: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/38.jpg)
Bit operations
BCF f,b set bit b of register f to 0
BSF f,b set bit b of register f to 1
Eg
BCF 34,1
Clears bit 1 of register 34
![Page 39: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/39.jpg)
Test instructions
BTFSC f,b ; bit test skip on clear
BTFSS f,b ; bit test skip on set
Eg
INCF 33
BTFSC 33,4
GOTO OVERFLOW ; goto overflow when
; reg 33 >7
![Page 40: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/40.jpg)
GOTO
GOTO labelEg GOTO home….. home MOVLW 7….Transfers control to the label
![Page 41: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/41.jpg)
CALL and RETURN
Thare used for subroutines or procedures.
CALL foo
….
foo ; start of procedure
…. ; body of procedure
RETURN; end of procedure
![Page 42: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/42.jpg)
CALL continued
When a call occurs the PC+1 is pushed onto the stack and then the PC is loaded with the address of the label
When return occurs the stack is popped into the PC transferring control to the instruction after the original call.
![Page 43: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/43.jpg)
example call source
Init
call increment
goto Init
increment
incf CountL
return
labels
![Page 44: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/44.jpg)
Example call and return
Address opcode assembler 5 2007 CALL 0x7 6 2805 GOTO 0x5
7 0AA2 INCF 0x22, F ; increment reg
0x22 8 0008 RETURN
at start we have
state of the stack
![Page 45: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/45.jpg)
Next step
Address opcode assembler 5 2007 CALL 0x7 6 2805 GOTO 0x5
7 0AA2 INCF 0x22, F 8 0008 RETURN
stack holds addressto return to
![Page 46: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/46.jpg)
just before return
![Page 47: Instruction formats for the PIC series. ROM encoding Instructions are encoded in binary in ROM. The instructions are fixed format, each occupying 14 bits](https://reader035.vdocuments.mx/reader035/viewer/2022070305/5515c9cd550346a3758b4a86/html5/thumbnails/47.jpg)
after return
stack pointer is retracted