[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-chat] how to safely use register asm("r2")?

From: Britton Kerin
Subject: [avr-chat] how to safely use register asm("r2")?
Date: Wed, 13 May 2015 15:49:34 -0800

I've got some code with tight timing that works at 10MHz
only if I register lock some globals as described here:

I'm using registers r2 through r5.  The FAQ seems to be saying
that this is safe in the above faq_regbind section, and also in

>From that last item, I conclude that this consideration from the GCC manual:

  A function that can alter the value of a global register variable
  cannot safely be called from a function compiled without this variable,
  because it could clobber the value the caller expects to find there on
  return. Therefore, the function that is the entry point into the part of
  the program that uses the global register variable must explicitly save
  and restore the value that belongs to its caller.

Doesn't apply for register r2 through r5 on AVR.

Unfortunately, I then get an issue where changing these two lines:

  uint8_t const rss = 7;
  for ( uint8_t ii = 0 ; ii < rss ; ii++ ) {


  uint8_t const rss = 7;
  for ( uint8_t ii = 0 ; ii < rss + 0 ; ii++ ) {

is required for things to work.  Adding a "+ 0" to the loop test somehow
makes it work.  Hmm.

It also works (at higher CPU speeds) if I don't declare the globals with
register locking, so I'm guessing that is somehow the problem.  The FAQ says:

  Extreme care should be taken that the entire application is compiled with
  a consistent set of register-allocated variables, including possibly used
  library functions.

What is the best way to do this?  Is it safe to assume that avr-libc doesn't
use r2 through r7, or correctly saves and restores them if it uses them
from assembly?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]