Jeux D - Instructions Pic16f887
Jeux D - Instructions Pic16f887
Instruction Set
PIC Microcontrollers - Programming in Assembly
Table of
Contents
Move constant to
MOVLW k k -> w 1
W
Arithmetic-logic Instructions
Add W and
ADDLW k W+k -> W C, DC, Z 1
constant
Subtract W from
SUBLW k k-W -> W C, DC, Z 1
constant
Subtract W from
SUBWF f,d f-W -> d C, DC, Z 1 1, 2
f
Logical AND
W AND k ->
ANDLW k with W with Z 1
W
constant
Logical AND
ANDWF f,d W AND f -> d Z 1 1, 2
with W with f
Logical AND
ANDWF f,d W AND f -> d Z 1 1, 2
with W with f
Logical OR with
IORLW k W OR k -> W Z 1
W with constant
Logical OR with
IORWF f,d W OR f -> d Z 1 1, 2
W with f
Logical
XORWF f,d exclusive OR W XOR f -> d Z 1
with W with f
Decrement f by
DECF f,d f-1 -> f Z 1 1, 2
1
Rotate left f
RLF f,d through CARRY C 1 1, 2
bit
Rotate right f
RRF f,d through CARRY C 1 1, 2
bit
Bit-oriented Instructions
Test bit b of f.
Skip the
BTFSC f,b following Skip if f(b) = 0 1 (2) 3
instruction if
clear.
Test bit b of f.
BTFSS f,b Skip if f(b) = 1 1 (2) 3
Skip the
following
instruction if set.
Decrement f.
Skip the
f-1 -> d skip if
DECFSZ f,d following 1 (2) 1, 2, 3
Z=1
instruction if
clear.
Increment f.
Skip the f+1 -> d skip
INCFSZ f,d 1 (2) 1, 2, 3
following if Z = 0
instruction if set.
PC -> TOS, k
CALL k Call subroutine 2
-> PC
Return from
RETURN TOS -> PC 2
subroutine
Other instructions
Arithmetic-logic Instructions
Similar to most microcontrollers, PIC supports only two arithmetic
instructions- addition and subtraction. Flags C, DC, Z are automatically set
depending on the results of addition or subtraction. The only exception is
the flag C. Since subtraction is performed as addition with negative value,
the flag C is inverted after subtraction. It means that the flag C is set if it
is possible to perform operation and cleared if the larger number is
subtracted from smaller one. Logic one (1) of the PIC is able to perform
operations AND, OR, EX-OR, inverting (COMF) and rotation (RLF and RRF).
Instructions which rotate a register actually rotate its bits through the flag
C by one bit left (toward bit 7) or right (toward bit 0). The bit shifted from
the register is moved to the flag C which is automatically moved to the bit
on the opposite side of the register.
Bit-oriented Instructions
Instructions BCF and BSF clear or set any bit in memory. Although it
seems to be a simple operation, it is not like that. CPU first reads the
entire byte, changes one its bit and rewrites the whole byte to the same
location.
retlw k1 ;...
retlw k2 ;...
... ;...
... ;...
The first line of the subroutine ( instruction ADDWF PCL,f )simply adds a
literal "k" from W register and table start address which is stored in the
PCL register. The result is real data address in program memory. Upon
return from the subroutine, the W register will contain the addressed
literal k. In this case, it is the "k2" literal. RETFIE (RETurn From IntErrupt)
represents a return from interrupt routine. In contrast to the RETURN
instruction, it may automatically set the GIE bit (Global Interrupt Enable).
When an interrupt occurs this bit is automatically cleared. Only the
program counter is pushed to the stack, which means that there is no
auto save of registers’ status and the current status either. The problem is
solved by saving status of all important registers at the beginning of
interrupt routine. These values are retrieved to these registers
immediately before leaving the interrupt routine. Conditional jumps are
executed by two instructions: BTFSC and BTFSS. Depending on the state
of bit being tested in the ‘f’ register, the following instruction will be
skipped or not.
Instructions
Legend f - Any memory location (register); W - Working register
(accumulator); b - Bit address within an 8-bit register; d - Destination bit;
[label] - Set of 8 characters indicating start of particular address in the
program; TOS - Top of stack; [] - Option; <> - bit field in register
(several bit addresses); C - Carry/Borrow bit of the STATUS register; DC -
Digit Carry bit of the STATUS register; and Z - Zero bit of the STATUS
register. ADDLW - Add literal and W Syntax: [label] ADDLW k Description:
The content of the register W is added to the 8-bit literal k. The result is
stored in the W register. Operation: (W) + k -> W Operand: 0 ≤ k ≤ 255
Status affected: C, DC, Z Number of cycles: 1 EXAMPLE:
....
C=0 (the result is not greater than 0xFF, which means that Carry has not occurred).
REG = 0xC2
REG = 0xC2
C=0 (No carry occurs, i.e. the result is maximum 8-bit long).
EXAMPLE 2:
....
------------------
Z = 0 (result is not 0)
EXAMPLE 2:
....
------------------
Z = 1( result is 0)
ANDWF - AND W with f Syntax: [label] ANDWF f,d Description: AND the W
register with register f. If d = w or d = 0, the result is stored in the W
register. If d = f or d = 1, the result is stored in register f. Operation: (W)
AND (f) -> d Operand: 0 ≤ f ≤ 127, d[0,1] Status affected: Z Number of cycles:
1 EXAMPLE 1:
....
------------------
EXAMPLE 2:
....
------------------
EXAMPLE 2:
....
FSR = 0xC2
FSR = 0xC2
BSF - Bit set f Syntax: [label] BSF f,b Description: Bit b of register f is set.
Operation: 1 -> f (b) Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7 Status affected: -
Number of cycles: 1 EXAMPLE 1:
....
EXAMPLE 2:
....
FSR = 0xC2
FSR = 0xC2
BTFSC - Bit test f, Skip if Clear Syntax: [label] BTFSC f, b Description: If bit b
of register f is 0, the next instruction is discarded and a NOP is executed
instead, making this a two-cycle instruction. Operation: Discard the next
instruction if f(b) = 0 Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7 Status affected: -
Number of cycles: 1 or 2 depending on bit b EXAMPLE:
....
After instruction:
BTFSS - Bit test f, Skip if Set Syntax: [label] BTFSS f, b Description: If bit b
of register f is 1, the next instruction is discarded and a NOP is executed
instead, making this a two-cycle instruction. Operation: Discard the next
instruction if f(b) = 1 Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7 Status affected: -
Number of cycles: 1 or 2 depending on bit b EXAMPLE:
....
After instruction:
....
....
LAB_02 ....
Z = 1
EXAMPLE 2:
Before instruction execution: FSR=0xC2
Z = 1
[label] CLRW
Z = 1
[label] CLRWDT
WDT prescaler = 0
TO = 1
PD = 1
WDT prescaler = 1: 128
; complementing
------------------
W = 0xEC
EXAMPLE 2:
....
Z = 0
Z = 1
EXAMPLE 2:
....
W = x, Z = 0
After instruction: REG = 0x13
W = 0x12, Z = 0
MOVLW .10
Loop ......
......
.....
.....
W = 0x11, Z = 0
EXAMPLE 2:
....
Z = 0
Z = 1
Z = 0
W = 0x91
W = 0x93 Z = 0
EXAMPLE 2:
....
W = 0x91
W = 0x91 Z = 0
W=0x00
Z = 0
EXAMPLE 2:
....
Z = 1
MOVLW - Move literal to W Syntax: [label] MOVLW k Description: 8-bit literal
k is moved to register W. Operation: k -> (W) Operand: 0 ≤ k ≤ 255 Status
affected: - Number of cycles: 1 EXAMPLE 1:
....
EXAMPLE 2:
Const equ 0x40
W=0x40
W=0x40
EXAMPLE 2:
....
After instruction: PC = x + 1
[label] RETFIE
GIE = 1
PC = x
[label] RETURN
RLF - Rotate Left f through Carry Syntax: [label] RLF f, d Description: The
content of register f is rotated one bit to the left through the Carry flag. If
d = w or d = 0, the result is stored in register W. If d= f or d = 1, the
result is stored in register f. Operation: (f(n)) -> d(n+1), f(7) -> C, C ->
d(0); Operand: 0 ≤ f ≤ 127, d[0,1] Status affected: C Number of cycles: 1
C = 0
W = 1100 1100
C = 1
EXAMPLE 2:
....
C = 0
C = 1
RRF - Rotate Right f through Carry Syntax: [label] RRF f, d Description: The
content of register f is rotated one bit right through the Carry flag. If d =
w or d = 0, the result is stored in register W. If d = for d = 1, the result is
stored in register f. Operation: (f(n)) -> d(n-1), f(0) -> C, C -> d(7);
Operand: 0 ≤ f ≤ 127, d -> [0,1] Status affected: C Number of cycles: 1
W = x
C = 0
After instruction: REG = 1110 0110
W = 0111 0011
C = 0
EXAMPLE 2:
....
SLEEP - Enter Sleep mode Syntax: [label] SLEEP Description: The processor
enters sleep mode. The oscillator is stopped. PD bit (Power Down) of the
STATUS register is cleared. TO bit of the same register is set. The WDT
and its prescaler are cleared. Operation: 0 -> WDT, 0 -> WDT prescaler, 1
-> TO, 0 -> PD Operand: - Status affected: TO, PD Number of cycles: 1
EXAMPLE :
....
[label] SLEEP
WDT prescaler = x
WDT prescaler = 0
TO = 1
PD = 0
W = 0x3F
EXAMPLE 2:
....
------------------
Z = 0
EXAMPLE 2:
Const equ 0x37
-------------------------------
Z = 0
------------------
EXAMPLE 2:
....
------------------
B k Branch GOTO
Branch on Digit
BDC k BTFSC GOTO STATUS,DC
Carry
Branch on No
BNC k BTFSS GOTO STATUS,C
Carry
Branch on No
BNDC k BTFSS GOTO STATUS,DC
Digit Carry
Branch on No
BNZ k BTFSS GOTO STATUS,Z
Zero
Clear Digit
CLRDC BCF STATUS,DC
Carry
Skip on Digit
SKPDC BTFSS STATUS,DC
Carry
Skip on No
SKPNC BTFSC STATUS,Z
Carry
Skip on No Digit
SKPNDC BTFSC STATUS,DC
Carry
Skip on Non
SKPNZ BTFSC STATUS,Z
Zero
Subtract Carry
SUBCF f, d BTFSC DECF STATUS,C
from File
Subtract Digit
SUBDCF f, d BTFSC DECF STATUS,DC
Carry from File