Mainframes 360
The one stop destination for System Z professionals

Saturday, December 19, 2009

COBOL Tutorial – USAGE Clause


Q. What is USAGE Clause? Could you cite an example, where one would employee USAGE Clause?

USAGE Clause signifies, how the computer internally stores or represents DATA. Memory space calculation is done by the computer, on looking at the USAGE Clause. Thus, USAGE Clause answers the question, How much storage space does a data-item occupy? 1 byte, 2bytes, 4 bytes,… well how much? Depending upon the type of data – whether alphabetic or numeric, you should choose a USAGE(internal representation) that suits your purpose.

USAGE

Purpose
DISPLAY When you want to work with characters, manipulate them, work on textual string data
COMPUTATIONAL More appropriate, when you want to perform arithmetic computations, specially on numbers – integers and fractional numbers

When there is no explicit USAGE clause, it defaults to USAGE IS DISPLAY.

Q. How do computers internally store data?

Computers store data in Binary digits 0 and 1. Thus, all the data you enter into the computer is represented as 0’s and 1’s. The computer is said to understand and speak binary language.

Text data such as ‘HELLO’, ‘I HOPE YOU ARE IN THE PINK OF HEALTH’,’QUASAR CHUNAWALA’ are stored using a collating sequence(ASCII or EBCDIC). The collating sequence assigns a unique code number to each character. For example, A is assigned ASCII code 65, B = 66, C = 67, and so on. Now, when you type A on the computer, the computer interprets it as ASCII Code 65, which gets stored as 0100 0001.

Image45[1]

Numeric data can be held as Text Digits(ASCII Digits), or as pure Binary numbers, or as Binary Coded Decimal(BCD), or as Real Numbers(IEEE Format).

Q. What is the USAGE IS DISPLAY Clause?

USAGE IS DISPLAY is mainly used with Text Data such as ‘HELLO’,’HOW ARE YOU’ and numeric data, which is not used in computation such as Street no. ‘2530’, phone numbers like ’17654094509’.

Text-data(strings) are made up of several characters. For example, the text-string ‘HELLO’ is composed of the characters ‘H’,’E’,’L’,’L’ and ‘O’. When you apply USAGE IS DISPLAY, the text-data is stored as ASCII/EBCDIC Characters.

Generally, when Text-data is stored on PC, the collating sequence used is ASCII. On Mainframes, Text-Data is stored in EBCDIC Format.

Example:

01 WS-TEXT PIC X(5) VALUE ‘HELLO’ USAGE IS DISPLAY. 

Here, ‘H’, ‘E’, ‘L’, ‘L’ and ‘O’ are stored ASCII/EBCDIC Characters. A picture of how they look is shown below :

Image46[2] 

Each ASCII/EBCDIC Character requires 1 byte. So, the text-data ‘HELLO’ occupies 5 bytes of storage.

Q. Is there any problem, if I store numeric data, which is used in computations with a USAGE IS DISPLAY?


01 WS-VAR PIC 9 VALUE 2. 

Since no USAGE clause is explicitly specified, it defaults to USAGE IS DISPLAY.

When numeric data is specified with USAGE IS DISPLAY, these text-digits of the number are stored as ASCII/EBCDIC digits. Thus, the Text-digit 2 would be stored in computer memory as shown below -

Image47[1] 

Each text-digit is stored in ASCII/EBCDIC Form, so it will occupy 1 byte. Hence, if you store 12345.67 with USAGE IS DISPLAY, it will occupy 7 bytes.

Imagine a scenario, where you want to read and process a data-file containing 1 million phone-numbers, each phone-number being a PIC 9(10). Each phone number occupies 10 bytes of storage.

For processing 1 million phone numbers,

1 MILLION x(multiplied by) 10 bytes for each phone number = 10 million bytes.

This is quite computationally expensive in terms of storage space, time and bandwidth. Reading and processing a file as large as 10 million bytes(10 MB) takes time. This is why,
USAGE IS DISPLAY is not good choice for storing numeric data.

USAGE is DISPLAY, is more suited to working on a character-by-character level on Text data-items. When you want to perform character-level manipulations, use DISPLAY.

Q. What is the USAGE IS COMP/COMPUTATIONAL/BINARY Clause?

USAGE IS COMPUTATIONAL is more appropriate when you are dealing with integer numbers. USAGE IS COMPUTATIONAL indicates that the data-item shall be stored as pure binary numbers.

Example:
01  WS-NUMBER  PIC 9(04) VALUE 1327 USAGE IS COMPUTATIONAL.

USAGE IS COMPUTATIONAL indicates that 1327 is stored in pure binary form. A picture of how 1327 is stored, is given below :

Image49[1]  

The pure binary equivalent of 1,327 occupies 2 bytes.

The memory space calculation is done by the computer, as follows:

Integer Number Range PICTURE Clause Storage Space

-32,768 to +32,767

S9(01) to S9(04) 2 Bytes
-2 billion to +2 billion S9(05) to S9(09) 4 Bytes
-1.8 x 10^19 to +1.8 x 10^19 S(09) to S9(18) 8 Bytes

For numbers upto 32,767, 2 bytes of memory space is reserved. What if, you wanted to store an even larger integer number like 50,000? The pure binary equivalent of such numbers would then not fit into 16 bits(2 bytes). A 2-bytes bucket would not be large enough to accommodate it. They would spill over. They would need a big tumbler(say 4 bytes capacity). So, automatically 4 bytes of memory space would be reserved. What if, you store a mammoth number like a hundred billion? A hundred billion would have a picture clause S9(11), so it would occupy 8 bytes.

As you may observe on the table, based on the ranges, we can draw a correlation between the PICTURE Clause of the data-item and the Storage space.

Consider the following simple example, in which the variables WS-VAR-1 and WS-VAR-2 are declared as COMPUTATIONAL. As the USAGE IS COMPUTATIONAL, the numbers 10 and -12 are stored internally in binary format.

USAGE-1[2] 

Upon the addition Of these two integers, 10 and -12, the COBOL Code shows the following output. 10 added to –12 gives a sum of –2. Note, that sign is stored as part of the rightmost digit. K stands for minus(-ve) 2. So, 1K means –12 and 0K means –2.

USAGE-2[2] 

Q. What is the USAGE IS COMP-1/COMP-2 Clause?

When you want to work with fractional numbers, numbers containing a decimal point, you must employ USAGE IS COMP-1. USAGE IS COMP-1 is used to store floating point numbers(real numbers). COMP-1 occupies 4 bytes of memory space. Floating point(real numbers) are divided into 2 parts : mantissa part and exponent. For example, 123.456 can be represented as mantissa = 123456 e -3. The mantissa part is occupies 3 bytes. The exponent part occupies 1 byte.

On the other hand, if you have an extremely huge number like a googol, 1 e +100, you would need more space. Moreover, you a number 23 e -50 is a extremely tiny number(quite close to zero, but not zero), to store it require lot of precision and accuracy. For such numbers, you would use COMP-2. COMP-2 is used for real fractional numbers which are extremely large, and very small. COMP-2 occupies 8 bytes of memory space.

Q. What is the USAGE IS COMP-3 Clause?

As most references quote, COMP-3 is used mainly for packed decimal values(BCD Format). I would say, on most computer systems, running COBOL Applications, COMP-3 is the general standard. The reason behind this is, COMP-3 packs, two digits in 1 single byte. This is really cool, and it saves a lot of space.

Let me show ya, how COMP-3 achieves this remarkable feat. In the decimal number system, any digit can take a value ranging from 0-9, there are in all 10 possibilities. Thus, you can represent any digit with 4 bits(16 possibilities, 4 bits = 1 nibble). So, in COMP-3 format, each digit occupies only 4 bits. You must find the binary representation for each digit separately. For example, the number

  4    5    9    8    1    2 .  5    6   would be stored as
0100 0101 1001 1000 0001 0010 0101 0110.

One can infer, in COMP-3 format, two digits are packed into a single byte. So, 459812.56, a number, which is composed of 8 digits would take only 4 bytes of computer storage. There’s a general formula for memory space calculation, in COMP-3 format -
S9(4) COMP-3 would occupy 2 bytes.
S9(6) COMP-3 would occupy 3 bytes.
S9(7) COMP-3 would occupy 4 bytes.
S9(n) COMP-3 would occupy (n+1)/2 bytes.

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.