That has at least two bugs: the first test will incorrectly treat 1*-1 as 
having an overflow and the second test is testing all of R0, not just the high 
bit.


--
Shmuel (Seymour J.) Metz
http://mason.gmu.edu/~smetz3

________________________________________
From: IBM Mainframe Assembler List [ASSEMBLER-LIST@LISTSERV.UGA.EDU] on behalf 
of Ian Worthington [00000c9b78d54aea-dmarc-requ...@listserv.uga.edu]
Sent: Wednesday, April 20, 2022 9:46 AM
To: ASSEMBLER-LIST@LISTSERV.UGA.EDU
Subject: Detection of integer overflow

Whilst looking at reliable techniques to detect signed and unsigned overflow in 
integer multiplication I was checking out the late John Erhman's "Assembler 
Language Programming for IBM System z™ Servers" in which I discovered he 
presented this problem and solution:
18.2.13.(2)+ A programmer wanted to test whether the product of two positive 
32-bit binary
integers was too large to fit in a 32-bit register. <snip>
Consider multiplying 75141×56789: the product X'FE5808A9' is indeed 32
bits long but appears to be negative, −27785047. An additional test is needed:
L 1,X Load first operand
M 0,Y Multiply by second operand
LTR 0,0 Check high-order 32 bits
BNZ NotOK If not zero, product is too big
LTR 1,1 Check high-order bit of GR1
BZ ProdOK Branch if high-order 33 bits are 0s
- - - Not OK
X DC F'75141'
Y DC F'56789'
One hesitates to suggest it, but surely this cannot be correct?  This checks 
that r0 and r1 are both zero.  Surely John meant BNM ProdOk as the final 
instruction (at least for signed 32 bit integers:  no further test is required 
for unsigned integers, I think.)?
Google finds no errata for John's book.  It is, of course, much more likely 
I've misinterpreted something that John made an error!


Best wishes / Mejores deseos /  Meilleurs vœux

Ian ...

Reply via email to