8 on page 176 to the pc after scaling it to a word

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 'false' and {..} is the body of the loop which is executed a number of times determined by the control structure. Often the body of a loop is very simple, which puts the onus on the compiler to minimize the overhead of the control structure. Each loop must have at least one branch instruction, but any more would be wasteful. for loops A typical 'for' loop uses the control expressions to manage an index: for (i=0; i<10; i++) {a[i] = 0} The first control expression is executed once before the loop begins, the second is tested each time the loop is entered and controls whether the loop is executed, and the third is executed at the end of each pass through the loop to prepare for the next pass. This loop could be compiled as: (This example illustrates the optimization technique of strength reduction, which means moving fixed operations outside the loop. The first two instructions are logically inside the loop in the C program, but have been moved outside since they are initializing registers to the same constants each time round the loop.) This code may be improved by omitting the conditional branch to EXIT and applying the opposite condition to the following instructions, causing the program to fall through rather than branch round them when the terminating condition is met. However, it can be further improved by moving the test to the bottom of the loop (which is possible here since the initialization and test are with constants, so the compiler can be sure that the loop will be executed at least once). 174 Architectural Support for High-Level Languages while loops A 'while' loop has a simpler structure and generally controls loops where the number of iterations is not a constant or clearly denned by a variable at run-time. The standard conceptual arrangement of a 'while' loop is as follows: LOOP .. BEQ .. B ; evaluate expression EXIT ; loop body LOOP EXIT The two branches appear to be necessary since the code must allow for the case where the body is not executed at all. A little reordering produces more efficient code: LOOP TEST EXIT B . . .. BNE TEST ; loop body ; evaluate expression LOOP Th...
View Full Document

Ask a homework question - tutors are online