SIGN LEADING, TRAILING and SEPARATE

A signed DISPLAY field has to keep a sign somewhere, and the space between the characters is already taken. COBOL offers four answers: the sign pressed into the zone of the first digit, into the zone of the last one, or in a byte of its own at either end. The four differ by a byte and by where a reader of the file has to look, and a file with the wrong one in it is a file whose numbers are all wrong.

The sign in the last byte

Without a clause the sign goes into the zone of the last digit, and the field keeps the length of its picture. The clause that says so is SIGN IS TRAILING, and the form is called trailing overpunch: the last character holds a digit and a sign at once.

IDENTIFICATION DIVISION. PROGRAM-ID. SIGN. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEF-F PIC S9(3) VALUE -42. 01 LEAD-F PIC S9(3) SIGN IS LEADING VALUE -42. 01 TSEP-F PIC S9(3) SIGN IS TRAILING SEPARATE VALUE -42. 01 LSEP-F PIC S9(3) SIGN IS LEADING SEPARATE VALUE -42. PROCEDURE DIVISION. DISPLAY "default " LENGTH OF DEF-F " " FUNCTION HEX-OF(DEF-F) " [" DEF-F "]". DISPLAY "leading " LENGTH OF LEAD-F " " FUNCTION HEX-OF(LEAD-F) " [" LEAD-F "]". DISPLAY "trail sep " LENGTH OF TSEP-F " " FUNCTION HEX-OF(TSEP-F) " [" TSEP-F "]". DISPLAY "leading sep " LENGTH OF LSEP-F " " FUNCTION HEX-OF(LSEP-F) " [" LSEP-F "]". STOP RUN.

cobc -x -free -Wall -o sign sign.cob
./sign

default 3 303472 [-042]
leading 3 703432 [-042]
trail sep 4 3034322D [042-]
leading sep 4 2D303432 [-042]

Four lines, four layouts, and the hex is the honest part of the output. The default field is 30 34 72: a plain zero, a plain four and 72, which is the letter r, whose low half is 2 and whose high half marks a minus. The field is three bytes and the sign is inside the third one.

LEADING and old files

SIGN IS LEADING moves the overpunch to the first byte: the hex becomes 70 34 32, where 70 is the letter p, the zero with the minus zone. Still three bytes, and the sign is at the front.

Both overpunch forms print as -042, because DISPLAY knows the clause and produces a readable sign in front. The clause exists because files exist: a record written by a 1970s program with the sign at the front stays readable by a program that declares SIGN IS LEADING and by no program that does not.

The two letters are the ASCII form of the convention. r for a minus two and p for a minus zero are the natural result of setting the zone to 0111 instead of 0011 on an ASCII digit; on an EBCDIC machine the same two fields show as D2 and D0. The lesson on ASCII, EBCDIC and UTF-8 has the code pages.

SEPARATE costs a byte

A file read by a program that does not know COBOL should not have to decode a zone, and SEPARATE is for it: the sign gets a byte of its own, holding + or -, in front of the digits or after them.

The price is one byte. S9(3) SIGN IS TRAILING SEPARATE is four bytes and its hex is 30 34 32 2D: the digits and then a real minus, the ASCII code 2D. The leading form puts the same byte first, 2D 30 34 32.

Notice what DISPLAY does with the trailing separate field: it prints 042-, the digits then the sign, because there is nothing to hide: the sign is a character in the field and it prints where it is stored. The overpunch forms print -042 because the sign has to be reconstructed from the zone to be seen at all.

The sign survives arithmetic

All four are numbers to the program, and the clause is a storage detail that arithmetic does not see:

IDENTIFICATION DIVISION. PROGRAM-ID. SIGNARITH. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-F PIC S9(4)V99 SIGN IS TRAILING SEPARATE VALUE 0. 01 ITEM-F PIC S9(3)V99 VALUE -12.34. PROCEDURE DIVISION. ADD ITEM-F TO TOTAL-F. ADD 0.34 TO TOTAL-F. DISPLAY "total [" TOTAL-F "]". ADD -1 TO TOTAL-F. DISPLAY "after -1 [" TOTAL-F "]". STOP RUN.

cobc -x -free -Wall -o signarith signarith.cob
./signarith

total [0012.00-]
after -1 [0013.00-]

Minus twelve and thirty-four hundredths plus thirty-four hundredths is minus twelve, and the field keeps its sign through the addition of a positive number. That is the property the whole clause protects: a negative amount that becomes positive has to change its sign character, and a field with no S cannot.

A record with a separate sign

The byte a separate sign costs is where file layouts go wrong. A record laid out with three bytes for the amount and two for the code is six bytes as written below, and every field after the amount is one byte to the right of where the specification put it:

IDENTIFICATION DIVISION. PROGRAM-ID. RECORD. DATA DIVISION. WORKING-STORAGE SECTION. 01 REC. 05 AMT-F PIC S9(3) SIGN IS TRAILING SEPARATE VALUE -42. 05 CODE-F PIC X(2) VALUE "AB". 01 RAW REDEFINES REC PIC X(6). PROCEDURE DIVISION. DISPLAY "record [" RAW "]". DISPLAY "length " LENGTH OF REC. DISPLAY "amount [" AMT-F "] code [" CODE-F "]". STOP RUN.

cobc -x -free -Wall -o record record.cob
./record

record [042-AB]
length 6
amount [042-] code [AB]

The redefined PIC X(6) shows the record as the file sees it: four bytes of amount, then the two of the code. A program on the other side that reads three bytes for the amount and two for the code takes 042 as the amount and -A as the code, and gets an amount of 42 and a code that no lookup table knows.

The lesson is not "never use SEPARATE": it is that the clause is part of the file format and has to be written in the record description at both ends. A record whose layout is fixed by another system keeps the clause that system uses, whatever the program would prefer.

SIGN belongs to DISPLAY

The clause describes where a character sign sits, so it only makes sense in a field that has characters. A binary field keeps its sign in a bit, and a packed field in a half byte, and the compiler refuses the clause on anything else:

IDENTIFICATION DIVISION. PROGRAM-ID. SIGNCOMP. DATA DIVISION. WORKING-STORAGE SECTION. 01 BIN-F PIC S9(3) COMP SIGN IS LEADING. PROCEDURE DIVISION. DISPLAY BIN-F. STOP RUN.

cobc -x -free -Wall -o signcomp signcomp.cob

signcomp.cob:6: error: elementary items with SIGN clause must be USAGE DISPLAY or NATIONAL
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 BIN-F PIC S9(3) COMP SIGN IS LEADING.
7 |
8 | PROCEDURE DIVISION.

The same rule covers COMP-3: a packed field already ends with C or D, as the lesson before this shows, and there is no third place for a sign to go.

Exercises

  1. Print the hex of PIC S9(2) VALUE 17 and of the same field with SIGN IS LEADING. Which two bytes differ?
  2. Change the record program to give the amount PIC S9(3) with no clause and print LENGTH OF REC and the raw record again.
  3. Print the hex of a PIC S9(3) SIGN IS LEADING SEPARATE VALUE -42 and name the byte that holds the sign.
  4. Build a program with PIC 9(3) SIGN IS TRAILING. Is the clause accepted on a field with no S, and what do the bytes look like?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does the sign of a PIC S9(3) field live by default?

    Answer

    In the zone of the last digit, so the field stays three bytes

    The default is SIGN IS TRAILING, which overpunches the last digit, so -42 stores as 04r.

  2. What does SEPARATE add to a signed DISPLAY field?

    Answer

    One byte holding + or - at the leading or the trailing end

    SEPARATE takes the sign out of the zone and stores it as a character, which costs a byte and makes the field readable as text.

  3. Which fields accept a SIGN clause?

    Answer

    USAGE DISPLAY and NATIONAL only

    The clause places a character sign, so it applies to the character usages; binary and packed fields keep the sign in a bit or a nibble.

  4. A PIC S9(3) SIGN IS TRAILING SEPARATE field takes how many bytes?

    Answer

    4

    Three digits and a sign character are four bytes, which is the byte that shifts every field laid out after it.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEF-F PIC S9(3) VALUE -42. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(DEF-F). STOP RUN.

    Answer

    303472

    Trailing overpunch keeps the first two digits plain and gives the last byte the minus zone, which is 72, the letter r.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 LEAD-F PIC S9(3) SIGN IS LEADING VALUE -42. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(LEAD-F). STOP RUN.

    Answer

    703432

    LEADING moves the overpunch to the first byte, 70, and the two digits stay plain after it.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 TSEP-F PIC S9(3) SIGN IS TRAILING SEPARATE VALUE -42. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(TSEP-F). STOP RUN.

    Answer

    3034322D

    The three digits come first and the sign is a real minus character, 2D, in the fourth byte.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 LSEP-F PIC S9(3) SIGN IS LEADING SEPARATE VALUE -42. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(LSEP-F). STOP RUN.

    Answer

    2D303432

    A leading separate sign is its own byte in front of the digits, and a minus is 2D.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 TSEP-F PIC S9(3) SIGN IS TRAILING SEPARATE VALUE -42. PROCEDURE DIVISION. DISPLAY "[" TSEP-F "]". STOP RUN.

    Answer

    [042-]

    A separate sign is a character in the field, so DISPLAY prints it where it is stored, at the end.

  10. A file specification gives three bytes to a signed amount, and the program declares the field with SIGN IS LEADING SEPARATE. What goes wrong?

    Answer

    The field is four bytes, so every field after it is read one byte off

    SEPARATE spends a byte on the sign, and the record description on both sides of a file has to agree on every byte of the layout.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image