Hardware Reference
In-Depth Information
2.6.4 Loop Primitive Instructions
A lot of the program loops are implemented by incrementing or decrementing a loop count.
The branch is taken when either the loop count is equal to zero or not equal to zero, depending
on the applications. The HCS12 provides a set of loop primitive instructions for implementing
this type of looping mechanism. These instructions test a counter value in a register or accu-
mulator (A, B, D, X, Y, or SP) for zero or nonzero value as a branch condition. There are predec-
rement, preincrement, and test-only versions of these instructions.
The range of the branch is from $80 (2128) to $7F (127) from the instruction immediately follow-
ing the loop primitive instruction. Table 2.4 shows a summary of the loop primitive instructions.
Mnemonic
Function
Equation or Operation
Decrement counter and branch if = 0
(counter = A, B, D, X, Y, or SP)
counter
(counter) − 1
If (counter) = 0, then branch;
else continue to next instruction.
counter
dbeq cntr, rel
Decrement counter and branch if
0
(counter) − 1
If (counter) 0, then branch;
else continue to next instruction.
counter (counter) + 1
If (counter) = 0, then branch;
else continue to next instruction.
counter
dbne cntr, rel
(counter = A, B, D, X, Y, or SP)
Increment counter and branch if = 0
(counter = A, B, D, X, Y, or SP)
ibeq cntr, rel
Increment counter and branch if
0
(counter) + 1
If (counter) 0, then branch;
else continue to next instruction.
If (counter) = 0, then branch;
else continue to next instruction.
ibne cntr, rel
(counter = A, B, D, X, Y, or SP)
Test counter and branch if = 0
(counter = A, B, D, X, Y, or SP)
tbeq cntr, rel
Test counter and branch if
0
(counter = A, B, D, X, Y, or SP)
If (counter)
0, then branch;
else continue to next instruction.
tbne cntr, rel
Note:
1. cntr is the loop counter and can be accumulator A, B, or D and register X, Y, or SP.
2. rel is the relative branch offset and is usually a label.
Table 2.4 Summary of loop primitive instructions
2.6.5 Implementation of Looping Constructs
The statement “for i 5 n1 to n2 do S” can be implemented as follows:
n1
equ
xx
; start index (a nonnegative number)
n2
equ
yy
; end index (a positive number)
i
ds.b
1
movb
#n1,i
; initialize loop index i
loop
ldaa
i
; check loop index
cmpa
#n2
;
bgt
next
; if all iterations have been performed, then exit
; perform the loop operation
;
inc
i
; increment loop index
 
Search WWH ::




Custom Search