Addressing modes
The PIC architecture has several addressing modes that are needed to access C arrays, structures, and pointers.
- File register —
sum - Register direct —
W5 - Register indirect —
[W6] - Immediate —
#2012
Types of register indirect addressing
- No modification:
[Wn] - Pre-increment:
[++Wn] - Pre-decrement:
[--Wn] - Post-increment:
[Wn++] - Post-decrement:
[Wn--] - Register offset:
[Wn+Wb]
The ++ and -- increment or decrement by two
for word operations and by one for byte operations.
Also, the following is not allowed because the
offset register differs in the two fields.
MOV [W5+W6],[W7+W8]
Literal offset
MOV allows signed literal register offsets as
either source or destination. Here are a couple of examples:
MOV W5, [W6+32]
MOV.B W5, [W6+15]
MOV [W7-1000], W13
MOV [W4+5], [W8+13] ;; ILLEGAL -- only one literal offset
MOV [W7+2000], W13 ;; ILLEGAL -- offset too large
MOV.B [W7+1000], W13 ;; ILLEGAL -- offset too large for byte
Places to use these addressing modes
- Arrays —
V[i] - Structures —
P.x - Pointers —
*P - Local varaibles
Some C operators and their PIC equivalents
In these examples, Wb,
Wn,
Wns, and
Wnd are registers;
Ws and
Wd are registers with indirect addressing modes;
Wso and
Wdo are registers with indirect
and offset addressing modes;
lit10 is a 10-bit literal (0 to 1023), and
lit5 is a 5-bit literal (0 to 31).
- 16-bit
+ADD fADD f,WREGADD Wb,Ws,WdADD Wb,#lit5,WdADD #lit10,Wn
- 8-bit
+-
Replace
ADDwithADD.Bin the the 16-bit+examples.
-
Replace
- 16-bit
-SUB fSUB f,WREGSUB Wb,Ws,WdSUB Wb,#lit5,WdSUB #lit10,WnSUBR fSUBR f,WREGSUBR Wb,Ws,WdSUBR #lit10,Wn
- 8-bit
--
Replace
SUBwithSUB.BandSUBRwithSUBR.Bin the the 16-bit-examples.
-
Replace
- 16-bit
++INC fINC f,WREGINC Ws,Wd
- 8-bit
++-
Replace
INCwithDECin the the 16-bit++examples.
-
Replace
- 16-bit
+=2-
Replace
INCwithINC2in the the 16-bit++examples.
-
Replace
- 8-bit
+=2-
Replace
INCwithINC2.Bin the the 16-bit++examples.
-
Replace
- 16-bit
---
Replace
INCwithDECin the the 16-bit++examples.
-
Replace
- 8-bit
---
Replace
INCwithDEC.Bin the the 16-bit++examples.
-
Replace
- 16-bit
-=2-
Replace
INCwithDEC2in the the 16-bit++examples.
-
Replace
- 8-bit
-=2-
Replace
INCwithDEC2.Bin the the 16-bit++examples.
-
Replace
- 16-bit
&-
Replace
ADDwithANDin the the 16-bit+examples.
-
Replace
- 8-bit
&-
Replace
ADDwithAND.Bin the the 16-bit+examples.
-
Replace
- 16-bit
|-
Replace
ADDwithIORin the the 16-bit+examples. - 8-bit
|-
Replace
ADDwithIOR.Bin the the 16-bit+examples.
-
Replace
- 16-bit
^-
Replace
ADDwithXORin the the 16-bit+examples.
-
Replace
- 8-bit
^-
Replace
ADDwithXOR.Bin the the 8-bit+examples. - 16-bit
~-
Replace
INCwithCOMin the the 16-bit++examples.
-
Replace
- 8-bit
~-
Replace
INCwithCOM.Bin the the 16-bit++examples.
-
Replace
- 16-bit clear – set all bits to zero
CLR fCLR WREGCLR Wd
- 8-bit clear – set all bits to zero
-
Replace
CLRwithCLR.Bin the the 16-bit clear examples.
-
Replace
- 16-bit set – set all bits to one
-
Replace
CLRwithSETMin the the 16-bit clear examples.
-
Replace
- 8-bit set – set all bits to one
-
Replace
CLRwithSETM.Bin the the 16-bit clear examples.
-
Replace
Uses of these instructions
x = y + a ;x = y + 10 ;x = y + 1000 ;x = ++y + a ;x = 3*x + 1 ;x = x && 0xBEEF ;x = 0 ;
C declarations
You cannot depend on short, int,
and long in C.
Use the fixed width
types of C99 and C++.