Unformatted text preview: is second code sequence executes the loop body and evaluates the control expression exactly the same way as the original version and has the same code size, but it executes one fewer (untaken) branch per iteration, so the second branch has been removed from the loop overhead. An even more efficient code sequence can be produced by the compiler:
.. BEQ LOOP . . TEST .. BNE EXIT ; EXIT ; ; ; LOOP evaluate expression skip loop if necessary loop body evaluate expression The saving here is that one fewer branch is executed each time the complete 'while' structure is encountered (assuming that the body is executed at least once). This is a modest gain and costs extra instructions, so it is worthwhile only if performance matters much more than code size. do..while loops The conceptual arrangement of a 'do..while' loop is similar to the improved 'while' loop above, but without the initial branch since the loop body is executed before the test (and is therefore always executed at least once):
LOOP . . ; loop body ; evaluate expression BNE LOOP EXIT Functions and procedures 175 6.8 Functions and procedures
Good programming practice requires that large programs are broken down into components that are small enough to be thoroughly tested; a large, monolithic program is too complex to test fully and is likely to have 'bugs' in hidden corners that do not emerge early enough in the program's life to be fixed before the program is shipped to users. Each small software component should perform a specified operation using a well-defined interface. How it performs this operation should be of no significance to the rest of the program (this is the principle of abstraction; see Section 6.1 on page 152). Furthermore, the full program should be designed as a hierarchy of components, not simply a flat list. A typical hierarchy is illustrated in Figure 6.12. The top of the hierarchy is the program called main. The remaining hierarchy is fairly informal; lower-level routines may be shared by higher-level routines, calls ma...
View Full Document
- Spring '09