On 18/04/2012 03:36, William Swanson wrote:
> On Tue, Apr 17, 2012 at 7:03 AM, David Brown<da...@westcontrol.com>  wrote:
>> You have to be particularly careful about read-modify-write codings.
>> The msp430 can do some of these actions atomically, but it is possible
>> that the compiler will split the actions up.  For example, if you write
>> "a += 1; b = a" then then compiler might read "a" into a register, do
>> the addition, then save the value to "a" as three separate actions, so
>> that it can re-use the saved value for "b".
>
> If you declare "a" as "volatile," that will tie the compiler's hands
> and force the reads and writes to occur exactly as specified.

The details of whether something like "a += 1" must be a read, modify, 
write, or a single RMW instruction, are not specified even if "a" is 
volatile.  Of course, in this example, if "a" is volatile then "b = a" 
would force a re-read of "a", negating any benefits of keeping it in a 
register.

> Any
> variable shared between ISR-land and normal code should be "volatile"
> to avoid surprises like this.
>

That's okay as a beginner's rule, as you err on the side of caution, but 
there are lots of situations when non-volatile variables are more 
efficient.  You just have to be careful about where and when you force 
volatile accesses on the non-volatile variables, or use memory barriers 
to ensure everything is done in the required orders.


------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second 
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to