On 2006-07-03, David Brown <[email protected]> wrote:
>> but in this case, reading the C source code, I honestly can't
>> understand for sure what it meant....if I were to translate
>> manually that code into asm, I would have asked a
>> clarification to the programmer: did you want to sample twice
>> the port and then add these two samples ? (say for making an
>> average) or did you want two sample the port once and double
>> that reading ? IMHO the answer wasn't clear, even if I
>> considered all the volatile qualifiers;
>
> In that case, you simply don't have the experiance to
> understand volatiles properly. The code was perfectly clear -
> the port should be sampled twice, and it's sum taken. If
> Grant had wanted to sample it once and double it, he would
> have writen "two = 2 * P6IN;". Of course, in real code the
> function and variable names would have given an indication of
> *why* he was doing this, but the code itself is perfectly
> clear.
I'd like to point out that
1) I didn't write that code originally, I ran across that
example in a thread in the yahoo msp430 list.
2) The example isn't supposed to do anything useful, it's just
a simple test-case that demonstrates a bug in the compiler.
> Local variables are, in general, more efficient than global variables - but
> they are not more efficient than calculations carried out internally by the
> compiler using registers. Add variables when they make sense in your code,
> and make the logic of your code clearer - don't do it for the compiler's
> sake.
Especially since the compiler will usually just optimize the
temporary variables away and you'll end up with the same code
either way. (At least for simple cases.)
--
Grant Edwards grante Yow! There's a SALE on
at STRETCH SOCKS down at the
visi.com "7-11"!!