MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

2 byte binary overflow

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
deepa12
Beginner


Joined: 05 Apr 2005
Posts: 109
Topics: 57
Location: chennai

PostPosted: Sun Feb 26, 2017 8:47 am    Post subject: 2 byte binary overflow Reply with quote

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
_________________
deepa
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 11769
Topics: 74
Location: San Jose

PostPosted: Sun Feb 26, 2017 12:31 pm    Post subject: Reply with quote

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
_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
Back to top
View user's profile Send private message Send e-mail Visit poster's website
William Collins
Supermod


Joined: 03 Jun 2012
Posts: 437
Topics: 0

PostPosted: Sun Feb 26, 2017 5:10 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
deepa12
Beginner


Joined: 05 Apr 2005
Posts: 109
Topics: 57
Location: chennai

PostPosted: Mon Feb 27, 2017 2:53 am    Post subject: Reply with quote

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
_________________
deepa
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 11769
Topics: 74
Location: San Jose

PostPosted: Mon Feb 27, 2017 11:41 am    Post subject: Reply with quote

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.
_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
Back to top
View user's profile Send private message Send e-mail Visit poster's website
deepa12
Beginner


Joined: 05 Apr 2005
Posts: 109
Topics: 57
Location: chennai

PostPosted: Wed Mar 01, 2017 7:33 am    Post subject: Reply with quote

OK. Thanks a lot for your clarifications
I will ensure that I post a working code & also numeric typos are not present
_________________
deepa
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group