This post describes MOVLW and MOVWF Instructions for PIC Microcontroller with examples. MOVLW
and MOVWF
instructions are used to copy values to and from WREG respectively.
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.
MOVLW: Move Literal to W
Syntax: MOVLW k
Length of the Instruction: 2 bytes
Instruction Cycles: 1
Instruction Encoding: 0000 1110 kkkk kkkk
Updated Flags: None
Description: MOVLW instruction loads the 8-bit literal value ‘k
‘ into the WREG.
MOVLW Instruction Example 1:
MOVLW 0x85 ; Copy 0x85 to WREG
MOVLW D'100'; ; Copy Decimal 100 into WREG
The first MOVLW
instruction copies an 8-bit hexadecimal value 0x85
into WREG
. The second MOVLW
instruction copies an 8-bit decimal value 100
into WREG
.
MOVLW Instruction Example 2:
COUNT_Value EQU D'100' ; define a literal using EQU directive
MOVLW COUNT_Value ; load the literal 'COUNT_Value' into WREG
In this example, first a literal is defined using EQU directive and then loaded into WREG
using MOVLW
.
MOVWF: Move W to f
Syntax: MOVWF f {,a}
Length of the Instruction: 2 bytes
Instruction Cycles: 1
Instruction Encoding: 0110 111a ffff ffff
Updated Flags: None
Destination: The destination is always the file register f
and cannot be changed.
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: MOVWF instruction copies the contents of WREG
to a file register ‘f
‘.
MOVWF Instruction Example 1:
MOVLW 0x85 ; Copy 0x85 to WREG
MOVWF 0x01 ; Copy WREG to file register 0x01
First, MOVLW
instruction loads an 8-bit hexadecimal value 0x85
into WREG
. Then MOVWF
is used to copy the contents of WREG (0x85)
into RAM location 0x01
. Loading a specific literal value into a file register is a two-step process.
MOVWF Instruction Example 2:
COUNT EQU 0x01 ; Define a variable in file register (RAM)
COUNT_Value EQU D'100' ; define a literal using EQU directive
MOVLW COUNT_Value ; load the literal 'COUNT_Value' into WREG
MOVWF COUNT ; Copy WREG into COUNT variable
First, MOVLW
instruction loads an 8-bit hexadecimal value COUNT_Value
(defined using EQU directive) into WREG
. Then MOVWF
is used to copy the contents of WREG
into a RAM location COUNT
(defined using EQU directive).