while ( condition ) +---------->|
statement | |
| +--------------+ FALSE
| | condition |--------+
| +--------------+ |
| | |
| | TRUE |
| | |
| V |
| statement |
| | |
+-----------+ |
|
+<---------------+
|
V
LOOP:
+--> Evaluate "condition" (CMP)
| |
| V
| Branch on the FALSE outcome of "condition" to A ------------+
| | |
| | TRUE |
| | |
| V |
| Translate "statement" into assembler code |
| | |
| V |
+--------- BRA LOOP |
|
A: +<------------------------------------------------+
|
V
int A, B;
int Q, R;
(Computes: Q = A/B
R = A%B)
Q = 0; MOVE.L #0, Q
R = A; MOVE.L A, R
while ( R >= B ) Loop: MOVE.L R, D0
{ CMP.L B, D0
Q = Q + 1; BLT LoopExit
R = R - B;
} MOVE.L Q, D0
ADD.L #1, D0
MOVE.L D0, Q
MOVE.L R, D0
SUB.L B, D0
MOVE.L D0, R
BRA Loop
LoopExit:
The flow chart of the program segment:
|
The following assembler program does the same thing, just "faster". But since the focus of the course is understanding how the computer works, and not make it run faster, this program will not be discussed. If you're curious, take a look. Basically, don't use the memory if you don't have to: click here
while ( C )
{
s1;
s2;
...
}
|
give rise to an assembler program with the following structure:
WhileStart:
instructions to perform a compare
specified by the condition C
branch of FALSE of the condition C to label WhileEnd
instructions to perform s1
instructions to perform s2
....
bra WhileStart
WhileEnd:
|
|