Rate This Document
Findability
Accuracy
Completeness
Readability

Loop Structures

In addition to branch structures, loop structures are also basic and important structures in HLL, such as the for loop, while loop, and do-while loop. It is important to determine whether a loop structure meets the end condition. The assembly implementation of the loop structure is similar to that of the branch structure, as shown in the following table.

C Program Implementation

Assembly Implementation

for(i = 0; i < end; i++){

... ...

(loop body)

... ...

}

/* Assume the values of i and end are stored in registers name-defined as reg_i and reg_end. */

b LoopCond

LoopStart:

... ...

(loop body)

... ...

LoopCond:

cmp reg_i, reg_end

b.le LoopStart

while (i < end) {

... ...

(loop body)

... ...

}

do {

... ...

(loop body)

... ...

i++;

} while (i < end);

... ...

LoopStart:

... ...

(loop body)

... ...

LoopCond:

cmp reg_i, reg_end

b.le LoopStart

The key to the transformation from the branch structure to the loop structure lies in the condition judgment and loop body entrance determination. When one of the targets of the branch judgment jump is the loop body entrance, the loop structure can be implemented.

From the preceding implementation, the assembly conversion of the for loop structure is the same as that of the while loop structure. The only difference between the two structures and the do-while loop structure is whether to execute the loop body content once through the first jump. We are optimistic about the assumption that the loop condition is always met for the first time in the application. Therefore, the do-while loop structure is preferred in the actual assembly implementation because the jump judgment may bring different performance.