ILE RPG, RPG IV OpCodes Definition

Opcodes in RPGLE

What are OpCodes?

To perform any action in RPG or RPG - ILE you need to provide an OpCode. The OpCode also written as Op-Code is actually a short form of Operation Code. Opcodes may be understood to be similar to the system commands within an RPG program. This means that an opcode tells the program what to do. Owing to the positional nature of RPGLE programs, we can specify an operation code at the 26th position only. Most of the opcodes operate on factor 2 (eg. Eval, Read, Chain etc). However, some opcodes require Factor 1 also(eg. PLIST KLIST etc.) and yet some other the result field (KFld, Parm etc.). Here we provide some common opcodes and there purpose.

RPGIV OpCode Action Performed
Eval Evaluates the expression in Factor 2.
SetLl Sets the cursor before any specifc record of a file in factor on the basis of Keylist specified in factor 1.
Read Read the next record in a File.
ReadE Reads the next record in a file with a keyfield.
Chain Equivalent to SetLl + ReadE
ReadP Read the previous record from the current cursor position.
ReadPE Read the previou record with key list.
CallP Calls a bound procedure. This procedure might be in the same or different module or in some service program.
Call Calls an external Program.
PList Defines a parameter list specified in factor 1.
Parm Declares the parameters of a parameter list.
KList Declares a keylist.
KFld Declares the ingredient keyfields of a keylist.
ExSr Calls subroutine specified in the Factor 2.
BegSr /EndSr Declares the beginning and end of a subroutine specified in factor 1 on 12th position.The subroutine name is optional with EndSr.

We will learn about these opcodes in detail as we read about them in coming chapters.


OpCode extenders
We can use an OpCode extender to trap error or to specify the program not to take a lock while reading a database file declared in update mode. The Opcode extender is also used to test a date. We can not specify more than one opcode extender at a time on an opcode except with the D. Some common examples of opcodes are described below. The common opcode extenders are N, E, H and D.

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++
C                   Eval(H)   W@Sum = 2 + 5.68      

C                   Read(E)   File1              

C                   Read(N)   File2              

C                   Update(E) RCD001             

C     *ISO          Test(DE)                W@ABC
  • The first example uses the extender H to half adjust the result. If the W@Sum is 5P 1 then its value will become 7.7 after the code is executed.
  • The second example traps any error occurred while reading the File1 using the extender 'E'. Whether error occured or not during the read operation, can be verified by the %Error built in function. In case of error this return *On or '1'.
  • The Read(N) opcode uses the extender N to 'read without lock'. When a file has been declared in update mode, the read operation results in a lock on File2. It means the record will not be available for any other program for updation unless you unlock it. If you are not doing this read for any updation, it is advised to use the 'N' extender to read without lock. This extender can be used with all other read operation like CHAIN(N), READE(N), READPE(N), READP(E) etc.
  • The Update(E) traps error occurred during updation.
  • The Test(DE) tests whether w@ABC contains a valid *ISO date or not. In case of error the %Error function return *On or '1'.

The example mentioned above are the basic examples only. You can test whether an extender is allowed with some specific opcode or not by entering the extender next to the opcode in SEU. In case the extender is not valid with the opcode the SEU will throw an error.