RE: [AVR-Chat] Claiming global registers in WinAVR / assembly
2007-07-18 by Tim Mitchell
organix80 wrote:
> Hi folks,
>
> I am using AVRStudio with WinAVR, with a mixed C / inline assembly
> project. For a specific application, I want to write a very tight and
> fast interrupt function in (inline) assembly.
>
> I am using the 'naked' interrupt attribute and assembly with manually
> stack control. The interrupt function must use a existing (global)
> variable, calculate something and store it again.
>
> The problem is that the WinAVR compiler uses SRAM locations for this
> global variable. This causes a larger compiled code, that uses SRAM
> and (Z) pointer instructions. When I should write a 100% assembly
> program, it is easy to manually assign dedicated registers that store
> this global value, but how to do this in C?
>
> Is it possible to claim registers that not should be used by the C
> compiler? so they could be used as a global variable in assembly
> functions.
>
I haven't done this in WinAVR C, but in imagecraft C you can refer to C
variables from asm by putting an underscore in front of the C variable
name. You could then use lds and sts instructions which would avoid
using Z.
E.g.
In C define a global var:
unsigned char anumber=42;
In asm you can then do
lds R16,_anumber
And you will get/set the value from the C part of the program.
There is probably a way to force the compiler not to use some registers,
but you'd have to use some of R16-R25 to have any code advantage over
using lds/sts. I think it is something like
register uint8_t s1 asm("r2");
There is an avrfreaks thread about this, search for thread called "how
to increase productivity of my code?"
--
Tim Mitchell
tim@sabretechnology.co.uk http://www.sabretechnology.co.uk
Sabre Technology (Hull) Ltd, 3a Newlands Science Park, Hull HU6 7TQ
Registered in England and Wales no.3131504
t:01482 801003 f:01482 801078