
Data Division
Data Field Formats
Input and output fields can be in
-Characters
-Zoned decimals
-Packed decimals
-Binary format
-
Negative sign is represented by a hexadecimal D
-
Positive sign is represented by a hexadecimal F
Packed Decimal Format
-
Each byte of storage (except low order byte) can contain two decimal numbers
-
Each byte (except low order type) is divided into 4-bit digit portions
-
Low order byte contains one digit in the leftmost portion and a sign (+/-) in the rightmost portion
-
Numeric input fields (unless they are in data structures) are converted to packed decimal format for internal processing by the compiler.
-
Initial value = zeros
-
Arithmetic calculations processed more efficiently if data is in packed decimal format
-
Length can be upto 16 bytes long
-
Note - the length of a packed field can increase by one when it is unpacked in another program
-
Example
Number 216 will have the bit pattern
F 2 F 1 F 6
Number -216 will have the bit pattern
F 2 F 1 D 6
Zoned Decimal Format
-
Each byte of storage can contain one digit or one character
-
Any character or numeric field can be read in zoned decimal format
-
Each byte of storage is divided into a 4-bit zone portion and a 4 bit digit portion
-
Example
Number 216 will have the bit pattern
2 1 6 F
Number -216 will have the bit pattern
2 1 6 D
-
Sign of the decimal number is indicated by the zone portion of the low order byte
-
Each digit in a decimal number include a zone portion; however, only the low order zone portion serves as the sign
-
Leading and trailing signs can be used with zoned decimal formats only
-
Decimal points are allowed
-
Initial values is spaces
Binary Format
-
Contains the sign (+/-) in the leftmost bit of the field and the integer value in the remaining bits of the field
-
Positive nos. have zero and negative nos. have one in the sign bit
-
Each field must be either two or four bytes long
-
Since a length of four is assigned to a 2 byte binary field and a length of nine is assigned to a 4 byte binary field, the highest decimal value that can be assigned to a 2 byte binary field is 9999 and to a 4 byte binary field is 999999999
Data Structures
-
A data structure is an area defined in storage consisting of field layouts called subfields
-
Can be program described (blank in position 17 of data structure statement) or externally described (E in position of 17 of data structure statement)
-
Subfields within a data structure comply with subfield specifications
-
A data structure is considered to be a character field and is initialized to blanks. Numeric subfields must be initialized with numeric data before they are used in arithmetic or editing operations
-
Examples :
Program defined
I DSNAME DS
I P 6 70 DSFLD1
I P 8 110 DSFLD2
Externally defined
I DSNAME E DS
Data Structures can be used for the following purposes
-
Same internal areas can be defined multiple times using different data formats
-
Define a data structure and its subfields in the same way a record is defined
-
Define multiple occurrences of a set of data
-
Group non contiguous data into contiguous internal storage locations
1 *..1..+..2...+...3...+...4...+...5...+...6...+...7..
1 FilenameSqNORiPos1NCCPos2NCCos3NCC..........*
1 IFILEIN NS 01 1 CA 2 CB
1 ............................PFromTo++DField+lM1FrPlMnZr...*
1 I 3 18 PARTNO
1 I 19 19 NAME
1 I 30 40 PATNO
1 I 41 61 DR
1 IDsname...NODsExt-file+++....Occrlen+...............*
1 IPARTNO DS
1 I..............Ext-field+....PFromTo++DField+.......*
1 I 1 4 MFG
1 I 5 10 DRUG
1 I 11 13 STRNTH
1 I 14 160 COUNT
1 *..1..+..2...+...3...+...4...+...5...+...6...+...7...
1 IFilenameSqNORiPos1NCCPos2NCCPos3NC.........*
1 ITRANSACTNS 01 1 C1 2 C2
1 I ...........................PFromTo++DField+lM1FrPlMnZr..*
1 I 3 10 PARTNO
1 I 11 160 QTY
1 I 17 20 TYPE
1 I 21 21 CODE
1 I 22 25 LOCATN
1 IDsname....NODsExt-file+++....Occrlen+...............*
1 IPRTKEY DS
1 I..............Ext-field+ .....PFromTo++DField+......*
1 I 1 4 LOCATN
1 I 5 12 PARTNO
1 I 13 16 TYPE
There are three special data structures each with a specific purposes
-
A data area data structure (identified by a U in position 18)
-
A file information data structure (referred to by keyword INFDS on a file description specs. continuation line)
-
A program status data structure (defined by an S in position 18)
Rules for Specifying Data Structure Statements
-
Data structure name should be a symbolic name with a maximum of six characters
-
All entries for one data structure and its subfield must appear together
-
A compile time or run time array cannot be used in a data area data structure or in a multiple occurrence data structure
-
Length can be 1 to 9999 characters
-
A data structure name and a subfield name cannot be the same
-
Length is determined by the first specification in the program that defines a length in one of the preceding ways. Subsequent conflicting lengths are invalid
-
The length specified in the input field specs. of data structure name is an input field
-
The highest TO position of a subfield within a data structure if the data structure name is not an input field
-
The length specified in positions 48 through 51 of a data structure statement
-Multiple Occurrence Data Structure
-
Its definition is repeated in a program to form a series of data structures with identical formats
-
Number of occurrences specified in position 44 through 47 of the data structure statement
-
All its occurrences have the same attributes and can be referred to individually using OCUR verb
-
Not allowed for a data area, file information or program status data structure
E*
E* A compile-time array, ARC, is specified in the extension specifications. It has 70 entries. There are 10 entries in each record, and each array element is 6 positions long. The relative location of the alpha code in the array (for example, the 37th entry) sets the current occurrence of the data structure.
E*
E ARC 10 70 6 ARRAY of CODES
Data Area Data Structure
-
A data area is an output object used to communicate data such as variable values between programs within a job and between jobs
-
This indicates to the RPG/400 program that it should read in and lock the data area of the same name at program initialization and should write out and unlock the same data area at the end of the program
-
If position 7 through 12 of input specs are blank and position 18 contains U, RPG/400 uses a local data area. To provide a name for a LDA, use *NAMVAR DEFN operation with *LDA in factor 2 and the name in the result field.
I U DS
C *NAMVAR DEFN *LDA TEST
File Information Data Structure
-
Provides status information on the occurrence of file exception/error
-
Contains predefined subfields to deliver such information
-
Must be unique for each file
-
The following entries define and name a file information data structure on a file description specs. continuation line
-
Position Entry
-
6 F
7-52 blank (if information is on separate continuation line)
53 K (indicates a continuation line)
54-59 INFDS
60-65 name of file information data structure
IFILDS1 DS
I *FILE FIL1
I *RECORD REC1
I *OPCODE OP1
I *STATUS STS1
I *ROUTINE RTN1
IFILDS2 DS
I *FILE FIL2
I *RECORD REC2
I *OPCODE OP2
I *STATUS STS2
I *ROUTINE RTN2
Program Status Data Structure
-
Provides program exception/error information to the program
-
Identified by an S in position 18 of the data structure statement
-
Example of this error : Division by zero, invalid array index, sort by a negative number
-
I SDS
I *ROUTINE LOC
I *STATUS ERR
I *PARMS PARMS
I *PROGRAM NAME
C *PSSR BEGSR
C ERR COMP 102 20 DIV BY ZERO?
C 20 ADD 1 DIVSR
C 20 MOVE ‘*DETC’ RETURN 6
C N20 MOVE ‘*CANCL’ RETURN
C ENDSRRETURN
Constants
-
Literals and named constants are types of constants
-
Constants can be specified in factor1 or factor 2 of certain operations and in the constant field of output specifications
-
Used to specify initialization values for data structure subfields on the input specification
-
Literal - is a self-defining constant that can be referred to in a program. It can be a character, hexadecimal or numeric.
Character Literal
-
Rules for specifying character literal
-
Any combination of characters allowed, embedded blanks are valid
-
must be enclosed in apostrophes (‘)
-
‘ as part of literal is represented by two consecutive ‘
-
Cannot be used for arithmetic operations
Hexadecimal Literals
-
Rules for specifying Hexadecimal Literal
-
Takes the form X’x1x2………..xn’, which can only contain A-F, a-f, and 0-9
-
Literal coded between the ‘ must of even length
-
Each pair of characters defines a single byte
-
Each pair of characters defines a single byte
-
Are allowed any where that character literals are supported except as factor 2 of ENDSR and as edit words
-
Except when used in bit operations BITON, BITOF, and TESTB, a hexadecimal literal has the same meaning as the corresponding character literal.
Numeric Literals
-
Rules for specifying a numeric literal :
-
Any combination of digits 0-9, a decimal point can be included
-
=, - sign if present must be the left most character
-
Blanks cannot appear
-
Must not be enclosed within apostrophes (‘)
-
Are used in the same way as a numeric field, except that values cannot be assigned to numeric literals
Named Constants
-
A symbolic name assigned to a character or numeric constant
-
Are defined on Input specifications
-
The value of a named constant follows the rules specified for literals
Examples
Character constant
I ‘MICKEY’ C MOUSE
Numeric constant
I 123456789 C INTEGER
Hexadecimal constant
I X’010203’ C HEX1