Getting Started 51
Any number of instructions can be contained within the loop. Here is a
simple program that will obtain a number from the terminal, double it,
and then output the result. These steps are then repeated over and over.
♦INPUT A VALUE FROM THE TERMINAL, DOUBLE,
♦AND THEN OUTPUT THE RESULT
*
NEXT:
JSR
INDEC
OBTAIN AN INPUT VALUE
JSR
NEWLINE
ADD.L
DO, DO
DOUBLE THE VALUE
JSR
OUTDEC
OUTPUT THE NUMBER
JSR
NEWLINE
JMP
NEXT
If you actually run this program, you may have to stop your computer
manually and reboot your operating system (the exact procedures to
follow depend on your particular computer; consult your owner’s manual
for the details). Some systems allow you to abort a program and return to
the operating system by typing a “Control C” (character C typed while
also holding down the key labeled CTRL).
Another frequently used type of loop is the counting loop. This is
a loop that repeats a number of instructions a fixed number of times.
Unlike most 8-bit microprocessors, the 68000 provides a single instruction
to perform a counting loop. This is the test condition, decrement, and
branch instruction, with the peculiar mnemonic DBRA. (It is actually a
member of a family of instructions with the mnemonics DBcc, where
the characters cc are replaced by the appropriate characters to select the
particular instruction desired.) The DBRA instruction has the following
format:
DBRA Dn,<label>
It works somewhat like a JMP instruction except that it uses the value
contained in the data register as a loop counter. It does this by first
subtracting 1 from the current value in Dn and then checking to see
if the result is equal to —1. If the updated value contained in Dn is not
equal to —1, the DBRA instruction then performs like a JMP to the label
specified as the second operand. If the new value of Dn is —1, the next
sequential instruction is executed.
In order to use the DBRA instruction, the Dn register must first be
set up with the total number of times we wish to go through the loop
minus one. The instructions between the label of the DBRA and the
DBRA instruction itself will be executed. These instructions will always
be executed at least once even if Dn is initialized with —1. In fact,
Comments to this Manuals