# IBM RPG II

RPG II is a very early and popular version of the IBM RPG programming language.
It was developed in the late 1960s and designed to work on the smallest IBM systems of the time such as the IBM 1130, IBM System/3, System/32, System/34, System/36. It was also available for the System/370, The Singer System 10, Sperry Univac 90/25, 90/30, 90/40, System 80, 1100 mainframe series and the Wang VS Series. ICL produced versions for its ICL 2903 system and for VME/K; and Burroughs Corporation produced an RPG II compiler with database extensions for its B1700 series of computers. Digital Equipment Corporation had their own implementation named VAX RPG II for VAX/VMS systems.
An enhanced version RPG III appeared in 1978.
It has a number of unusual features, including: an implied processing loop; and that it is a fixed-format programming language, so that programs are difficult to read without a special debugging template.

## Features

RPG II is a fixed-format programming language, which means that code must be placed in exact column locations in order to generate correct results. There are eight different specification types, and separate coding forms are used to write each, and a special debugging template used as an aid to read program printouts.
Every RPG II program executes within an implied loop, the program cycle, which applies the program successively to every record of a file - this is documented via a "Logic Flow" diagram on the debugging template. Each record (individual punched card) would be compared to each line in the program, which would act upon the record, or not, based upon whether that line had an "indicator" turned "on" or "off" — from a set of logical variables numbered 01–99 for user-defined purposes, or other smaller sets based upon record, field, or report processing functions. Special variables such as UDATE, UYEAR, PAGE, and so forth, are filled when the program begins or when page overflow occurs, even though there is no explicit instruction for these activities. Total calculations and output are done at "total time," after the detail cycle when L1/LR has been set on by fixed logic.
The concept of RPG's program cycle fitted neatly with a cyclical machine that read cards, summarised their contents and prints a result, rather like a tabulating machine. The language was extended to handle other input and output devices and provides a fast and efficient method of programming.
Devices such as the workstation (WORKSTN), the keyboard (KEYBORD), or the console (CONSOLE) do not have a fixed number of records at the beginning of the job and therefore, in order to incorporate the fixed-logic RPG "Last Record" cycle, the LR indicator can be set on with a SETON instruction. LR cannot be set off.
RPG II did not evolve much from the 1977 implementation on the System/34 to 2000 when the Advanced/36 was discontinued from marketing. Changes that were made from the 1970s version included the IFEQ/IFNE/IFGT/IFGE/IFLT/IFLE and END grouping. Also, the call/parm to be able to call external subroutines. Another change was that for internal subroutines, you no longer had to put SR in columns 7 and 8 of the C (calculation) specs.
Third-party providers sold more than 200 different assembler subroutines that could be used by System/36 and Advanced/36 programmers to exceed RPG II limitations. Some of the limitations of RPG II on the System/3, 32, 34 and 36 including the Advanced/36 was the 64K limit and the number of files you could have in a program. So if you had a lot of programming lines or had large arrays, it was easy to exceed the 64,000 bytes of object code. However, RPG II running on the AS/400 and its follow on iseries and IBM i (those running OS/400 or i5/OS in what is called S/36E (execution environment)) those limits (the 64K bytes and the number of files) were either greatly expanded or removed.

## RPGII specifications

In the System/36 implementation of RPG II, there are eight different specification (spec) types, that is a fixed-format line of text, 80 characters (bytes) in length, derived from the original use of punched card input for earlier IBM systems, like the System/3. Like specifications were grouped together, and each type was required to be in a specific order: U, T, H, F, E, L, I, C AND O.
Specification codes appear in column 6 of an RPG-II specification:
| Specification code | Name |
| U | Auto Report spec is only required for Auto Report programs. |
| T | Telecommunications. This ninth specification was added later in 1977: |
| H | Header spec is at the top of the program and describes compiler options such as maximum compile size, whether the program is an MRT (Multiple Requestor Terminal) program, and what type of listing is generated when the program is compiled. The object name of the program created is located in columns 75–80; if a source does not have an H spec, the name RPGOBJ is used. |
| F | File spec(s) are next, and describes the files used in the program. Files may be disk files (DISK) or may be devices such as a printer (PRINTER), the workstation (WORKSTN), keyboard (KEYBORD), unformatted display (CRT or DISPLAY), or user-defined (SPECIAL). Record size, block size, overflow indicators, and external indicators are described. It is possible that an RPG program will not use any F specs. |
| E | Extension spec(s) are next, and describe arrays and tables, which may be prefetched from disk files (an Input table), drawn from constants placed at the end of the source between ** and /* symbols, or built from calculations. |
| L | Line Counter spec(s) are next, and if present, describe the form to be printed. It defines the number of lines in a page and the positions where printing begins and ends. |
| I | Input specs are next, and describe the data areas within files. RPG II permits redefinition of data areas so that a field named FLDA might occupy the same area as an array AR that contains 8 elements of 1 character each. Non-record areas such as data structures can be described. Depending on the values of the input record, indicators may be conditioned. |
| C | Calculation spec(s) are next. Total fields may be described and accumulated. Complex computations and string manipulations are possible. Indicators may be conditioned. |
| O | Output specifications, which describe the output record in terms of fields and output positions. |
Operation codes appear in columns 28–32 of an RPG-II calculation specification.
| Operation code | Name |
| ADD | Add |
| SUB | Subtract |
| MULT | Multiply |
| DIV | Divide |
| Z-ADD | Zero and Add |
| Z-SUB | Zero and Subtract |
| MVR | Move Remainder |
| MOVE | Move |
| MOVEL | Move Left |
| MHLZO | Move High to Low Zone |
| MLHZO | Move Low to High Zone |
| MLLZO | Move Low to Low Zone |
| MHHZO | Move High to High Zone |
| BITON | Bit On |
| BITOF | Bit Off |
| TESTB | Test Bit |
| GOTO | Go To |
| TAG | Tag (destination of a GOTO) |
| EXSR | Execute Subroutine |
| BEGSR | Begin Subroutine |
| ENDSR | End Subroutine |
| SETON | Set On |
| SETOF | Set Off |
| EXCPT | Exception |
| EXIT | Exit |
| RLABL | Redefine Label |
| ULABL | User Label |
| CALL$^1$ | Call |
| PARM$^1$ | Parameter |
| CHAIN | Chain |
| READ | Read |
| READE | Read Equal |
| READP | Read Previous |
| REDPE | Read Previous Equal |
| SETLL | Set Lower Limits |
| SET | Set |
| TIME | Time of day (000000-235959) |
| DSPLY | Display |
| SORTA | Sort Array |
| XFOOT | Crossfoot |
| MOVEA | Move Array |
| LOKUP | Lookup (find in array) |
* CALL/PARM was added to RPG II with Release 6.0 (also known as the VASP).
CHAIN retrieves the record in the indexed file named in Factor 2 that matches the exact key specified by the value in Factor 1.
SETLL causes the index pointer for the file named in Factor 2 to be positioned at the location specified by the value in Factor 1.
SORTA causes the named array to be sorted in place; that is, the elements appear in order.
Z-SUB calculates Factor 2 with opposite sign and moved to result field.
XFOOT causes an array to be summed and the result moved to result field.
MVR must follow a DIV operation. The integer remainder of the DIV operation is placed in the result field. MVR following the DIV operation for "56 divided by 3" would place the value 2 in the result field.

## Indicators

Indicators are either 'set' or not, they are "flags" or boolean data types.
* Generic, usually manually set, for custom usage within I, C and O specs of a program:
       01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
       20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
       40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
       60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
       80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
* Halt Indicators, manually set within the calculation C-specs, to terminate a program with a return code:
       H1 H2 H3 H4 H5 H6 H7 H8 H9
* Matching Indicators, assigned to input I-spec fields, automatically set when reading records from a file, to coordinate the hierarchy of and signal which fields (variables) within records from two or more files are synchronized, assuming a sorted order of the records within each file, the records being processed sequentially:
       M1 M2 M3 M4 M5 M6 M7 M8 M9, and MR which reflects that matching has occurred
* Control-Level Indicators, assigned to input I-spec fields, to coordinate the hierarchy of and signal which fields within a record from a file are grouped, assuming a sorted order of the records within a file, the records being processed sequentially:
       L0 L1 L2 L3 L4 L5 L6 L7 L8 L9
* Command Key Indicators, to signal to the program processing input from a workstation keyboard, equivalent to the function keys (F1-F12, and shift-F1-F12) on a PC keyboard, used instead of the Enter key:
       KA KB KC KD KE KF KG KH KI KJ KK KL KM KN KP KQ KR KS KT KU KV KW KX KY
       These indicators refer to command keys 1 to 24, in that order. Note that there is no KO indicator.
* First Cycle Indicator, set automatically upon output of the first page, mainly used for printer files, used similarly to an overflow indicator for controlling output like page breaks and headings:
       1P
* Overflow Indicators, tripped automatically processing the pagination limits of a printer output file, per each file's L-spec line counter, and may be set manually, too. Usually establishing page breaks and printing of headings. One indicator each, for up to 8 output files:
       OA OB OC OD OE OF OG OV
* Last Record Indicator, not to be confused with Control-Level indicators, terminates a program, to return control to the batch processing jobstream (script), set automatically when end-of-file is reached on a sequentially processed file, manually set to terminate a program that has no sequential process source (file):
       LR
* External Indicators, generic for custom usage, passed to the program from the jobstream control (script) batch file:
       U1 U2 U3 U4 U5 U6 U7 U8
Foldable sheets, templates, plastic coated against wear and staining, were available, summarizing the different specifications. Used for the visual inspection and verification of the fixed-format source code text when held against its printed output. They matched and aligned with the then standard spacing of the text, as drawn from the original usage of punched cards:
System/36 SEU templates :
| 3 | H | header |
| 5 | F | files |
| 6 | F | extensions |
| 7 | E | extensions |
| 8 | L | line counters |
| 10 | I | file |
| 11 | I | fields |
| 12 | C | calculation |
| 13 | O | file |
| 14 | O | fields |