HP 3000 III Series Manual page 173

Table of Contents

Advertisement

Machine Instructions and Stack Operations
half of the same instruction (DEL) discards the remainder word by
decrementing S as shown at (H).
To save the result, the STOR Q-6
(line 13) first copies the TOS into the location reserved for the
procedure result,
formerly occupied
by a 0,
as
shown at
(I).
Then it
is possible
to exit from
the procedure.
The EXIT in-
struction
(line 14)
restores Q to its initial setting,
and the
"2"
included
with
the
instr uction
causes S to
move back two
locations past the stack marker.
As shown at
(J),
this leaves
the
result, 2, in the location reserved for QUOTIENT (now on the
TOS).
The EXIT returns
program control to line 19
which causes
the
content for QUOTIENT to be stored in the location for
ANSw~R
in the global data area.
This produces the final result shown at
(K).
Finally (line 20),
a procedure call to the
sys tem returns
control back to the system.
4-20.
Re~ursion
This example demonstr a tes
the stack principles involved in a re-
cursive procedure.
A recursive procedure is one which calls it-
self one or more times during execution.
The form of the
source
language program
for this example
(figure 4-18) is nearly iden-
tical to that of
the preceding example in figure 4-16.
The pro-
cedure simply
computes N1
(N factor ial),
where N is the formal
parameter.
The procedure will be called with an actual parameter
of 4 so that computation of 41 will be: 1 x 2 x 3 x 4
=
24.
This problem
consists of
repetitively multiplying
the previous
product by a parameter which is incremented by one on each
repe-
tition.
To provide a starting point (initial previous product),
the value 1 is automatically given.
The procedure is designed to
perform this multiplication
sequence by repetitively calling it-
self after it has been called once by the main program.
Thus for
any N,
the procedure will be called N+l times.
In this example
there will
be one
call by the
main program and
four recursive
calls.
Figure 4-18 lists the source and machine
language forms
of a program block to solve this problem.- Since the source lang-
uage program is similar to the preceding example,
it need not be
discussed at
this point.
The machine
language form
has been
slightly changed to more closely resemble an actual program list-
ing.
Some assumed PB-relative addresses are given for
each in-
struction,
beginning at
address 00114.
The assumption is that
this program
block is embedded in a larger main program.
(Note
that the
assigned STT entry for this
procedure is assumed to be
026 and that the global assignment for
Y
is DB+15.)
The starting
point for execution is at address 00130.
Figure 4-19 illustrates the program in flowchart form.
Box 1 in
the diagram calls the procedure (boxes 2 through 9), box 10 saves
the result,
and then control reverts to the main program
at box
11.
The procedure consists of two phases.
The call phase begins
when the procedure is called by the program and is repeated
four
times.
In this phase,
N
values are
placed on the
stack along
with a
space for intermediate answers.
The N values
are decre-
mented to
zero and then the exit phase begins.
This phase suc-
cessively multiplies
an accumulating
product by
each of
the
N
4-43

Advertisement

Table of Contents
loading

Table of Contents