We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hai All.... I am new to Embedded world...past 3 months I working in 8051 programming through keil C...I request all experts to guide "How To write a Optimized Code" (kindly post any links and book names)...Give me tips what are all topics to learn in Embedded system...what is the important things to be known for beginners.....
thank you, karthik
Counting down to zero is a very special case, since a lot of processors have native instructions to decrement and jump if non-zero. A very good instruction to have at the end of a loop to return up again for more iterations.
Good compilers tends to auto-detect when a loop can be rewritten - but that requiers that the code doesn't do too fancy stuff with the loop index.
It must be me, but I don't think I have ever seen a compiler warn about removing an empty loop - either because the loop hasn't been removed, or the compiler does not have such a warning or I have not pushed my luch by having an empty loop :)
I now the old Turbo C/.../Borland C++ did throw away empty loops but I think it did it without warning. Good practice is to write code where you are not relying on the compiler warnings to know that you are pushing your luck. Whenever the compiler has a select to do one of several options, write the code so it doesn't matter which option gets selected.
In the case with an empty loop, you do not really know if it will be fully remoed, or if it will be semi-optimized so it runs fast but does take time, or if it will go the slow way and write down the loop counter to memory and perform a memory reload. This makes it impossible to know how fast the loop will be and makes it extremely dangerous to use empty loops as delays. Having a volatile loop variable will inform the compiler that you want it to perform memory transfers for the loop variable.
I'm not sure if a compiler is allowed to deduce that a volatile variable on the stack and without the address taken will not produce any side effects (except load on CPU and memory subsystem) and if the compiler are then allowed to ignore the loop anyway.