PIC18 cheatsheet

SWAPF

Detailed reference for the SWAPF instruction in PIC18 ISA.

Description

Swap the upper and lower 4‑bit nibbles of the 8‑bit file register f.

  • The operation is performed in place on f.
  • d = 0 → the swapped value is stored in WREG.
  • d = 1 → the swapped value overwrites the original contents of register f.
  • There is no a (access‑bank) bit for SWAPF; the file register address can refer to any location.

Examples

; d=0 → result goes into WREG (Access bank example)
SWAPF 0x20, 0   ; W = 0xBA  (if 0x20 originally contains 0xAB)
; d=1 → result written back to file register
SWAPF 0x20, 1   ; 0x20 = 0xBA (if 0x20 originally contains 0xAB)
; Preload WREG with a literal, then d=0 → WREG is overwritten by swapped value
MOVLW 0x10
SWAPF 0x20, 0   ; W = 0xBA  (ignores previous WREG value)