While: Example 1 Our first example has a simple condition. In Pep/8

advertisement
While: Example 1
Our first example has a simple condition.
input(N);
while (N < 99)
{
output(N);
input(N);
}
In Pep/8 this might be
top:
skip:
deci
lda
cpa
brge
deco
deci
br
N,d
N,d
99,i
skip
N,d
N,d
top
; sets bits according to value of N
; check condition
; skip loop if N >= 99
; new value for N
; back to test loop condition
While: Example 2
Our second example has two relational and one logical operator.
input(N);
while (N > 6 && N <= 10)
{
sum += N;
input(N);
}
In Pep/8 this might be
top:
skip:
deci
lda
cpa
brle
cpa
brgt
adda
sta
br
N,d
N,d
6,i
skip
10,i
skip
sum,d
sum,d
top
;
;
;
;
;
sets bits according to value of N
check first part of condition
branch if N <= 6 – no need to check N<=10
note - no need to reload N
skip the loop if N > 10
; sum += N
While: Example 3
Another example (this time with an “or” connector) is
while (A > 2 || B < 7)
{
input(A,B);
output(A+B);
}
In Pep/8 this might be
top: lda
cpa
brgt
lda
cpa
brge
loop:deci
deci
lda
adda
sta
deco
br
skip:
A,d
2,i
loop
B,d
7,i
skip
A,d
B,d
A,d
B,d
-2,s
-2,s
top
; first clause is true so don’t need to check second
; if second clause is also false, don’t do loop
; get two numbers
; use stack for scratch space
; output sum of inputs
Download