Use of FSR registers

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Use of FSR registers

Post by SHughes_Fusion » Mon Aug 18, 2014 10:12 am

Are there any general rules as to which FSR registers the compiler uses and which are 'safe' for the user to take advantage of in their own code?

I'd been seeing a problem with my bootloader which I finally narrowed down to the fact I'd moved some code which accesses a const. My code uses FSR0 and FSR1 for accessing two buffers but FSR1 seems to be used by the code to access consts.

To try and make this code as robust as possible and avoid future problems it would be useful to know if there are any things I can be sure of when it comes to use the FSRs or whether I need to check the asm to be sure. If the latter, what instructions do I need to look for which modify the FSRs?

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Use of FSR registers

Post by Jerry Messina » Mon Aug 18, 2014 1:40 pm

I don't think there are any hard and fast rules... the compiler is free to use what it wishes.

If the code involves a string, an array, a structure, or anything 'byref' you can pretty much bet that one (or more) of the FSR registers are involved.
Normally FSR2 is "free to use", but that's just an observation and not a rule.

If I want to use an FSR I always examine the asm, and if I see the compiler using one of them I usually rewrite the code at a lower level so that I'm the one using the FSR registers and not the compiler. That way you don't have to worry about which one it picks. Sometimes you're safe just saving/restoring the FSR into a temp variable, but that depends on what the code is doing. If you call a subroutine, don't assume that the registers weren't touched when you return.

Things to look for:

The FSRx register addresses are assigned alias names A0, A1, and A2 (and AxH), so you'll typically see them in the asm code

Code: Select all

A0 EQU 4073
A0H EQU 4074
A1 EQU 4065
A1H EQU 4066
A2 EQU 4057
A2H EQU 4058
Instructions that use them include the mnemonics LFSR, INDFx, POSTINCx, POSTDECx, and PREINCx, but you'll probably also see MOV to those alias names.

Post Reply