avr-gcc-list
[Top][All Lists]
Advanced

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

RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignm


From: Dave Hylands
Subject: RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignments
Date: Wed, 8 Dec 2004 11:20:40 -0800

Hi Ned,

> I only define the registers as volatile when their bits could 
> be changed outside my program control.

That's still not quite good enough.

Consider:

        SOME_REG = 0;
        SOME_REG = 1;
        SOME_REG = 0;

without the volatile keyword, the compiler can "safely" optimize this to
become:

        SOME_REG = 0;

which may no longer produce the desired result. 

Another classic example that I've seen (not on the AVR) goes along the
lines of:

        SOME_REG_NUM = 3;
        SOME_REG_VAL = 1;       // Stores the value 1 into register 3

which the compiler can happily rearrange to become

        SOME_REG_VAL = 1;       // Stores the value 1 into last accessed
register
        SOME_REG_NUM = 3;

Even though these register might be write-only and nobody but little
piece of code modifies them, failure to use the volatile keyword can get
you code that doesn't function properly.

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/ 



reply via email to

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