Unit 7.8 -- Linking PASCAL and ASSEMBLER
Prog

Same as 7.7 --- Main program in PASCAL

PED

To  learn  how  to  interface a higher level  language  with
ASSEMBLER

CONCEPTS

One  can  have  a  PASCAL program call an ASSEMBLY  Language
program

Linkage conventions
  Must use IBM Standard Calling Sequence
  Register one points to a parameter control block
  One address for each value

Assembler program must be declared in PASCAL program.

PROCEDURE proc-name(var value:integer;....);FORTRAN;

ASSEMBLER program must have
proc-name CSECT at beginning

82.PAS

83.html
            
Unit 7.8 -- linking PASCAL to Assembly

It  is  often  necessary to link higher level  languages  to
Assembly  language.   This is usually  done  by  writing  an
assembly  language  subroutine.  The higher  level  language
program would then call this subroutine.

Often  a higher level language program would need to  access
things  at  a  bit  level.  On a PC, the ASSEMBLER  language
program  might diddle with a graphics screen  or  other  I/O
device.   Perhaps a certain calculation might  be  performed
very  often.   It  might be desired  to  code  most  of  the
application  in  PASCAL.  (Remember, that  a  single  PASCAL
statement  is  equivalent to several  ASSEMBLER  statements.
Since  the  time to debug a program is proportional  to  the
number  of  lines of code, we can get the same functionality
in less time if the code is written in PASCAL.)  However,  a
small section of code is executed very often--perhaps it  is
nested  in a loop.  Thus, a great deal of efficiency can  be
gained  at  little  programming cost by  recoding  this  one
function in ASSEMBLER.

This unit will teach you how to link PASCAL and ASSEMBLER on
the  mainframe.   I  would need to  teach  you  the  linkage
conventions  for  the IBM VSPASCAL compiler  and  Assembler.
These linkage conventions would include such information as:

1.        How the parameters are passed (address or value)--
          what register points to a control block, if any.

2.        For  arrays, particularly two-dimensional  arrays,
          how they are arranged

Every  computer  and  compiler would have  its  own  linkage
conventions.  By linkage conventions, I mean how  parameters
are  passed  and  how  registers and  other  information  is
stored.

In  the case of IBM Pascal and Assembler Language, there are
two things that we need to know:

A.        Use the IBM convention--what else would you expect
          from an IBM compiler

B.        The IBM Pascal passes its var and value parameters
          just  like  we learned in units 7.2  and  7.3.   A
          parameter control block is created.  There is  one
          word  for each item being passed.  If it's  a  var
          parameter,  then the program will put the  address
          of  the memory location in the parameter list.  If
          it's  a  value  parameter, then the actual  number
          will be in this word.

          The  address of this parameter control block  will
          be in register one on entry to this program.

Our  sample  program is our familiar Assembler program  that
takes  two  numbers as arguments and produces  a  third  one
containing  the sum.  Of course the first two arguments  are
value parameters and the the last is a var parameter.

Below  you will see the actual pascal code used in the  main
program.   In  this case, I took advantage of  the  relative
ease  in  reading and writing numbers to files.   Thus,  the
PASCAL  code  allows us to read and write the  program.   We
will  see  in  section  11  how  to  do  I/O  directly  from
Assembler.  (It's much harder in Assembler.)

Also observe the line that reads:

PROCEDURE BLAH(Q,R:INTEGER; VAR S:INTEGER); EXTERNAL;

This  declares the type of the arguments to BLAH.  It  tells
PASCAL to prepare a parameter control block containing three
words.   The first two will contain the values for Q and  R.
The   third  will  contain  the  address  of  the   variable
corresponding to S.

The  word  "EXTERNAL"  means that we  will  not  define  the
procedure  in this program but it will be externally  linked
in.   The  PASCAL compiler neither knows nor cares that  the
procedure  BLAH will in fact be in Assembler  language.   It
could have been in PASCAL, for all it cared.

The  Assembler  code is the same as we saw in  the  previous
unit.  However, we must use the IBM conventions since we are
linking with an IBM compiler.

If  you would like to try this on your own account, you  can
run  PASCAL  by a ten step manual procedure below.   If  you
have precisely one assembler program and one pascal program,
you can use my RUNPAS exec.

First, the long procedure

0)   Create a file of the form "name1 PASCAL"
     containing a PASCAL program to call our subroutine

1)   Type
     "LINKTO VSPASCAL"

     (this connects you up with the PASCAL disks.)

2)   Type
     VSPASCAL name1
     where  name1  is  the name of the file  containing  the
     PASCAL program

     (this  compiles  the PASCAL program into  a  TEXT  file
     that can be linked with an external procedure.)

3)   Type
     GLOBAL TXTLIB AMPLANG VSPASCAL

4)   Type
     GLOBAL LOADLIB AMPXLVEC PASRTLIB

5)   Type
     FILEDEF INPUT DISK name2 name2-extension
     (this  defines your input file--presumably  you  edited
     this beforehand)

6)   Type
     FILEDEF OUTPUT DISK name3 name3-extension
     (This defines a place for the output to go)

7)   Type
     LOAD name1 name4 (start
     where  name4  is the name of your assembler subroutine.
     Presumably you ran HASM on this before

8)   type
     type name3 name3-extension
     to see the output from your PASCAL program.

Steps 1, 3 and 4 only have to be done once per login.

Or  you  could  use  my RUNPASCAL procedure.   It  does  the
equivalent of the above eight steps.  Simply type,

RUNPAS  pasname ass-name file-in file-in-ext file-out  file-
out-ext

These six arguments are:

pasname  --  the name of a PASCAL program.  It will  have  a
full  name like pasname PASCAL.  The procedure will  compile
it as needed.

ass-name -- the name of Assembler program.  RUNPAS will  run
this through HASM for your convenience.

file-in file-ext is the name of a file containing any  input
for your program.  You must have created this with XEDIT  in
advanced.

file-out file-out-ext is the name of a file to contain  your
output.   RUNPAS will type it out on your terminal when  all
is finished for your convenience.