Hardware Reference
In-Depth Information
bra
loop
; go back to the loop body
next …
The implementation of that statement “for i 5 n2 to n1 do S” can be modified from the
previous instruction sequence as follows:
n1
equ
xx
; start index (a nonnegative number)
n2
equ
yy
; end index (a positive number)
…
i
ds.b
1
…
movb
#n2,i
; initialize loop index i
loop
ldaa
i
; check loop index
cmpa
#n1
;
“
blt
next
; if all iterations have been performed, then exit
…
; perform the loop operation
…
;
“
dec
i
; decrement loop index
bra
loop
; go back to the loop body
next …
Like the for loop, the while-loop construct also checks the condition at the start of the
loop, and its implementation is similar with the following exceptions:
•
The condition to be checked may be an external event instead of a variable.
•
Updating the condition may be done by an external event such as an interrupt or
the change of an input signal.
Assume that the CPU will keep performing a certain operation as long as the variable
icount
is not zero and
icount
is decremented by the interrupt service routine (discussed in Chapter 6).
Then the following instruction sequence implements a loop using the while-loop construct:
N
equ
xx
…
icount
ds.b
1
…
movb
#N, icount
wloop
ldaa
#0
cmpa
icount
beq
next
…
; performed loop operation
…
bra
wloop
next …
The “repeat S until C” looping construction is used more often to perform some operation a
certain number of times. The following instruction sequence will perform a certain operation
N
times:
N
equ
xx
; define the constant N
…
…
ldy
#N
; use Y to hold loop count
loop
…
; perform operations
…
dbeq
Y,loop
; is loop count decremented to 0 yet?
Search WWH ::
Custom Search