Joined: 06 Jun 2008 Posts: 8378 Location: Dubuque, Iowa, USA
What you want cannot be done -- PERIOD. COMP-3 data is numbers (as in digits zero through nine) and a sign nubble exclusively. You may convert your value to 2's complement, or you may have your value in COMP-3 -- you CANNOT have both.
Joined: 30 Nov 2013 Posts: 657 Location: The Universe
When Cobol converts data in packed decimal to "2s complement," which most of us call binary, it uses the hardware CVB (Convert to Binary) instruction, either inline or in a subroutine. CVB automatically creates the proper 2s complement value.
I wrote up the following sample Assembler program to show the steps that Cobol performs.
DWORK DC D'0'
HEXTAB DC C'0123456789ABCDEF'
RESULT DC F'0'
SOURCE DC X'000021111D'
MSG1 DC 0C'SOURCE = 000021111D'
DC C'SOURCE = '
MSG1VAL DC CL(L'SOURCE*2)' ',C' '
MSG2 DC 0C'DWORK = 0123456789ABCDEF'
DC C'DWORK = '
MSG2VAL DC CL(L'DWORK*2)' ',C' '
MSG3 DC 0C'RESULT = 01234567'
DC C'RESULT = '
MSG3VAL DC CL(L'RESULT*2)' ',C' '
The CVB instruction uses an 8 byte packed decimal data area, so the ZAP (Zero and Add Decimal) instruction expands the source to 8 bytes. The output from CVB is in a register, so the program stores the 32-bit register value. The remainder of the program translates the data areas to hexadecimal data for display purposes. TPUT is a macro to display a message at a TSO user's workstation, and is much easier than more traditional mechanisms for a simple program like this.
The output as displayed on my TSO workstation is
SOURCE = 000021111D
DWORK = 000000000021111D
RESULT = FFFFAD89