Thank you all for reacting! Thanks Sergio, James, Bob and Zsolt (am I leavi= ng anyone out?). The solution of Bob yields probably the most compact (in t= ime) code, which I am after. Pure math! Of course with bookkeeping of the p= ointers, and needing both a pointer_in as pointer_out, I need more instruct= ions than only four, but I am quite happy with the method he proposes. And = I even want to suggest that Bob writes a short white paper in general how t= o implement the modulo math method with the addlw instructions. Thanks Arjen ________________________________ Van: piclist-bounces@mit.edu namens Bob Ammerman = Verzonden: maandag 7 augustus 2017 19:52 Aan: 'Microcontroller discussion list - Public.' Onderwerp: RE: [PIC] pic16f628a memory bank switching in a large circular b= uffer. NOTE: all numbers are in HEX below. The trick below works because we are working MODULO 100 hex. Input: W is a pointer ranging from 00h thru 9Fh Output: W points to the correct register in bank 0 or 1 ; At entry, the value of W is as follows: ; 00..4F - we want to map to page 0 addresses 20..6F ; 50..9F - we want to map to page 1 addresses A0..EF ; We start by adding B0. This will set STATUS,C to 1, ; if we have a page 1 address, and to 0 if we have a ; page 0 address. After this add, we will have: ; B0..FF - which we want to map to page 0 addresses 20..6F ; 00..4F - which we want to map to page 1 addresses A0..EF ADDLW 0B0h ; Now, if carry is set, we add 30. After this conditional ; add, we will have: ; B0..FF - which we want to map to page 0 addresses 20..6F ; 30..7F - which we want to map to page 1 addresses A0..EF BTFSC STATUS,C ADDLW 030h ; One more add gives us our answer. By adding 70 we get: ; 20..6F for page 0 addresses ; A0..EF for page 1 addresses ADDLW 070h -- Problem solved in 4 instructions! ~ Bob Ammerman RAm Systems -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive PIClist, Microchip PIC www.piclist.com The PICList is a collection of people interested in the Microchip PIC proce= ssor. View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .