Michael Coughlan
Page 36
Chapter 12 ■ advanCed data deClaration
MOVE MicroSecs TO EditedNum
DISPLAY EditedNum " MicroSecs"
MOVE MilliSecs TO EditedNum
DISPLAY EditedNum " MilliSecs"
MOVE Seconds TO EditedNum
DISPLAY EditedNum " Seconds"
STOP RUN.
The RENAMES Clause
As you have seen, the REDEFINES clause allows you to give a new data definition and name to an area of storage. The RENAMES clause lets you give a new name (or alias) to a data item or a collection of data items. This can be useful when you want to regroup a number of elementary data items in a record so that they can belong to the original as well as to the new group.
The RENAMES clause is used with the special level number 66. In the same way that condition names are
sometimes called level eighty-eights, RENAMES data items are sometimes called level sixty-sixes.
Because of the maintenance problems associated with RENAMES , it has largely fallen into disuse; and in some programming shops, it is banned. I include it here only for completeness.
RENAMES Syntax
The syntax metalanguage for the RENAMES clause is given in Figure 12-7. Identifier2 [THRU Identifier3] is the original area of storage, and Identifier1 is the new name that you can use to manipulate it.
Figure 12-7. RENAMES metalanguage
RENAMES Notes
The syntax diagram in Figure 12-7 is modified by the following semantic rules:
• The level number of Identifier2 and Identifier3 cannot be 77, 88, 01, or 66.
• Identifier2 and Identifier3 must not contain an OCCURS clause or be subordinate to a data
item that contains an OCCURS clause.
• No data item between Identifier2 and Identifier3 can contain an OCCURS clause.
• RENAMES entries must follow the last data-description entry of a record (can’t be in the middle
of a record description).
284
Chapter 12 ■ advanCed data deClaration
RENAMES Examples
Listing 12-2 contains a number of examples that show how to use the RENAMES clause.
Listing 12-2. RENAMES Examples
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing12-2
AUTHOR. Michael Coughlan.
* RENAMES clause examples
DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentRec.
02 StudentId PIC 9(8) VALUE 12345678.
02 GPA PIC 9V99 VALUE 3.25.
02 ForeName PIC X(6) VALUE "Matt".
02 SurName PIC X(8) VALUE "Cullen".
02 Gender PIC X VALUE "M".
02 PhoneNumber PIC X(14) VALUE "3536120228233".
66 PersonalInfo RENAMES ForeName THRU PhoneNumber.
66 CollegeInfo RENAMES StudentId THRU SurName.
66 StudentName RENAMES ForeName THRU SurName.
01 ContactInfo.
02 StudName.
03 StudForename PIC X(6).
03 StudSurname PIC X(8).
02 StudGender PIC X.
02 StudPhone PIC X(14).
66 MyPhone RENAMES StudPhone.
PROCEDURE DIVISION.
Begin.
DISPLAY "Example 1"
DISPLAY "All information = " StudentRec
DISPLAY "College info = " CollegeInfo
DISPLAY "Personal Info = " PersonalInfo
DISPLAY "Example 2"
DISPLAY "Combined names = " StudentName
MOVE PersonalInfo TO ContactInfo
DISPLAY "Example 3"
DISPLAY "Name is " StudName
DISPLAY "Gender is " StudGender
DISPLAY "Phone is " StudPhone
DISPLAY "Example 4"
DISPLAY "MyPhone is " MyPhone
STOP RUN.
285
Chapter 12 ■ advanCed data deClaration
Listing Notes
Listing 12-2 contains a number of RENAMES clause examples. The first example uses the RENAMES clause to rename sections of the StudentRec to allow the college and personal information parts of the record to be accessed separately.
No new data storage is created when this is done, but the existing storage is given new names. This example also shows that multiple, overlapping, RENAMES may be used.
The second example renames the elementary data items ForeName and SurName as StudentName so they can be
treated as a single item (that is, members of a group item). For the purpose of contrast, in the record ContactInfo I made these items subordinate to a group item.
The third example shows that the renamed data can be manipulated using the new name. I moved PersonalInfo
to ContactInfo and then displayed the individual fields in ContactInfo.
The final example shows that you can also use RENAMES to rename a single item.
The USAGE Clause
Computers store their data in the form of binary digits. Apart from cardinal numbers (positive integers), all other data stored in the computer’s memory uses some sort of formatting convention.
For instance, text data is stored using an encoding sequence like ASCII or EBCDIC. An encoding system is simply a convention that specifies that a particular set of bits represents a particular character. For instance, Figure 12-8
shows the bit configuration used to represent an uppercase A in the ASCII and EBCDIC encoding sequences.
Figure 12-8. Uppercase A in the ASCII and EBCDIC encoding sequences
Representation of Numeric Data
COBOL gives you a lot of control over how numeric data is held in memory. In COBOL, numeric data can be held as text digits (ASCII digits), as twos-complement binary numbers, or as decimal numbers (using binary-coded decimal [BCD]).
You use the USAGE clause to specify how a data item is to be stored in the computer’s memory. Every data item declared in a COBOL program has a USAGE clause—even when no explicit clause is specified. When there is no explicit USAGE clause, the default USAGE IS DISPLAY is applied. USAGE IS DISPLAY has been used in all the examples so far.
Disadvantage of USAGE DISPLAY
For text items, or for numeric items that will not be used in a computation (phone numbers, account numbers, and so son), the default of USAGE IS DISPLAY presents no problems. But the default usage is not the most efficient way to store data that will be used in a calculation.
When numeric items (PIC 9 items) have a usage of DISPLAY, they are stored as ASCII digits (see the ASCII digits 0–9 in the ASCII table in Figure 12-9).
286
Chapter 12 ■ advanCed data deClaration
Figure 12-9. Table of ASCII digits
Consider the program fragment in Example 12-6. Figure 12-10 shows what would happen if computations were done directly on numbers stored in this format. Because none of the data items have an explicit USAGE clause, they default to USAGE IS DISPLAY. This means the values in the variables Num1, Num2, and Num3 are stored as ASCII digits.
This in turn means the digit 4 in Num1 is encoded as 00110100 and the digit 1 in Num2 is encoded as 00110001. When these binary numbers are added together, the result, as shown in Figure 12-10, is the binary value 01100101, which is the ASCII code for the lowercase letter e.
Example 12-6. Arithmetic on Items Held as USAGE IS DISPLAY
Num1 PIC 9 VALUE 4.
Num2 PIC 9 VALUE 1.
Num3 PIC 9 VALUE ZERO.
: : : : : : : : :
ADD Num1, Num2 GIVING Num3.
Figure 12-10. Adding two ASCII digits gives the wrong result
When calculations are done with numeric data items using USAGE IS DISPLAY, the computer has to convert the numeric values to their binary equivalents before the calculation can be done. When the result has been computed, the computer must reconvert it to ASCII digits. Conversion to and from ASCII digits slows down computations.
For this reason, data that is heavily involved in computation is often declared
using one of the usages optimized for computation, such as USAGE IS COMPUTATIONAL.
287
Chapter 12 ■ advanCed data deClaration
Advantage of USAGE IS DISPLAY
Although it is computationally inefficient, there are a number of advantages to holding numeric data as text digits.
One obvious advantage is that USAGE DISPLAY items can be output to the computer screen by the DISPLAY verb without the need for conversion. Another advantage is portability. Files whose data is encoded as text can be processed without difficulty on different makes of computers or using other programming languages. In contrast, the chosen binary formats of some computers and the Big Endian/Little Endian byte order preference of others means that non-text files produced on one make of computer are often difficult to read on another make of computer or even using another programming language or utility program on the same computer.
StOraGe OF MULtIBYte NUMBerS
Some computers store numeric binary values using a byte order where the low-order byte of a number is stored at the lowest memory address, and the high-order byte at the highest address. this is known as the Little Endian byte order. For instance, a four-byte-long integer value would be stored as
Byte0 at BaseAddress+0
Byte1 at BaseAddress+1
Byte2 at BaseAddress+2
Byte3 at BaseAddress+3
in contrast, in Big Endian computers, the high-order byte of the number is stored at the lowest memory address, and the low-order byte at the highest address. For instance:
Byte3 at BaseAddress +0
Byte2 at BaseAddress +1
Byte1 at BaseAddress +2
Byte0 at BaseAddress +3
The advantages of the USAGE IS DISPLAY format and the speed of modern computers means that the USAGE
clause is an optimization that is only worth doing if the data item will be used in thousands of computations.
USAGE Clause Syntax
As you have seen, the default representation (USAGE IS DISPLAY) used by COBOL for numeric data items can
negatively impact the speed of computations. USAGE is used for purposes of optimization of both speed and storage. It allows you to control the way data items (normally numeric data items) are stored in memory. One important point to note is that because computers can be quite different under the skin (for instance, register size and Endian order), the COBOL standard leaves the actual implementation of the binary data items to the compiler implementer. This means a COMP item on one computer may not be exactly the same as a COMP item on another computer.
The metalanguage syntax diagram for the USAGE clause is given in Figure 12-11, and some example declarations are shown in Example 12-7.
288
Chapter 12 ■ advanCed data deClaration
Figure 12-11. The USAGE clause metalanguage
Example 12-7. Example USAGE Clause Declarations
01 Num1 PIC 9(5)V99 USAGE IS COMP.
01 Num2 PIC 99 USAGE IS PACKED-DECIMAL.
01 IdxItem USAGE IS INDEX.
01 FirstGroup COMP.
02 Item1 PIC 999.
02 Item2 PIC 9(4)V99.
02 Item3 PIC S9(5) COMP SYNC.
Notes
Here are some things to note:
• The USAGE clause may be used with any data description entry except those with level
numbers of 66 or 88.
• When the USAGE clause is declared for a group item, the usage specified is applied to
every item in the group. The group item itself is still treated as an alphanumeric data item
(see FirstGroup in Example 12-7).
• The USAGE clause of an elementary item cannot override the USAGE clause of the group to
which it is subordinate (for instance, in Example 12-7, the USAGE of Item3 is COMP because that
is the USAGE of FirstGroup).
• USAGE IS COMPUTATIONAL and COMP or BINARY are synonyms of one another.
• The USAGE IS INDEX clause is used to provide an optimized table subscript.
• Any item declared with USAGE IS INDEX can only appear in
•
A SEARCH or SET statement
•
A relation condition
•
The USING phrase of the PROCEDURE DIVISION
•
The USING phrase of the CALL statement
• The picture string of a COMP or PACKED-DECIMAL item can contain only the symbols 9, S, V, and/or P.
• The picture clause used for COMP and PACKED-DECIMAL items must be numeric.
289
Chapter 12 ■ advanCed data deClaration
■ Bug Alert Group items are always treated as alphanumeric, and this can cause problems when there are subordinate COMP items. For instance, suppose you defined a group of data items as follows:
01 SecondGroup.
02 NumItem1 PIC 9(3)V99 USAGE IS COMP.
02 NumItem2 PIC 99V99 USAGE IS COMP.
and then applied a statement such as MOVE ZEROS TO SecondGroup to it.
on the surface, it appears that the statement is moving the numeric value 0 to NumItem1 and NumItem2; but because SecondGroup is an alphanumeric item, what is actually moved into NumItem1 and NumItem2 is the aSCii digit “0”. When an attempt is made to use NumItem1 or NumItem2 in a calculation, the program will crash because these data items contain non-numeric data.
COMP Explanation
COMP items are held in memory as pure binary twos-complement numbers. You don’t have to understand how
twos-complement numbers work or how they are stored, but you must understand the storage requirements for fields described as COMP. The storage requirements are shown in Table 12-1. For instance, the declaration 01 TotalCount PIC 9(7) USAGE IS COMP
requires a LongWord (4 bytes) of storage.
Table 12-1. Storage Requirements of COMP Data Items
Number of Digits
Storage Required
PIC 9(1 to 4)
1 Word (2 bytes)
PIC 9(5 to 9)
1 LongWord (4 bytes)
PIC 9(10 to 18)
1 QuadWord (8 bytes)
PACKED-DECIMAL Explanation
Data items declared as PACKED-DECIMAL are held in binary-coded decimal (BCD) form. Instead of representing the value as a single binary number, the binary value of each digit is held in a nibble (half a byte). The sign is held in a separate nibble in the least significant position of the item (see Figure 12-12).
290
Chapter 12 ■ advanCed data deClaration
Figure 12-12. Memory representation of BCD numbers
The SYNCHRONIZED Clause
The SYNCHRONIZED clause is sometimes used with USAGE IS COMP or USAGE IS INDEX items. It is used to optimize speed of processing, but it does so at the expense of increased storage requirements.
Many computer memories are organized in such a way that there are natural addressing boundaries, such as word boundaries. If no special action is taken, some data items in memory may straddle these boundaries. This may cause processing overhead because the CPU may need two fetch cycles to retrieve the data from memory. The SYNCHRONIZED
clause is used to explicitly align COMP and INDEX items along their natural word boundaries. Without SYNCHRONIZED, data items are aligned on byte boundaries. The metalanguage for SYNCHRONIZED is given in Figure 12-13.
Figure 12-13. Metalanguage for the SYNCHRONIZED clause
The effect of the SYNCHRONIZED clause is implementation dependent. You need to read your implementer manual to see how it works on your computer (in some cases it may have no effect). To illustrate how SYNCHRONIZED works in general, assume that a COBOL program is running on a word-oriented computer where the CPU fetches data from memory a word at a time. Suppose the program performs a calculation on the number stored in the variable TwoBytes (as shown in Figure 12-14). Because of the way the data items have been declared, the number stored in TwoBytes straddles a word boundary. In
order to use the number, the CPU has to execute two fetch cycles: one to get the first part of the number in Word2 and the second to get the second part of the number in Word3. This double fetch slows down calculations.
Figure 12-14. With no SYNCHRONIZED clause, numbers may straddle word boundaries
When the SYNCHRONIZED clause is used, as shown in Figure 12-15, TwoBytes is aligned along the word boundary, so the CPU only has to do one fetch cycle to retrieve the number from memory. This speeds up processing, but at the expense of wasting some storage (the second byte of Word2 is no longer used).
291
Chapter 12 ■ advanCed data deClaration
Figure 12-15. With the SYNCHRONIZED, clause numbers are aligned along word boundaries
Nonstandard USAGE Extensions
The USAGE clause is one of the areas where many implementers have introduced extensions to the COBOL standard. It is not uncommon to see COMP-1, COMP-2, COMP-3, COMP-4, COMP-5, and POINTER usage items in programs written using these extensions.
Even though COMP-1 and COMP-2 are extensions to the COBOL standard, many implementers seem to use
identical representations for these usages. Comp-1 is usually defined as a single-precision, floating-point number, adhering to the IEEE specification for such numbers (Real or float in typed languages). Comp-2 is usually defined as an IEEE double-precision, floating-point number (LongReal or double in typed languages). COMP-3 items are usually defined as BCD numbers. The official introduction of PACKED-DECIMAL in the ANS 85 version of COBOL has made this extension unnecessary.
Decimal Arithmetic
One of COBOL’s strengths, and one of its main claims to fitness for writing business and enterprise applications, is its native support for fixed-point decimal arithmetic. Until the problems associated with floating-point arithmetic are pointed out to them, most programmers are not even aware that there is a problem. In the Java community, this is such a problem that even now articles and forum posts are still produced warning of the dangers of using float or double for currency calculations.