Hello,
I work with cortex M3-Microcontroller (LPC1768) and I want to know how much clock has a loop (for)
for(i=0;i<1;i++);
Thanks
Using Godbolt's online compiler explorer (set to gcc 9.2.1 although the clang output is similar enough) you will find with no optimization:
movs r3, #0 str r3, [r7, #4] .loop: ldr r3, [r7, #4] cmp r3, #0 bgt .loopexit ldr r3, [r7, #4] adds r3, r3, #1 str r3, [r7, #4] b .loop .loopexit: ...
This would loop once, so ...1 + 2 + 2 + 1 + 1 (branch not taken) + 2 + 1 + 2 + 3 (branch taken) + 2 + 1 + 3 (branch taken)In other words, 21 instruction cycles without optimization if I'm not missing anything (like byte alignment or anything else). Just glancing at the clang output, it looks like it has one more taken branch and would thus take 24 instruction cycles.Still, Andy has the right answer. It depends, and without an instruction that has an effect (like changing a pin output) the optimizer will erase the loop. Even with useful code in the loop, you'd probably need greater than 1 loop to see meaningful differences between the different optimization levels.
Don't forget that the operation of the "flash accelerator" may (or may not) make timing of instructions fetches somewhat non-deterministic :-(
If I want "deterministic" instruction execution, I'd go for a 6502 ;-)
Some chips offer the feature of turning off any "flash acceleration" in order to achieve determinism (but slower.)
And some have "tightly coupled RAM" memory where you can stick code to run at full rate...
But some SAMD21 cycle-counting delay code in Arduino went all wonky when ported to the SAMD51 (which has actual cache rather than just flash acceleration.) Even thought it was theoretically adjusted for the change in clock rate. github.com/.../71