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 f
ADD f,WREG
ADD Wb,Ws,Wd
ADD Wb,#lit5,Wd
ADD #lit10,Wn
- 8-bit
+
-
Replace
ADD
withADD.B
in the the 16-bit+
examples.
-
Replace
- 16-bit
-
SUB f
SUB f,WREG
SUB Wb,Ws,Wd
SUB Wb,#lit5,Wd
SUB #lit10,Wn
SUBR f
SUBR f,WREG
SUBR Wb,Ws,Wd
SUBR #lit10,Wn
- 8-bit
-
-
Replace
SUB
withSUB.B
andSUBR
withSUBR.B
in the the 16-bit-
examples.
-
Replace
- 16-bit
++
INC f
INC f,WREG
INC Ws,Wd
- 8-bit
++
-
Replace
INC
withDEC
in the the 16-bit++
examples.
-
Replace
- 16-bit
+=2
-
Replace
INC
withINC2
in the the 16-bit++
examples.
-
Replace
- 8-bit
+=2
-
Replace
INC
withINC2.B
in the the 16-bit++
examples.
-
Replace
- 16-bit
--
-
Replace
INC
withDEC
in the the 16-bit++
examples.
-
Replace
- 8-bit
--
-
Replace
INC
withDEC.B
in the the 16-bit++
examples.
-
Replace
- 16-bit
-=2
-
Replace
INC
withDEC2
in the the 16-bit++
examples.
-
Replace
- 8-bit
-=2
-
Replace
INC
withDEC2.B
in the the 16-bit++
examples.
-
Replace
- 16-bit
&
-
Replace
ADD
withAND
in the the 16-bit+
examples.
-
Replace
- 8-bit
&
-
Replace
ADD
withAND.B
in the the 16-bit+
examples.
-
Replace
- 16-bit
|
-
Replace
ADD
withIOR
in the the 16-bit+
examples. - 8-bit
|
-
Replace
ADD
withIOR.B
in the the 16-bit+
examples.
-
Replace
- 16-bit
^
-
Replace
ADD
withXOR
in the the 16-bit+
examples.
-
Replace
- 8-bit
^
-
Replace
ADD
withXOR.B
in the the 8-bit+
examples. - 16-bit
~
-
Replace
INC
withCOM
in the the 16-bit++
examples.
-
Replace
- 8-bit
~
-
Replace
INC
withCOM.B
in the the 16-bit++
examples.
-
Replace
- 16-bit clear – set all bits to zero
CLR f
CLR WREG
CLR Wd
- 8-bit clear – set all bits to zero
-
Replace
CLR
withCLR.B
in the the 16-bit clear examples.
-
Replace
- 16-bit set – set all bits to one
-
Replace
CLR
withSETM
in the the 16-bit clear examples.
-
Replace
- 8-bit set – set all bits to one
-
Replace
CLR
withSETM.B
in 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++.