SETA instruction

The SETA instruction assigns an arithmetic value to a SETA symbol. You can specify a single value or an arithmetic expression from which the assembler computes the value to assign.

You can change the values assigned to an arithmetic or SETA symbol. This lets you use SETA symbols as counters, indexes, or for other repeated computations that require varying values.

Read syntax diagramSkip visual syntax diagram>>-variable_symbol--SETA--expression---------------------------><

variable_symbol
is a variable symbol.

A global variable symbol in the name field must have been previously declared as a SETA symbol in a GBLA instruction. Local SETA symbols need not be declared in a LCLA instruction. The assembler considers any undeclared variable symbol found in the name field of a SETA instruction as a local SET symbol. The variable symbol is assigned a type attribute value of N.

expression
is an arithmetic expression evaluated as a signed 32-bit arithmetic value that is assigned to the SETA symbol in the name field. The minimum and maximum allowable values of the expression are -231 and +231-1, respectively.

Figure 40 defines an arithmetic expression.

Figure 40. Defining arithmetic (SETA) expressions
*-----------*
|           |
|Arithmetic |
|Expression |
|           |
*-----*-----*
      |
      V Can be any of
      *------------*-----------*--------------------*----------*
      V            V           V                    V          V
*-----*-----* *----*----* *----*--------------* *---*----*  ---*----*
|Arithmetic | | (Arith. | | Arithmetic-Valued | |+Arith. | |-Arith. |
|Term       | |  Exp.)  | | Built-in Function | |^  Exp. | |^  Exp. |
*-----*-----* *---------* *-------------------* *+-------* *+-------*
      |                                          *----*-----*
      V Can be any of                                 |
      *-------------*-------------*-------------*    unary operators
      V             V             V             V
*-----*-----* *-----*-----* *-----*-----* *-----*-----*
|           | |Predefined | | Self-     | |           |
| Variable  | |Absolute   | | Defining  | | Attribute |
| Symbol    | |Ordinary   | | Term      | | Reference |
|           | |Symbol     | |           | |           |     Operators Allowed
*-----------* *-----------* *-----------* *-----*-----*
                                                |           Unary:  + Positive
                                                |                   - Negative
                                                | Can
                                                V only be   Binary: + Addition
                                          *-----*-----*             - Subtraction
                                          | Length    |             * Multiplication
                                          | Scale     |             / Division
                                          | Integer   |
                                          | Count     |
Arith. Exp. = Arithmetic Expression       | Number    |
                                          | Defined   |
                                          *-----------*

Table 58 shows the variable symbols that are allowed as terms in an arithmetic expression.

Table 58. Variable symbols allowed as terms in arithmetic expressions
Variable symbol Restrictions Example Valid value
SETA None --- ---
SETB None --- ---
SETC Value must evaluate to an unsigned binary, hexadecimal or decimal self-defining term 123 123
Symbolic parameters Value must be a self-defining term
&PARAM
 
&SUBLIST(3)
X'A1'
 
C'Z'
&SYSLIST(n)
 
&SYSLIST(n,m)
Corresponding operand or sublist entry must be a self-defining term
&SYSLIST(3)
 
&SYSLIST(3,2)
24
 
B'101'
&SYSDATC
&SYSM_HSEV
&SYSM_SEV
&SYSNDX
&SYSNEST
&SYSOPT_DBCS
&SYSOPT_RENT
&SYSOPT_XOBJECT
&SYSSTMT
None --- ---

The following example shows a SETA statement with a valid self-defining term in its operand field:

&ASYM1   SETA  C'D'           &ASYM1 has value 196 (C'D')

The second statement in the following example is valid because in the two positions in the SETA operand where a term is required (either side of the + sign), the assembler finds a valid self-defining term:

&CSYM2   SETC  'C''A'''       &CSYM2 has value C'A'
&ASYM3   SETA  &CSYM2+&CSYM2  &ASYM3 has value 386 (C'A' + C'A')

A SET statement is not rescanned by the assembler to see whether substitutions might affect the originally-determined syntax. The original syntax of the self-defining term must be correct. Therefore the assembler does not construct a self-defining term in a SETA statement. The third statement of the next example shows this:

&CSYM3   SETC  '3'            &CSYM has value 3 (C'3')
&ASYM3   SETA  &CSYM3         &ASYM has value 3
&ASYM4   SETA  C'&ASYM3'      Invalid self-defining term

In this example C'&ASYM3' is not a valid term.

Subscripted SETA symbols

The SETA symbol in the name field can be subscripted. If the same SETA symbol has not been previously declared in a GBLA or LCLA instruction with an allowable dimension, then the symbol is implicitly declared as a local SETA array variable.

The assembler assigns the value of the expression in the operand field to the position in the declared array given by the value of the subscript. The subscript expression must not be 0 or have a negative value.

Arithmetic (SETA) expressions

Table 59 shows how arithmetic expressions can be used.

Table 59. Use of arithmetic expressions
Used in Used as Example
SETA instruction Operand &A1 SETA &A1+2
AIF or SETB instruction Term in arithmetic relation AIF (&A*10 GT 30).A
Subscripted SET symbols Subscript &ASYM(&A+10-&C)
Substring notation Subscript 'STRING'(&A*2,&A-1)
Sublist notation Subscript
Given sublist (A,B,C,D)
named &PARAM,
if &A=1 then &PARAM(&A+1)=B
&SYSLIST Subscript
&SYSLIST(&M+1,&N-2)
&SYSLIST(N'&SYSLIST)
SETC instruction Character string in operand
Given &C  SETC  '5-10*&A'  1 
if &A=10 then
&C=5-10*10  2 
Given &D  SETC  '5-10*&A'  1 
if &A=-10 then
&D=5-10*10 3 
Built-in functions Operand &VAR SETA (NOT &OP1) &VAR SETA BYTE(64)

When an arithmetic expression is used in the operand field of a SETC instruction (see  1  in Table 59), the assembler assigns the character value representing the arithmetic expression to the SETC symbol, after substituting values (see  2  in Table 59) into any variable symbols. It does not evaluate the arithmetic expression. The mathematical sign (+ or -) is not included in the substituted value of a variable symbol (see  3  in Table 59), and any insignificant leading zeros are removed.

Here are the built-in functions for arithmetic (SETA) expressions:

AND
Format: Logical-expression
Operands: Arithmetic
Output: (aexpr1 AND aexpr2) provides an arithmetic result where each bit position in the result is set to 1 if the corresponding bit positions in both operands contain 1, otherwise, the result bit is set to 0.

Example

After the following statements &VAR contains the arithmetic value +2.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 AND &OP2)
B2A
Format: Function-invocation
Operands: Character
Output: B2A('bitstring') converts a character string argument containing '0' and '1' characters to an arithmetic value.

The result of the B2A function is the same as

&value  SETA  B'bitstring'

except that null strings are allowed by B2A but not by SETA.

Examples

B2A('')              has value 0
B2A('0000000101')    has value 5
B2A('11111111111111111111111111111110')    has value -2
C2A
Format: Function-invocation
Operands: Character
Output: C2A('charstring') converts a character string of zero to four characters to a binary arithmetic value having the same bit pattern.

The result of C2A is the same as would be obtained from

&value  SETA C'charstring'

except that C2A gives a zero result for null strings, and does not pair apostrophes or ampersands before conversion.

Example

C2A('')              has value 0
C2A('+')             has value 78
C2A('1')             has value 241
C2A('0000')          has value -252645136
D2A
Format: Function-invocation
Operands: Character
Output: D2A('decstring') converts a character string argument containing an optional leading plus or minus sign followed by decimal digits to an arithmetic value. Error conditions are detected if

Null argument strings return zero.

The result of the D2A function is the same as

&value  SETA  decstring

except that SETA does not allow leading plus or minus signs.

Examples

D2A('')              indicates an error condition
D2A('000')           has value 0
D2A('10')            has value 10
D2A('+100')          has value 100
D2A('-5')            has value -5
DCLEN
Format: Function-invocation
Operands: Character
Output: DCLEN('cexpr') returns the length of its argument string after pairs of apostrophes and ampersands have been internally replaced by single occurrences. No change is made to the argument. Such pairing occurs only once; that is, three successive occurrences of an apostrophe or ampersand will result in two occurrences, not one.

Examples

DCLEN('')          has value 0 (null string)
DCLEN('''')        has value 1 (argument is a single apostrophe)
DCLEN('''''')      has value 2 (argument is two apostrophes)
DCLEN('&&')        has value 1 (argument is two ampersands)
DCLEN('a''''b')    has value 3 (DCVAL string would be "a'b")
DCLEN('a''''b&&c') has value 5 (DCVAL string would be "a'b&c")
DCLEN('&&&''''''') has value 4 (DCVAL string would be "&&''")

Note: DCLEN is similar to DCVAL, except that DCLEN returns only the length of the result, not the paired string.

FIND
Format: Logical-expression, function-invocation
Operands: Character
Output: ('string1' FIND 'string2') or FIND('string1','string2') finds the first match of any character from operand2 within operand1. The value returned by FIND indicates the position where the match occurs. FIND returns 0 if no match occurs or if either operand is a null string.

Examples

After the following statements &VAR contains the arithmetic value 3.

Name      Operation      Operand

&OP1      SETC           'abcdef'
&OP2      SETC           'cde'
&VAR      SETA           ('&OP1' FIND '&OP2')

In the above example the character c in &OP2 is the first character found in &OP1. Consider the following example where the character c, in &OP1, has been replaced with the character g.

Name      Operation      Operand

&OP1      SETC           'abcdef'
&OP2      SETC           'gde'
&VAR      SETA           ('&OP1' FIND '&OP2')

&VAR contains the arithmetic value 4. The character d in &OP2 is the first character found in &OP1.

In the following example, the ordering of the characters in the second operand is changed to egd.

Name      Operation      Operand

&OP1      SETC           'abcdef'
&OP2      SETC           'egd'
&VAR      SETA           FIND('&OP1','&OP2')

&VAR still contains the arithmetic value 4. Because FIND is looking for a single character from the character string, the order of the characters in the second operand string is irrelevant.

INDEX
Format: Logical-expression, function-invocation
Operands: Character
Output: INDEX('cexpr1','cexpr2') or ('cexpr1' INDEX 'cexpr2') locates the first occurrence of the second argument within the first argument, and returns the position of the match. A zero value is returned if:

Examples

INDEX('ABC','B')     has value 2
INDEX('ABC','D')     has value 0
ISBIN
Format: Function-invocation
Operands: Character
Output: ISBIN('cexpr') determines the validity of cexpr, a string of 1 to 32 characters, as the nominal value of a binary self-defining term usable in a SETA expression. If valid, ISBIN returns 1; otherwise, it returns zero. The argument string may not be null.

Example

ISBIN('10101')          returns 1
ISBIN('101010101010101010101010101010101') returns 0 (excess digits)
ISBIN('12121')          returns 0 (non-binary digits)
ISBIN('')               indicates an error condition
ISDEC
Format: Function-invocation
Operands: Character
Output: ISDEC('cexpr') determines the validity of cexpr, a string of 1 to 10 characters, as the nominal value of a decimal self-defining term usable in a SETA expression. If valid, ISDEC returns 1; otherwise, it returns zero. The argument string may not be null.

Example

ISDEC('12345678')       returns 1
ISDEC('+25')            returns 0 (non-decimal character)
ISDEC('2147483648')     returns 0 (value too large)
ISDEC('00000000005')    returns 0 (too many characters)
ISDEC('')               indicates an error condition
ISHEX
Format: Function-invocation
Operands: Character
Output: ISHEX('cexpr') determines the validity of cexpr, a string of 1 to 8 characters, as the nominal value of a hexadecimal self-defining term usable in a SETA expression. If valid, ISHEX returns 1; otherwise, it returns zero. The argument string may not be null.

Example

ISHEX('ab34CD9F')       returns 1
ISHEX('abcdEFGH')       returns 0 (non-hexadecimal digits)
ISHEX('123456789')      returns 0 (too many characters)
ISHEX('')               indicates an error condition
ISSYM
Format: Function-invocation
Operands: Character
Output: ISSYM('cexpr') determines the validity of cexpr, a string of 1 to 63 characters, for use as an ordinary symbol. If valid, ISSYM returns 1; otherwise, it returns zero. The argument string may not be null.

Examples

ISSYM('Abcd_1234')      returns 1
ISSYM('_Abcd1234')      returns 1
ISSYM('##@$_')          returns 1
ISSYM('1234_Abcd')      returns 0 (invalid initial character)
ISSYM('')               indicates an error condition
NOT
Format: Logical-expression
Operands: Arithmetic
Output: (NOT aexp) provides the ones complement of the value contained or evaluated in the operand.

Example

After the following statements &VAR contains the arithmetic value -11.

Name      Operation      Operand

&OP1      SETA           10
&VAR      SETA           (NOT &OP1)
OR
Format: Logical-expression
Operands: Arithmetic
Output: Each bit position in the result is set to 1 if the corresponding bit positions in one or both operands contains a 1, otherwise the result bit is set to 0.

Example

After the following statements &VAR contains the arithmetic value +10.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 OR &OP2)
SLA
Format: Logical-expression
Operands: Arithmetic
Output: The 31-bit numeric part of the signed first operand is shifted left the number of bits specified in the rightmost six bits of the second operand. The sign of the first operand remains unchanged. Zeros are used to fill the vacated bit positions on the right.

Example

After the following statements &VAR contains the arithmetic value +8.

Name      Operation      Operand

&OP1      SETA           2
&OP2      SETA           2
&VAR      SETA           (&OP1 SLA &OP2)
SLL
Format: Logical-expression
Operands: Arithmetic
Output: (aexp1 SLL aexp2) shifts the 32-bit first operand left the number of bits specified in the rightmost six bits of the second operand. Bits shifted out of bit position 0 are lost. Zeros are used to fill the vacated bit positions on the right.

Example

After the following statements &VAR contains the arithmetic value +40.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 SLL &OP2)
SRA
Format: Logical-expression
Operands: Arithmetic
Output: The 31-bit numeric part of the signed first operand is shifted right the number of bits specified in the rightmost six bits of the second operand. The sign of the first operand remains unchanged. Bits shifted out of bit position 31 are lost. Bits equal to the sign are used to fill the vacated bit positions on the left.

Examples

After the following statements &VAR contains the arithmetic value +2.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 SRA &OP2)

After the following statements &VAR contains the arithmetic value -1.

Name      Operation      Operand

&OP1      SETA           -344
&OP2      SETA           40
&VAR      SETA           (&OP1 SRA &OP2)

Compare this result with the result in the second example under SRL below.

SRL
Format: Logical-expression
Operands: Arithmetic
Output: The 32-bit first operand is shifted right the number of bits specified in the rightmost six bits of the second operand. Bits shifted out of bit position 31 are lost. Zeros are used to fill the vacated bit positions on the left.

Examples

After the following statements &VAR contains the arithmetic value +2.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 SRL &OP2)

After the following statements &VAR contains the arithmetic value 0.

Name      Operation      Operand

&OP1      SETA           -344
&OP2      SETA           40
&VAR      SETA           (&OP1 SRL &OP2)
X2A
Format: Function-invocation
Operands: Character
Output: X2A('hexstring') converts a character string argument containing hexadecimal digits to an arithmetic value.

The result of the X2A function is the same as

&value  SETA  X'hexstring'

except that null strings are allowed by X2A but not by SETA.

Examples

X2A('00000101')      has value 257
X2A('C1')            has value 193
X2A('')              has value 0
X2A('FFFFFFF0')      has value -16
XOR
Format: Logical-expression
Operands: Arithmetic
Output: Each bit position in the result is set to 1 if the corresponding bit positions in the two operands are unlike, otherwise the result bit is set to 0.

Example After the following statements &VAR contains the arithmetic value +8.

Name      Operation      Operand

&OP1      SETA           10
&OP2      SETA           2
&VAR      SETA           (&OP1 XOR &OP2)
Rules for coding arithmetic expressions

The following is a summary of coding rules for arithmetic expressions:

  1. Unary (operating on one value) operators and binary (operating on two values) operators are allowed in arithmetic expressions.
  2. An arithmetic expression can have one or more unary operators preceding any term in the expression or at the beginning of the expression. The unary operators are + (positive) and - (negative).
  3. The binary operators that can be used to combine the terms of an expression are + (addition), - (subtraction), * (multiplication), and / (division).
  4. An arithmetic expression must not begin with a binary operator, and it must not contain two binary operators in succession.
  5. An arithmetic-valued function is a term.
  6. An arithmetic expression must not contain two terms in succession.
  7. An arithmetic expression must not contain a decimal point. For example, 123.456 is not a valid arithmetic term, but 123 is.
  8. An arithmetic expression must not contain spaces between an operator and a term, nor between two successive operators except for built-in functions using the "logical-expression format" described at Logical (SETB) expressions.
  9. Ordinary symbols specified in arithmetic expressions must be defined before the arithmetic expression is encountered, and must have an absolute value.
  10. An arithmetic expression can contain up to 24 unary and binary operators, and is limited to 255 levels of parentheses. The parentheses required for sublist notation, substring notation, and subscript notation count toward this limit.

    An arithmetic expression must not contain two terms in succession; however, any term may be preceded by up to 24 unary operators. +&A*-&B is a valid operand for a SETA instruction. The expression &FIELD+- is invalid because it has no final term.

Evaluation of arithmetic expressions

The assembler evaluates arithmetic expressions during conditional assembly processing as follows:

  1. It evaluates each arithmetic term.
  2. It carries out arithmetic operations from left to right. However,
    1. It carries out unary operations before binary operations.
    2. It carries out the binary operations of multiplication and division before the binary operations of addition and subtraction.
    3. It carries out the binary operations of addition and subtraction before the bitwise logical operations.
    4. It carries out the bitwise logical operations before shift operations.
  3. In division, it gives an integer result; any fractional portion is dropped. Division by zero gives a 0 result.
  4. In parenthesized arithmetic expressions, the assembler evaluates the innermost expressions first, and then considers them as arithmetic terms in the next outer level of expressions. It continues this process until the outermost expression is evaluated.
  5. The computed result, including intermediate values, must lie in the range -231 through +231-1. (Note that if the value -231 is substituted in a SETC expression, its magnitude, 2147483648, is invalid if subsequently substituted in a SETA expression.)
SETC variables in arithmetic expressions

The assembler permits a SETC variable to be used as a term in an arithmetic expression if the character string value of the variable is a self-defining term. The value represented by the string is assigned to the arithmetic term. A null string is treated as zero.

Examples:

         LCLC            &C(5)
&C(1)    SETC            'B''101'''
&C(2)    SETC            'C''A'''
&C(3)    SETC            '23'
&A       SETA            &C(1)+&C(2)-&C(3)

In evaluating the arithmetic expression in the fifth statement, the first term, &C(1), is assigned the binary value 101 (decimal 5). To that is added the value represented by the EBCDIC character A (hexadecimal C1, which corresponds to decimal 193). Then the value represented by the third term &C(3) is subtracted, and the value of &A becomes 5+193-23=175.

This feature lets you associate numeric values with EBCDIC or hexadecimal characters to be used in such applications as indexing, code conversion, translation, and sorting.

Assume that &X is a character string with the value ABC.

&I       SETC            'C'''.'&X'(1,1).''''
&VAL     SETA            &TRANS(&I)

The first statement sets &I to C'A'. The second statement extracts the 193rd element of &TRANS (C'A' = X'C1' = 193).

The following code converts a hexadecimal value in &H into a decimal value in &VAL:

&X       SETC            'X''&H'''
&VAL     SETA            &X

The following code converts the double-byte character Da into a decimal value in &VAL. &VAL can then be used to find an alternative code in a subscripted SETC variable:

&DA      SETC            'G''<Da>'''
&VAL     SETA            &DA

Although you can use a predefined absolute symbol as an operand in a SETA expression, you cannot substitute a SETC variable whose value is the same as the symbol. For example:

ABS     EQU    5
&ABS    SETA   ABS           &ABS has value 5
&CABS   SETC   'ABS'         &CABS has value 'ABS'
&ABS    SETA   &CABS         invalid usage
DBCS assembler option

The G-type self-defining term is valid only if the DBCS assembler option is specified.

Using SETA symbols

The arithmetic value assigned to a SETA symbol is substituted for the SETA symbol when it is used in an arithmetic expression. If the SETA symbol is not used in an arithmetic expression, the arithmetic value is converted to a character string containing its absolute value, with leading zeros removed. If the value is 0, it is converted to a single 0.

Example:

          MACRO
 &NAME    MOVE            &TO,&FROM
          LCLA            &A,&B,&C,&D
 &A       SETA            10                  Statement 1
 &B       SETA            12                  Statement 2
 &C       SETA            &A-&B               Statement 3
 &D       SETA            &A+&C               Statement 4
 &NAME    ST              2,SAVEAREA
          L               2,&FROM&C           Statement 5
          ST              2,&TO&D             Statement 6
          L               2,SAVEAREA
          MEND
 -------------------------------------------------------------------
 HERE     MOVE            FIELDA,FIELDB
 -------------------------------------------------------------------
+HERE ST             2,SAVEAREA
+    L               2,FIELDB2
+    ST              2,FIELDA8
+    L               2,SAVEAREA

Statements 1 and 2 assign the arithmetic values +10 and +12, respectively, to the SETA symbols &A and &B. Therefore, statement 3 assigns the SETA symbol &C the arithmetic value -2. When &C is used in statement 5, the arithmetic value -2 is converted to the character 2. When &C is used in statement 4, however, the arithmetic value -2 is used. Therefore, &D is assigned the arithmetic value +8. When &D is used in statement 6, the arithmetic value +8 is converted to the character 8.

The following example shows how the value assigned to a SETA symbol may be changed in a macro definition.

          MACRO
 &NAME    MOVE            &TO,&FROM
          LCLA            &A
 &A       SETA            5                   Statement 1
 &NAME    ST              2,SAVEAREA
          L               2,&FROM&A           Statement 2
 &A       SETA            8                   Statement 3
          ST              2,&TO&A             Statement 4
          L               2,SAVEAREA
          MEND
 -------------------------------------------------------------------
 HERE     MOVE            FIELDA,FIELDB
 -------------------------------------------------------------------
+HERE ST             2,SAVEAREA
+    L               2,FIELDB5
+    ST              2,FIELDA8
+    L               2,SAVEAREA

Statement 1 assigns the arithmetic value +5 to SETA symbol &A. In statement 2, &A is converted to the character 5. Statement 3 assigns the arithmetic value +8 to &A. In statement 4, therefore, &A is converted to the character 8, instead of 5.

A SETA symbol may be used with a symbolic parameter to refer to an operand in an operand sublist. If a SETA symbol is used for this purpose, it must have been assigned a positive value.

Any expression that may be used in the operand field of a SETA instruction may be used to refer to an operand in an operand sublist. Sublists are described in Sublists in operands.

The following macro definition adds the last operand in an operand sublist to the first operand in an operand sublist and stores the result at the first operand. A sample macro instruction and generated statements follow the macro definition.

          MACRO
          ADDX            &NUMBER,&REG        Statement 1
          LCLA            &LAST
 &LAST    SETA            N'&NUMBER           Statement 2
          L               &REG,&NUMBER(1)
          A               &REG,&NUMBER(&LAST) Statement 3
          ST              &REG,&NUMBER(1)
          MEND
 -------------------------------------------------------------------
          ADDX            (A,B,C,D,E),3       Statement 4
 -------------------------------------------------------------------
+    L               3,A
+    A               3,E
+    ST              3,A

&NUMBER is the first symbolic parameter in the operand field of the prototype statement (statement 1). The corresponding characters (A,B,C,D,E) of the macro instruction (statement 4) are a sublist. Statement 2 assigns to &LAST the arithmetic value +5, which is equal to the number of operands in the sublist. Therefore, in statement 3, &NUMBER(&LAST) is replaced by the fifth operand of the sublist.


[ Top of Page | Previous Page | Next Page | Contents | Index ]