2 byte binary overflow
Select messages from
# through # FAQ
[/[Print]\]

MVSFORUMS.com -> Application Programming

#1: 2 byte binary overflow Author: deepa12Location: chennai PostPosted: Sun Feb 26, 2017 8:47 am
    —
Code:

01  ws-new pic  s9(4) comp value 36125.
01  ws-old  pic   s9(4) comp value 27053.
01 ws-result  pic s9(8) comp.



Actually ws-new - ws-old is 9072 But it was showing a value of -5646
s9(4) comp can have only 32767
So would like to know how it really works. i.e how any no: higher than 32767 be stored & how it shows -5646

#2:  Author: kolusuLocation: San Jose PostPosted: Sun Feb 26, 2017 12:31 pm
    —
deepa12,

You need to understand the compiler option TRUNC in COBOL.Check this link which explains in detail about how you are getting the results you got.

https://www.ibm.com/support/knowledgecenter/SSQ2R2_9.5.1/com.ibm.etools.cbl.win.doc/topics/up4060.htm

#3:  Author: William Collins PostPosted: Sun Feb 26, 2017 5:10 pm
    —
What compiler are you using? It's name and version is on the top of each page of the compiler listing.

That code won't compile with any IBM COBOL I've used (one or two, Severe messages, depending on the option for TRUNC).

The maximum value for COMP PIC S9(4) is +9999, being defined by the number of digits specified in the PICture.

The maximum value for COMP-5 PIC S9(4) is +32767, being defined by the size of the field (two bytes) which is defined by the number of digits specified in the PICture.

A COMP automatically is treated as COMP-5 if you use compiler option TRUNC(BIN).

COMP-5 is also known as "native binary". Native-binary is, generally, slower than COBOL's normal decimal-limited binary fields.

#4:  Author: deepa12Location: chennai PostPosted: Mon Feb 27, 2017 2:53 am
    —
We are using IBM Enterprise COBOL for z/OS 4.2.0 version of cobol compiler
Sorry the values specified in VALUE CLAUSE are all run time values during a move statement
Yes we are using TRUNC(BIN) option
When its displayed in intertest, its showing F5F6F4D6
I would like to know how it shows this value

when i write a small program
Code:


01 ws-a pic s9(4) comp
01 ws-a-x redefines ws-a pic xx.
01 ws-dest pic s9(4) comp
01 ws-dest-x redefines ws-dest pic xx.
01 ws-sysin pic 9(5).
procedure division
accept ws-sysin
move ws-sysin to  ws-a
compute ws-dest =ws-a -1
display ws-a :" ws-a-x ":" ws-dest ":" ws-dest1


ws-sysin takes 32615
result is:
ws-a is 2941J WS-A-X is 8D1D

WS-DEST IS 2941K WS-DEST-X is 8D1C

i would like to know how it shows 2941J

#5:  Author: kolusuLocation: San Jose PostPosted: Mon Feb 27, 2017 11:41 am
    —
deepa12 wrote:

When its displayed in intertest, its showing F5F6F4D6
I would like to know how it shows this value

ws-sysin takes 32615
result is:
ws-a is 2941J WS-A-X is 8D1D

WS-DEST IS 2941K WS-DEST-X is 8D1C

i would like to know how it shows 2941J



Deepa12,

First and most important thing you need to remember is please make sure you write the correct numbers. You say the sysin value is 32615, but based on the output display values, you passed 36125 which is in your initial post.

So be real careful when dealing with numbers

Now coming to your question.

Cobol COMP items definitions and max values are defined here

https://www.ibm.com/support/knowledgecenter/en/SS6SG3_4.2.0/com.ibm.entcobol.doc_4.2/PGandLR/ref/rlddecom.htm

So S9(1) through S9(4) is a Binary halfword (2 bytes) and can store values between -32768 through +32767

But you are passing 36125 and it is greater than 32767, so Cobol subtracts the 2's complement X'10000' aka 65,536 from your passed value.

ie. 36,125 - 65,536 = -29411

and since the sign is over punched upon display you see it as 2941J

and the pic xx of ws-a value shows x'8D1D' which is just the hex value of 36125, as there is no rounding involved.

And for Ws-Dest, you are subtracting 1 from ws-a which is now -29411 and minus 1 will be -29412 which is shown as 2941K with sign overpunch.

And your Pic XX value of ws-dest has x'8D1C' which is 36125 - 1 = 36124.

If you passed value greater than 65536 and less than 98303, you will see a positive value

ex: 76125 - 65536 = 10589

so here is a rule of thumb that you can figure out if you end up with a positive number or negative number assuming you pass positive values

Int(passed-value/32767) = odd then the result is Negative

ex: if passed-value is 36125 then Int(36125/32767) = 1 , so the result will be negative which is -29411

Int(passed-value/32767) = even then the result is Positive

ex: if passed-value is 76125 then Int(76125/32767) = 2 , so the result will be positive which is 10589.

PS: And the next time you post of piece of code, make sure you post it correctly without any mistakes as the posted sample program will NOT compile.

#6:  Author: deepa12Location: chennai PostPosted: Wed Mar 01, 2017 7:33 am
    —
OK. Thanks a lot for your clarifications
I will ensure that I post a working code & also numeric typos are not present



MVSFORUMS.com -> Application Programming


output generated using printer-friendly topic mod. All times are GMT - 5 Hours

Page 1 of 1

Powered by phpBB © 2001, 2005 phpBB Group