RPG Tutorial: Example Program 5 - Calling Assembler subroutine from RPG main

  1  H   
  2  H*
  4  H*
  5  FSTMASTR IPE F 250 250  7KI     2 DISK14       S
  6  FROSTER  O   F4800  48            DISK14       S
  7  FREPORT  O   F 132 132           LPRINTER
  8  LREPORT    101 6012
  9  ISTMASTR AA  01   1 CA
  10 I       OR   02   1NCA
  11 I                                        2   8 IDENT
  12 I                                        9  43 NAME
  13 I                                      136 1410CN1N            11
  14 I                                      136 141 CN1
  15 I                                      145 1500CN2N            12
  16 I                                      145 150 CN2
  17 I                                      154 1590CN3N            13
  18 I                                      154 159 CN3
  19 I                                      163 1680CN4N            14
  20 I                                      163 168 CN4
  21 I                                      172 1770CN5N            15
  22 I                                      172 177 CN5
  23 I                                      181 1860CN6N            16
  24 I                                      181 186 CN6
  25 I                                      190 1950CN7N            17
  26 I                                      190 195 CN7
  27 I                                      199 2040CN8N            18
  28 I                                      199 204 CN8
  29 C*
  31 C*
  32 C   01                MOVE ' '       SNAME  35
  33 C   01                EXIT EX5SUB
  34 C                     RLABL          NAME   35
  35 C                     ULABL          SNAME  35
  36 C*
  38 C*
  39 C   01      STCNT     ADD  1         STCNT   30
  40 C   01N11   OUTCNT    ADD  1         OUTCNT  30
  41 C   01N12   OUTCNT    ADD  1         OUTCNT
  42 C   01N13   OUTCNT    ADD  1         OUTCNT
  43 C   01N14   OUTCNT    ADD  1         OUTCNT
  44 C   01N15   OUTCNT    ADD  1         OUTCNT
  45 C   01N16   OUTCNT    ADD  1         OUTCNT
  46 C   01N17   OUTCNT    ADD  1         OUTCNT
  47 C   01N18   OUTCNT    ADD  1         OUTCNT
  48 OROSTER  D        01N11
  49 O                         SNAME     48
  50 O                         IDENT     13
  51 O                         CN1        6
  52 O        D        01N12
  53 O                         SNAME     48
  54 O                         IDENT     13
  55 O                         CN2        6
  56 O        D        01N13
  57 O                         SNAME     48
  58 O                         IDENT     13
  59 O                         CN3        6
  60 O        D        01N14
  61 O                         SNAME     48
  62 O                         IDENT     13
  63 O                         CN4        6
  64 O        D        01N15
  65 O                         SNAME     48
  66 O                         IDENT     13
  67 O                         CN5        6
  68 O        D        01N16
  69 O                         SNAME     48
  70 O                         IDENT     13
  71 O                         CN6        6
  72 O        D        01N17
  73 O                         SNAME     48
  74 O                         IDENT     13
  75 O                         CN7        6
  76 O        D        01N18
  77 O                         SNAME     48
  78 O                         IDENT     13
  79 O                         CN8        6
  80 OREPORT  H  201   1P
  81 O                                   33 'RECORD EXTRACT'
  82 O                                   19 'COURSE ROSTER '
  83 O        T 32     LR
  84 O                         STCNT Z    7
  85 O                                   24 'STUDENT RECORDS '
  86 O                                   28 'READ'
  87 O        T  1     LR
  88 O                         OUTCNTZ    7
  89 O                                   23 'ROSTER RECORDS '
  90 O                                   30 'WRITTEN'

Statements 5-7 are the File Description Specifications for the student master ISAM input (STMASTR), the class roster output (ROSTER),  and a report output (REPORT) files.  Notice that the R that was present in Example 4 has been removed from column 28 of the specification for the ISAM file STMASTR since we are processing the file sequentially from beginning to end.  The output disk file, ROSTER, will be blocked and each block will contain 100 records.

Statements 9 and 10 defines the input records expected to be read from STMASTR.  

Statements 11-28 define the fields in the student master record.  Again I have defined each course number field twice, once as an alphanumeric field and again as a numeric field.  On the numeric fields, the Field Indicator Zero/Blank (columns 69-70) are utilized to turn on indicators 11 through 18 if the corresponding course number field is zero.  These indicators will be used during calculations and output.

The purpose of this program is to produce a file which will be used to produce class rolls listing all students enrolled in each class.  Since it is normal to place student's names on such a roll in alphabetical order by last name, it will be the function of the external subroutine to transform the student name field from the order - first initial last - to the order - last, first initial.

Statement 32 moves space to the field which will contain the sort name.  Statement 33 is the statement which actually passes control the external subroutine (which is written in Assembler).  Both of these statements are conditioned by indicator 01, which is turned on for each record read from the primary input file which contains an 'A' in the first position.

Statement 34 uses the RLABL operation to specify an RPG field (NAME) which will be referenced by the Assembler subroutine.  Statement 35 uses the ULABL operation to specify a field that is defined in the Assembler subroutine but is accessed by the RPG main program.  These two operations, RLABL and ULABL, modify the attributes of the storage locations associated with the names to allow the linkage editor to resolve references to fields by both the main RPG program and the Assembler subprogram.

Statements  39-47 simply maintain some counters to be printed on the report.  Statement 39, conditioned by indicator 01 being on, counts the number of active primary input records read.  Statements 40-47, conditioned by both indicator 01 being on and indicators 11 through 18 being off (corresponding to course number fields one through eight being non-zero), counts the number of records which will be written to the output file (ROSTER).

Statements 48, 52, 56, 60, 64, 68, 72 and 76 define the conditions under which fields are to be written to the output file.  Each specification contains indicator 01, which will be on for an active student master record.  Each of the eight statements includes a specification that the corresponding indicator 11 through 18 must not be on, which will be true if the corresponding course number field in the student record was not zeros.  Therefore, for each student record read in it will be possible that up to eight output records may be written.

Under each output specification statement listed above is the specification for the group of fields which will be written to the output record.  The SNAME field returned from the Assembler subroutine and the student identification number from the student master record is included in each record.  The third field for each record is the course number field (one through eight) corresponding to the indicator (11 through 18) which is used to condition the output.

The remainder of the program produces the output report which contains the count of records read and records written.

The compiler listing and the output report are available for viewing here:  Example 5 SYSOUT.

The jobstream, complete with JCL, RPG source, Assembler subroutine source, and an IEBPTPCH step to print the output file are available for download here:  Example 5 jobstream.  You will need to ensure that the volume serial numbers for the student master ISAM file and the output file in the jobstream are correct for your MVT system.  Note:  There are two jobstreams in this archive, one is for MVS and the other is for MVT.  The difference is that the MVS jobstream uses the ISAM-VSAM interface to access an VSAM cluster instead of creating a native ISAM dataset.

The Assembler subprogram is very limited in functionality and is only included to complete the example.  There are no tests for validity of the field passed to it from the RPG main program and use with any data other than the test data it is expecting may produce unexpected results.

Return to Site Home Page 

This page was last updated on January 17, 2015.