Friday, November 18, 2011

Clauses in COBOL


PICTURE Clause

Describes the attributes of variable.

Numeric
9 (Digit), V (Implied decimal point), S (Sign)
Numeric Edited
+ (Plus Sign), -  (Minus Sign), CR DB (Credit Debit Sign)
. (Period), b (Blank), ‘,’(comma), 0 (Zero), / (Slash)
BLANK WHEN ZERO (Insert blank when data value is 0),
Z (ZERO suppression), * (ASTERISK), $(Currency Sign)
Non Numeric
A (alphabet), B (Blank insertion Character), X(Alpha numeric), G(DBCS)
Exclusive sets
  1. + - CR DB
  2. V  ‘.’
  3. $ + - Z * (But $ Can appear as first place and * as floating. $***.**)

DBCS (Double Byte Character Set) is used in the applications that support large character sets. 16 bits are used for one character. Ex: Japanese language applications.

Refreshing Basics

Byte.                8 Bits is one byte. By default, every character is stored in one byte.
Half word.       16 bits or 2 bytes is one half word. (MVS)
Full word.        32 bits or 4 bytes is one full word. (MVS)
Double word.   64 bits or 8 bytes is one double word. (MVS)

Usage Clause

DISPLAY        Default. Number of bytes required equals to the size of the data item. 
COMP            Binary representation of data item.
PIC clause can contain S and 9 only.
S9(01) – S9(04)       Half word.
S9(05) – S9(09)       Full word.
S9(10) - S9(18)       Double word.
Most significant bit is ON if the number is negative.
COMP-1         Single word floating point item. PIC Clause should not be specified.
COMP-2         Double word floating-point item. PIC Clause should not be specified.
COMP-3         Packed Decimal representation. Two digits are stored in each byte.      
                   Last nibble is for sign. (F for unsigned positive, C for signed positive  
                     and D for signed negative)
                   Formula for Bytes: Integer ((n/2) + 1)) => n is number of 9s.
INDEX           It is used for preserve the index value of an array. PIC Clause should
not be specified.

           

VALUE Clause

It is used for initializing data items in the working storage section. Value of item must not exceed picture size. It cannot be specified for the items whose size is variable.
Syntax:         VALUE IS literal.
                   VALUES ARE literal-1 THRU | THROUGH literal-2
                   VALUES ARE literal-1, literal-2 
Literal can be numeric without quotes OR non-numeric within quotes OR figurative constant.

SIGN Clause

Syntax          SIGN IS (LEADING) SEPARATE CHARACTER (TRAILING).
It is applicable when the picture string contain ‘S’. Default is TRAILING WITH NO SEPARATE CHARACTER. So ‘S’ doesn’t take any space. It is stored along with last digit.

+1=A +2=B +3=C +4=D +5=E +6=F +7=G +8=H +9=I
-0=}, -1= J, -2= K, -3=L, -4=M, -5=N, -6=O, -7=P, -8=Q, -9=R

Number
TRAILING SIGN (Default)
LEADING SIGN
LEADING SEPARATE.
-125
12N
J25
-125
+125
12E
A25
+125

SYNC Clause and Slack Bytes

SYNC clause is specified with COMP, COMP-1 and COMP-2 items. These items are expected to start at half/full/double word boundaries for faster address resolution. SYNC clause does this but it may introduce slack bytes (unused bytes) before the binary item.

01 WS-TEST.
          10 WS-VAR1 PIC X(02).
          10 WS-VAR2 PIC S9(6) COMP SYNC.

Assumes WS-TEST starts at relative location 0 in the memory, WS-VAR1 occupies zero and first byte. WS-VAR2 is expected to start at second byte. As the comp item in the example needs one word and it is coded with SYNC clause, it will start only at the next word boundary that is 4th byte. So this introduces two slack bytes between WS-VAR1 and WS-VAR2.

OCCURS Clause

OCCURS Clause is used to allocate physically contiguous memory locations to store the table values and access them with subscript or index. Detail explanation is given in Table Handling section.

No comments:

Post a Comment