Y register and local data

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm experiencing a problem with local data that is pointed to by the Y-register.

I'm using avr-gcc (GCC) 3.3 20030421 (prerelease), AvrX 2.6e, ATmega8.

The WinAVR documentation (manual section 7.3.14) states that the Y-register may be used as a pointer for local data and should be restored if changed.

As far as I can tell, the Y-register is initialised to top of RAM during initialisation. It is not initialised in the relevant subroutine. Y is used to store the return value from a function call like this: std Y+1, r24. I assume that if Y is pointing to top of RAM, then Y+1 is pointing to R0 (i.e. wraps around).

AvrX initialises its task stacks with all zeros, so when the task switch occurs at the end of main() Y is no longer pointing at top of RAM, but at zero. This is obviously a problem. Because Y+1, now points to R1, which GCC assumes to be always equal to zero. R1 not equal to zero is causing grief elsewhere. It is the bug caused by this that I originally started tracking down.

So the solution should be simple: Y should have the correct value. However, I don't know what the correct value should be. In AvrX this should be different for each task. It can't be initialised to the current value of the stack pointer, because Y+1 would then point the the most recent value pushed on the stack.

Can somebody please shed some light on how WinAVR uses the Y-register? Is it just an alternative way of referring to R0? How to determine an appropriate alternative value for Y? Is there more then one problem here?

Thanks in advance
Arthur

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avg-gcc can use Y as a frame pointer, i. e. as a
pointer to local data on the stack. Whether it
does or not depends on whether there are local
data on the stack at all, so there's no general
rule about what Y should point to. Those routines
that need a frame pointer do initialize it
appropriately (it's going to point somewhere into
the regular [return] stack), and they do expect
any called functions to preserve it.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Found the solution. AvrX declares its tasks as "naked" functions and hence the frame pointer is not being initialised. Changing the declaration to "noreturn" fixes the problem.
(Yahoo Groups AvrX http://groups.yahoo.com/group/av...)