* * *

Author Topic: AVR: Reserve register for assembly or register variable  (Read 1102 times)

kupferstecher

  • Full Member
  • ***
  • Posts: 218
AVR: Reserve register for assembly or register variable
« on: December 11, 2017, 09:02:12 pm »
Hello,

on AVR I have some assemebly sections in order to speed things up in an interrupt service routine. On each interrupt call a counter is incremented. This counter I would like to keep in a register instead of in a variable. Is there any chance to do that? I.e. the register has to be reserved so that the compiler never uses it, so its preserved between my inline assembler calls. Or, even better would be if its possible to define a global variable to be kept as register. On AVR controllers there are 30 registers, so some reserved registers wouldn't hurt. The interrupt in my application will be called every 100 clock cycles, so the loads and stores to memory do make a difference.

Regards

ccrause

  • Full Member
  • ***
  • Posts: 129
Re: AVR: Reserve register for assembly or register variable
« Reply #1 on: December 13, 2017, 01:08:34 pm »
I don't think it is currently possible to reserve registers.  For the AVR target FPC uses the avr-libc convention.  There is no mention of reserved registers for user purposes.

An alternative could be to use general purpose I/O registers if available on your particular controller (such as GPIOR0 - GPIOR2 on atmega328p).  At least you can read/write these using IN/OUT instructions - this may save you a few 2 clock cycles compared to memory read/write using LDS/STS.

kupferstecher

  • Full Member
  • ***
  • Posts: 218
Re: AVR: Reserve register for assembly or register variable
« Reply #2 on: December 13, 2017, 02:17:46 pm »
Thanks for the reply.

In C-Language its possible to assign a register to a variable and keep it in the register. But you are right, in the link you posted it seems that all registers may be used by c-lib-procedures. If the registers are used in assembler, the compiler doesn't have control over them. I'm not sure though if the gcc allows to use a compiler chosen register in inline assembly.

For my problem I probably change the program structure and use the hardware counters, but this will be less flexible in the end.

Regards

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus