On Thu, 2 Dec 2010, Ethan Dicks wrote:
I've done that "slowdown" trick in
embedded code and had it optimized
down to a simple assignment. I've found that rather than tweak the
compilier, tweaking the code this much...
for (i=0; i < 100; i++) {
a=i;
}
(i.e. - putting something active in the loop)
... was enough to leave the loop in and generate a few cycles for the
delay. If I'm in an environment where C and assembler are mixed (not
uncommon with microcontroller programming), a #asm/NOP construct
works, too.
Some optimizers are a little too smart for you, and WILL optimze that out,
;
or
a=i=100; /* IF a and/or i are used later in the code and are
read before the next time that they are assigned.*/
Some optimizers will even remove NOPs! requiring you to use the ALIGN
keyword instead of NOPs if word boundaries are going to matter.
WORD/DOUBLE boundaries was even useful (with NON-auto-optimized code) for
determining the word size on a machine, and/or as one of the steps in
determining processor. I always manually checked where buffers ended up
in the code, and rearranged variables to make sure that REP MOVSW was
dealing with aligned words (except in 5150 and 5160)
BTW, when Microsoft first attempted optimization in their C compiler (4.0?
5.0?), "VOLATILE" was NOT the only "excess optimization". It was
merely
the one that bit me first.
--
Grumpy Ol' Fred cisin at
xenosoft.com