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; sethi %hi(Q), %l0
st %g0, [%l0 + %lo(Q)]
R = A; sethi %hi(A), %l0
ld [%l0 + %lo(A)], %l0
sethi %hi(R), %l1
st %l0, [%l1 + %lo(R)]
while ( R >= B ) Loop: sethi %hi(R), %l0
{ ld [%l0 + %lo(R)], %l0 // l0 = R
Q = Q + 1; sethi %hi(B), %l1
R = R - B; ld [%l1 + %lo(B)], %l1 // l1 = B
} cmp %l0, %l1
bl LoopExit
nop
sethi %hi(Q), %l0
ld [%l0 + %lo(Q)], %l1 // l1 = Q
add %l1, 1, %l1
st %l1, [%l0 + %lo(Q)]
sethi %hi(R), %l0
ld [%l0 + %lo(R)], %l1 // l1 = R
sethi %hi(B), %l2
ld [%l2 + %lo(B)], %l2 // l2 = B
sub %l1, %l2, %l1
st %l1, [%l0 + %lo(R)]
ba Loop
nop
LoopExit:
sethi %hi(Q), %l0 st %g0, [%l0 + %lo(Q)] sethi %hi(A), %l0 ld [%l0 + %lo(A)], %l0 sethi %hi(R), %l1 st %l0, [%l1 + %lo(R)] Loop: sethi %hi(R), %l0 ld [%l0 + %lo(R)], %l0 // l0 = R sethi %hi(B), %l1 ld [%l1 + %lo(B)], %l1 // l1 = B cmp %l0, %l1 bl LoopExit nop sethi %hi(Q), %l0 ld [%l0 + %lo(Q)], %l1 // l1 = Q add %l1, 1, %l1 st %l1, [%l0 + %lo(Q)] sethi %hi(R), %l0 ld [%l0 + %lo(R)], %l1 // l1 = R sethi %hi(B), %l2 ld [%l2 + %lo(B)], %l2 // l2 = B sub %l1, %l2, %l1 st %l1, [%l0 + %lo(R)] ba Loop nop LoopExit: |
sethi %hi(Q), %l0 st %g0, [%l0 + %lo(Q)] sethi %hi(A), %l0 ld [%l0 + %lo(A)], %l0 sethi %hi(R), %l1 st %l0, [%l1 + %lo(R)] Loop: sethi %hi(R), %l0 ld [%l0 + %lo(R)], %l0 // l0 = R sethi %hi(B), %l1 ld [%l1 + %lo(B)], %l1 // l1 = B cmp %l0, %l1 bl LoopExit nop sethi %hi(Q), %l0 ld [%l0 + %lo(Q)], %l1 // l1 = Q add %l1, 1, %l1 st %l1, [%l0 + %lo(Q)] sethi %hi(R), %l0 ld [%l0 + %lo(R)], %l1 // l1 = R sethi %hi(B), %l2 ld [%l2 + %lo(B)], %l2 // l2 = B sub %l1, %l2, %l1 ba Loop st %l1, [%l0 + %lo(R)] LoopExit: |
In other words:
|