Unit 7.2 -- Subroutine with Parameters
               SILVER 6.2

PROG

See the following program's comment lines.

PED

To learn how to pass parameters.

CONCEPTS

Parameters  are passed to a subroutine by having a  register
(regA)
point to a parameter control block (PCB).

PCB is collection of four-byte entries (these entries can be
                                        either addresses  or
numbers)

for  value parameters -> put number in appropriate  location
of PCB

for var parameters -> put address in appropriate location of
PCB

In main routine:

PCB   DS nF

(in this PCB,
   the first entry is located at...CB+0
       second entry................CB+4
       third entry.................CB+8
       fourth entry................CB+12
      .
      .
      .
       nth entry...................CB+(n-1)*4  )

To call Subroutine

if first parameter a value parameter
     L    1,argument 1
     ST   1,CB+0
if first parameter a var parameter
     LA   1,argument 1
     ST   1,CB+0
if second parameter a value parameter
     L    1,argument 2
     ST   1,CB+4
if second parameter a var parameter
     LA   1,argument 2
     ST   1,CB+4
if nth parameter a value parameter
     L    1,argument n
     ST   1,CB+(n-1)*4
if nth parameter a var parameter
     LA   1,argument n
     ST   1,CB+(n-1)*4

     LA   1,CB
     BAL  regA,subroutine

(regA- register used for return address)

In subroutine:

to retrieve value from value parameter

   L regB,nn(0,regA)

where nn = 0 for first parameter
           4 for second parameter
           8 for third parameter
          12 for fourth parameter
            .
            .
            .
     (n-1)*4 for nth parameter

To use a value from value parameter

   x regB,nn(0,regA)

where x is an instruction such as "A" "S" "C" which normally
takes a memory location

to store regC into var parameter

   L  regB,nn(0,regA)
   ST regC,0(0,regB)

(where nn is defined the same as above)

26.html

Unit 7.2 -- Subroutines with Parameters

Obviously, as in PASCAL, it is desired to make our  routines
work with different parameters.

In PASCAL, there are two types of parameters.  One are those
without  the "var"  These are "pass by value" parameters  in
the  computer science community.  Note that changes  to  the
parameter inside the subroutine doesn't change the argument.

The  other are those with the "var"  (These are known in the
computer  science community as "pass by reference.")   Here,
changes to the parameter inside the subroutine do affect the
argument.   That's  the whole idea by specifying  the  "var"
designator in PASCAL.

All our parameters are kept in a control block of some kind.
In  this  class, we will use the suffix "CB" for  a  control
block.   (Control block's show up a lot in  the  IBM  world,
there  are  DCB's,  data   control blocks,  DSCB,  data  set
control blocks, JCB's, job control blocks.)  A control block
is some kind of collection of related data.

We  will learn how to deal with both of these in this  unit.
Often,  we  will  use  the designator,  PCB,  for  Parameter
Control   Block,  a  collection  of  parameters,  for   some
subroutine.

Somewhere, space is allocated for the control block.  In the
part  of  the  code associated with the allocation,  we  can
refer  to  the  first  entry as xxxCB+0,  the  next  one  as
xxxxCB+4,  the  third as xxxxCB+8, the fourth as  xxxxCB+12.
(Note we use decimal here.)

In  the  main program, we can use these designators  to  put
values into the control block to be passed to the routine.

When  we  pass  the  control block to  our  subroutine,  the
subroutine will receive the address of the control block  in
a  register.   Thus, if it is desired to call  a  subroutine
with  several different sets of parameters, each set can  be
stored in a separately allocated block of storage.

Asssume, that the subroutine was passed its parameters in  a
control block.  It's address was put in "regA"

The  subroutine can pick the values out of the control block
by writing

L   regB,0(0,regA)

for the first parameter

L   regB,4(0,regA)

for the second parameter

L   regB,8(0,regA)

for the third parameter

L   regB,12(0,regA)

for the fourth parameter

and, so on...

Value  parameters are a little different.  Here, we  put  in
the  control block, THE ADDRESS of the storage location.  We
can then store into that address in the subroutine.

In the main program, we would write, LA reg,blah,

ST reg,PCB+nn

where  nn is 0,4,8,12, etc., depending upon the position  of
the
parameter value.

In  the subroutine, to put something into a given parameter,
we get the address--in the same manner as above.

However, we then use this as the address, in which to store.
That assigns a value into the var parameter.

This is given by the template in the transparencey,

L regB,nn(0,regA)

ST regC,0(0,regB)

nn is 0 for the first parameter, 4 for the second parameter,
8 for the third parameter, 12 for the third parameter, etc.

regA contains the address of the parameter control block.

regB is a temporary register which will contain the value of
the "var" variable being passed.

regC presumably contains the value to be stored there.   The
store will put the item in that value.

In  our  example,  we  again add three  different  pairs  of
numbers,  getting three different sums.  Here,  however,  we
specify, the  the two variables containing the items  to  be
summed  as  parameters. These are the first two  parameters.
The  third  parameter, a var parameter takes the address  of
the  place to put the SUM.  See the equivalent PASCAL  inthe
comment to the sample program.

We  initialize A and B to 1 and 2, respectively, in lines 20
to 23.

The  call statement in the PASCAL, line 10, is done in lines
25 to 33 of the code.

In lines 25 to 26, we store A into the first position of the
parameter control block.  In lines 27 to 28 we store B  into
the  second positon of the parameter control block.  In line
29 and 30, we store the ADDRESS of C into the third position
of  the parameter.  Note that the third argument of BLAH, S,
is  a  VAR parameter so we do the ADDRESS.  In line  31,  we
store the address of the PCB itself into register one.   The
subroutine expects the address of the PCB in this  register.
Line 33 is the actual call.

We initialize E to 6 in lines 35 to 36.

The  call statement in the PASCAL, line 11, is done in lines
38 to 47 of the code.

In lines 36 to 39, we store E into the first position of the
parameter control block. In lines 40 to 41 we store  C  into
the second positon of the parameter control block.

In  line 42 and 43, we store the ADDRESS of F into the third
position of the parameter.  Note that the third argument  of
BLAH, S, is a VAR parameter so we do the ADDRESS.
In  line  44,  we store the address of the PCB  itself  into
register one.  Line 47 is the actual call.

We initialize G to 7 in lines 49 to 50.

The  call statement in the PASCAL, line 13, is done in lines
52 to 61 of the code.

In lines 52 to 53, we store G into the first position of the
parameter control block.  In lines 54 to 55 we store F  into
the  second positon of the parameter control block. In  line
55 and 56, we store the ADDRESS of H into the third position
of  the parameter.  Note that the third argument of BLAH, S,
is  a  VAR parameter so we do the ADDRESS.  In line  48,  we
store the address of the PCB itself into register one.  Line
61 is the actual call.

Line 62 simply returns to the operating system.

Now, we inspect the subroutine itself.

It appears from lines 77 to 83.

It  fetches the first parameter, from 0 off register one, in
line  79.  This  is  loaded  into register  4,  a  temporary
register.

Then,  the  second  parameter, from 4 off  register  one  is
added, in line 82.

The  final store is done in two steps.  We load the  address
from  8  off  of register one.  Then, this is  used  as  the
address in which to deposit the sum.

The  "BR 5" at line 85 is the RETURN to get us back  to  our
main program.