HP 3000 III Series Manual page 176

Table of Contents

Advertisement

Machine Instructions and Stack Operations
4-21. MAIN PROGRAM CALL. As in previous examples,the main program
has already reserved global space for the final answer (Y) before
the procedure is callede
When the call is
given,
the ZERO,NOP
instruction
at address 130 reserves space for
the procedure re-
sult, FACTORIAL. (Compare stacks (a), figure 4-20 and (Z), figure
4-21.
This is the first stack addition due to calling
the
pro-
cedure.
Next, the actual parameter 4 is loaded on (B), and then
the PCAL instruction is issued.
This
causes
the
first
stack
marker
to be loaded (C).
This marker differs from the ones that
follow in that is contains return information to the ovter
block
which
called the present procedure (i.e., the return P word is a
P-relative address for return to the caller in the
code
segment
and
Delta
Q
points back to the
Q
value that the caller was using
earlier in the stack). Now, Sand Q are both pointing at the last
word of the first marker for this procedure.
4-22. TEST FOR ZERO. At addresses 114 and 115 (stack (D) and (E),
figure 4-20),
the procedure parameter is
first tested for zero.
This is done by
copying it onto the top of the stack
(LOAD Q-4)
and a CMFI
0 instruction.
This instruction sets
the condition
code according to comparison results and deletes the tested
word
(E).
Sin ce the
fir s t te s t i s
no n- z e r
0
(
i . e.,
4),
the bra nch
instruction at
line 116 transfers control to address 121
(i .e.,
P+ 4).
This test and branch will be repeated in each of the fol-
lowing recursion loops until the parameter has become zero.
4-23. FIRST RECURSIVE CALL.
The branch to address 121 causes the
procedure to call itself.
As usual, the first action of the call
is to load the
procedure parameters onto the
stack.
The para-
meters
in this case are the variable FACTORIAL and a decremented
form of
the original
passed parameter.
Thus the ZERO, NOP in-
struction
reserves
a location
for FACTORIAL
(F),
figure 4-20
strictly for use by this recursion (i.e., distinct from the final
FACTORIAL location reserved at (A).
Then (G and H), the new par-
ameter, is obta ined by' cop ying the preced ing val ue to the
top of
the
stack (LOAD Q-4) and decrementing with a SUBI 1 instruction.
Afte r load ing parameters for the new call,
another PCAL instr uc-
tion is issued.
This causes a new stack marker (I),
figure 4-20
and, via the Segment Transfer Table, control is transferred
back
to the
starting point of the procedure
at address 114.
The new
stack marker gives as its return P value the address
immediately
following the
PCAL which is 125.
(This will be important to re-
member when
the exit sequence is discussed.)
Also,
the Delta Q
value is 6 since the previous Delta
Q was six locations back.
4-24. SUCCESSIVE RECURSIONS.
Next, all the previously
des.cr ibed
steps
are
repeated,
beginning
with paragraph 4-22.
Since the
parameter is 3 on the second recursion, the branch to address 121
again occurs.
The first actions, again, are to reserve
a
loca-
tion
for this recursion's answer
(J),
figure 4-20 and to load a
decremented parameter value of 2 (K) and (L).
After
this,
the
procedure
call back to the beginning is made again which results
in another stack marker (M) that is identical
to
the
one
gen-
erated
on
the first recursion.
The third and fourth recursions
repeat the entire process again, loading parameters of
1
and
0
4-46

Advertisement

Table of Contents
loading

Table of Contents