The One With Addressing Modes and BANK SWITCHING
The One With Addressing Modes and BANK SWITCHING
ADDRESSING MODES….
• How CPU is accessing Data……
• INDEXED
Recap….
• The 256-byte access bank file register is split into
two sections: The lower addresses, 00 to 7FH, are assigned to
the general purpose registers, and the upper addresses, F80-
FFFH, to the SFR.
•The access bank is the default bank when the PICI8 is
• When we move data into INDFx we are moving data into a RAM
location pointed to by the FSRx.
• Must use "INCF FSR0L, F" to increment the pointer because there
• Use MPLAB IDE to see the memory locations after the execution of
these programs.
Auto-increment option for FSR
• Because the FSR is a 12-bit register, it can go from 000 to
FFFH, which covers the entire 4K RAM space of the PIC18.
• Using the "INCF FSROL, F“ instruction to increment the
pointer can cause a problem when an address such as 5FFH is
incremented.
• The instruction "INCF FSROL, F" will not propagate the
carry into the FSR1H register.
• The PIC18 gives us the options of auto-increment and auto-
decrement for FSRn to overcome this problem.
The syntax used for such cases for the CLRF instruction
CLRF INDFn After clearing fileReg pointed to by FSRn, the FSRn stays the same.
CLRF POSTINCn After clearing fileReg pointed to by FSRn, the FSRn is incremented.
CLRF PREINCn The FSRn is incremented, then fileReg pointed to by FSRn is cleared.
CLRF PLUSWn Clears fileReg pointed to by (FSRn +WREG), FSRn & W unchanged.
• This table shows the syntax for the CLRF instruction, it works for all such
instructions.
• The auto-decrement or auto-increment affects the entire 12 bits of the FSRn
and has no effect on status register.
• This means that FSR0 going from FFF to 000 will not raise any flag.
• The option of PLUSWn is widely used for a RAM-based look-up table.
Clear 16 RAM locations starting at
RAM address 60H.
COUNTREG EQU 0x10 ;fileReg loc for counter
CNTVAL EQU 0x16 ;counter value
MOVLW CNTVAL ;WREG = 16
;load the counter, Count = 16
MOVWF COUNTREG ;load pointer. FSR1 = 60H, RAM
LFSR l,Ox60 Address
B2 CLRF INDF1 ;clear RAM loc FSRI points to
INCF FSR1L,F ;increment FSR1L, point to next lo
DECF COUNTREG,F ;decrement counter
;loop until counter = zero
BNZ B2
COUNTREG EQU 0xl0 ;fileReg loc for counter
CNTVAL EQU 0'16' ;counter value
LFSR FSR0,0x100 ;
NEXT CLRF POSTINC0 ; Clear INDF
; register then
; inc pointer
BTFSS FSR0H, 1 ; All done with
; Bank1?
GOTO NEXT ; NO, clear next
CONTINUE ; YES, continue
INDIRECT ADDRESSING OPERATION
Bank Switching…
• All the instructions we have used so far assumed the access bank as the
default bank.
• This was achieved by ignoring the letter A in instructions such as
"MOVWF fileReg, A".
• In other words, the instruction "MOVWF fileReg" is really
"MOVWF f i leReg, A" where the A bit can be 0 or 1.
• If A = 0, then the access bank is the default bank. If A = 1, however, then the
instruction will use the bank selector register (BSR) to select the bank instead of
using the access bank.
• If A is not stated in a given instruction, it means A = 0 and the access bank is the
default bank.
• It is for the simple reason of making the PIC18 Assembly language easier to
understand and master.
The BSR register and bank switching
• To use banks other than the access bank, we need to set
bit A = 1 in the coding of the instruction.
• With A = 1, we use the BSR (bank select register) to choose
the desired bank.
• The BSR is an 8-bit register and is part of the SFRs.
• 1. Load the BSR with the desired bank number (Use ‘MOVLB’ instruction to
do this.)
• BSR contains the higher 4 bits of the 12 bit RAM address (ie, the Bank No.).
This is needed when only 8 bit space is available in the instruction for the
address –For such instructions, remaining 4 bits have to be taken from the
BSR, to cover the 4k space.
• Eg. MOVWF instruction can contain only 8 bit address.
MYREG EQU 0x40
MOVLB 0x2 ;load 2 into BSR (use bank 2)
MOVLW 0 ;WREG = 0
MOVWF MYREG, 1 ;loc 0x240 (0) , WREG 0, Notice A =1