I have a legacy code written in ASM that is required to convert to COBOL.
The ASM code is as follows,
EX R8,UNPK1
OI UNWK16+15,X'F0'
UNPK1 UNPK UNWK16(16),0(0,R4)
UNWK16 DC CL16' '
The register R4 stores the address of contents in Packed format, the length is defined in R8.
For example, if R4 stores the address of content '123C' (2 bytes), then R8 stores 1 (2 bytes minus 1).
The above code is equivalent to
UNPK UNWK16(16),0(2,R4)
The execution result of this command, is '0000000000000123'.
If R4 stores the address of content '12345C' (3 bytes), then R8 stores 2 (3 bytes minus 1).
The above code is equivalent to
UNPK UNWK16(16),0(3,R4)
The execution result of this command, is '0000000000012345'.
Now I need to convert the above code to COBOL.
In COBOL, the conversion from PACKED decimal to string is automatic.
01 TEST PIC 9(8) COMP-3 VALUE 123.
01 TEST-PATTERN PIC 9(16).
01 TEST-STRING PIC X(16).
MOVE TEST TO TEST-PATTERN.
MOVE TEST-PATTERN TO TEST-STRING.
The above command will store '0000000000000123' into TEST-STRING.
However, I could not find a way to exactly define the actual data length underlying the packed decimal.
For example, one way could be,
01 R4 PIC X(16) VALUE X'123C'.
01 R4-1 REDEFINES R1 PIC 9(1) COMP-3.
01 R4-2 REDEFINES R1 PIC 9(2) COMP-3.
...
01 R4-16 REDEFINES R1 PIC 9(16) COMP-3.
This doesn't really work because I cannot make sure a 9(3) COMP-3 is exactly 2 byte, and 9(5) COMP-3 is exactly 3 bytes.
Is there a way to write this in COBOL with exactly the same input and output as ASM?
Q: Is there a way to write this in COBOL with exactly the same input and output as ASM?
The example given in the OP is for unsigned values (the
OIinstruction and the absence ofSin thePICclauses). Should the sameUNPKinstruction be used for signed values, an adaptation to the following is required.I also show there is no difference with a straight
MOVE.Note that the
LINKAGE SECTIONholds thePICTUREclauses that will be used by theMOVEstatement. UsingLENGTH OF ... - 1assures those picture clauses will match the original.Code:
Output:
Using UNPK procedure:
Using straight
MOVE