> Pasting in anything destroys line breaks for some reason,
That's what the "Insert Code" button is for in the edit title bar.
> so I've attached the disassembly in a text file.
Too long to read. Next time I'd suggest compiling it in both "Release" and "Debug" versions, comparing outputs and then posting a minimal set of differences, or at least commenting the disassembly with what you think is wrong.
> Sorry, I misunderstood your comment about the volatile before. Why would I have to declare the variable as volatile?
So that your code has a chance of working. Had you added "volatile" your code would have started working. Then you could have compared the disassembly and found out what changed.
> I understand the use of the volatile keyword in the context of letting
> a compiler know the value can be changed from somewhere else,
That only applies in "classic computing" where you are coding in a tightly controlled environment, running under an operating system, and where your code is basically doing "arithmetic in memory". It doesn't work all that well there either - see Wikipedia references below.
If you're writing to a hardware port and sequentially write the values "1, 2, 3, 4" to the same location, an optimising compiler is going to decide that sequence has the same effect as just writing "4", and so that's what it is likely to do. But the hardware may have required the four writes.
The other thing optimising compilers do is to change the ORDER of operations. So your code may be writing to hardware registers in a specific order, say the sequence "A=STOP; B=value; A=START;" and the compiler may do the write to "B" and then the two to "A" (if it does both of them at all).
In embedded programming, "volatile" means "do exactly what I typed, in that order".
Here's a better example, and also an example of using the "Insert Code" button:
http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Volatiles.html#index-volatile-access-2953
volatile int *src=somevalue;*src;
Imagine what an optimising compiler would do to the above with and without "volatile".
I suggest you read the Wikipedia page on this (http://en.wikipedia.org/wiki/Volatile_variable, and click on the "Disassembly Comparison - Show"), and especially follow through to the link to "volatile-considered-harmful.txt" and "volatile-almost-useless-for-multi-threaded-programming".
Tom