In the documentation (e.g. at http://www.nongnu.org/avr-libc/u... or http://www.nongnu.org/avr-libc/u...) I read
that the __zero_reg__ = r1 should always be 0, since the compiler or optimizer relies on this.
Also I read in the AVR Freaks Design Note #006 that after a power-on-reset or a watchdog/external reset the 32 general purpose registers are un-initialized or unchanged.
So, at start-up it is not assumed that r1 is 0.
Now to WinAVR/GCC:
On an ATmega128 16AU (DateCode 0705) I wondered, why at power-on-reset some port pins were sourcing much more current than assumed, but not after an external reset.
Than I found out that it is due to a "superfluous" command, which sets a Data-Direction-Register to inputs, fails, because the command, which initializes the zero-register
comes too late. (The port pins are connected to some switches => very low impedant, if closed!)
The initialization command is somethink like "eor r1, r1" or "eor __zero_reg__, __zero_reg__" or "clr r1" or "clr __zero_reg__".
#ifndef outp #define outp(val,sfr) (sfr)=(val) #endif outp(0x00, DDRB); outp(0xF0, PORTB);
out 0x17, r1 ; 23 ldi r24, 0xF0 ; 240 out 0x18, r24 ; 24 ... eor r1, r1
How can I avoid this behavior? Is this a bug? How can I force the compiler to do it at the beginning?
Should I do it manually? E.g. putting
__asm__ __volatile__ ("eor r1, r1"::)
at the beginning of main, bootloader, init-section to be safe?
Thank you for any hints.