This post describes MOVF and MOVFF Instructions for PIC Microcontroller with examples. MOVF
and MOVFF
instructions are used to copy values inside the PIC microcontroller.
Note: For this tutorial I am using MPLAB X v5.0 but you can use any version upto v5.35. Version 5.35 is last version that included MPASM i.e. the assembler we require to compile the assembly code used in this tutorial. Code given here may not work for later versions.
MOVF: Move f
Syntax: MOVF f{,d{,a}}
Length of the Instruction: 2 bytes
Instruction Cycles: 1
Instruction Encoding: 0101 00da ffff ffff
Updated Flags: Z, N
Destination: WREG
if d = 0
/ d = w
. File register f
if d = 1
/ d = f
. Destination is optional and the default is d = 1 / d = f
.
Access Bit: Access bank is used if a = 0
. BSR is used to select the bank if a = 1
. Access bit is optional and the default is a = 0
.
Description: PIC Microcontroller MOVF
Instruction copies the contents of file register f
to WREG
or f
itself (depending on d
bit).
MOVF Instruction Example 1:
SETF TRISC ; Make PORTC an Input port
MOVF PORTB, W ; Copy the contents of PORTB to WREG.
MOVF
instruction copies the contents of PORTB
into WREG
.
MOVF Instruction Example 2:
The destination can be the file register f
itself. This is used to affect the status flags Z and N based on the value of f
.
MOVLW 0xB5 ; Copy 0xB5 to WREG
MOVWF 0x01 ; Copy WREG to file register 0x01
MOVF 0x01, F ; Copy the contents of file register 0x01 to itself. N = 1 and Z = 0 in status register
Before the execution of MOVF
, the file register 0x01
contains 0xB5
. MOVF
copies the file register 0x01
to itself while updating Z and N status flags. N = 1
and Z = 0
, since the number in f
is negative and non-zero.
MOVFF: Move f to f
Syntax: MOVFF fs, fd
Length of the Instruction: 4 bytes
Instruction Cycles: 2
Instruction Encoding: 1100 ffff ffff ffff(s) 1111 ffff ffff ffff(d)
Updated Flags: None
Source and Destination: Source file register fs
and destination file register fd
are represented by 12-bits in the opcode. They can be anywhere in the 4096-byte RAM space and no bank switching is required.
The MOVFF
instruction cannot use the PCL, TOSU, TOSH or TOSL as the destination register.
Description: PIC Microcontroller MOVFF
Instruction copies the contents of the source file register f
s to destination file register f
d.
MOVFF Instruction Example 1:
CLRF TRISB ; Make PORTB an Output Port
MOVLW 0xB5 ; Copy 0xB5 to WREG
MOVWF 0x01 ; Copy WREG to file register 0x01
MOVFF 0x01, PORTB ; Copy the contents of file register 0x01 to PORTB
Before the execution of MOVFF
, the file register 0x01
contains 0xB5
. MOVFF
copies the contents of the file register 0x01
to PORTB
. PORTB = 0xB5
at the end.
MOVFF Instruction Example 2:
CLRF TRISB ; Make PORTB an Output Port
SETF TRISC ; Make PORTC an Input Port
MOVFF PORTC, PORTB ; Copy the contents of file register PORTC to PORTB
MOVFF
copies the contents of the PORTC
to PORTB
.