Menu

Confused about COMP limits and how it gets moved to X field

Anonymous
2021-08-12
2021-08-16
  • Anonymous

    Anonymous - 2021-08-12

    Please help me understand what is going on in the code below. Do you think the same results would appear on the mainframe or other dialects of COBOL? Thank you much.
    This code was compiled and run on Windows 10 GNU COBOL 3.2.1

    IDENTIFICATION DIVISION.
    PROGRAM-ID. HELLO-WORLD.
    
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    
    01 C    PIC S9(4)  COMP.
    01 C5   PIC S9(4)  COMP-5.
    01 MSG  PIC X(100).
    
    PROCEDURE DIVISION.
    
    MOVE 32767 TO C, C5
    
    *> CASE 1 - I expected that COMP is to hold the value 32767
    IF C  = 32767 THEN DISPLAY " C  IS OK" ELSE DISPLAY "C NOT OK" END-IF *> C NOT OK
    
    *> CASE 2 ok
    IF C5 = 32767 THEN DISPLAY " C5 IS OK" END-IF                         *> C OK
    
    *> CASE 3 I expected the value 32767 to be shown at least for the c5
    DISPLAY "LAST:" C SPACE C5 *> LAST:2767 2767
    
    STOP RUN.
    
     
  • Emmad

    Emmad - 2021-08-14

    I believe this has to do with the compilation options. Not sure which options to use though.

     

    Last edit: Emmad 2021-08-14
  • Ralph Linkletter

    if -fnotrunc is specified as a compile option the binary fields (comp (aka comp-4)) and comp-5 will support (2 ^ the number of bits -1) in the picture clause. Sans -fnotrunc the digits defined in the PIC clause dictate the maximum value. 9(4) COMP-5 is 16 bits wide - therefore the maximum value is 2 ^ 15th or 32768 in decimal. If the high order (most significant bit) is has value of one it indicates a negative number

    Omitting -fnotrunc will do things like 2's complement when the value exceeds 9999. I don't know the exact behaviors.

    -fnotrunc should make your example work correctly. I have no idea what COMP-X does to the axiom.

    Normally Pic 9(01) thru Pic 9(04) will be 16 bits wide.
    Pic 9(05) thru PIC 9(09) will be 32 bits wide
    Pic 9(10) thru 9(18) will be 64 bits wide

    S9(1) - S9(4) Binary halfword (2 bytes) -32768 through +32767
    S9(5) - S9(9) Binary fullword (4 bytes) -2,147,483,648 through +2,147,483,647
    S9(10) - S9(18) Binary doubleword (8 bytes) +-9,223,372,036,854,775,808
    9(1) through 9(4) Binary halfword (2 bytes) 0 through 65535
    9(5) through 9(9) Binary fullword (4 bytes) 0 through 4,294,967,295
    9(10) through 9(18) Binary doubleword (8 bytes) 0 through 18,446,744,073,709,551,615

    Ralph

     
  • Anonymous

    Anonymous - 2021-08-14

    Thank you for the valuable information.

     
  • Anonymous

    Anonymous - 2021-08-14

    Would this flag -fnotrunc be equivelent to mainframe TRUNC(BIN)? I guess yes.

     

Anonymous
Anonymous

Add attachments
Cancel