PDP-8 Code Optimization (was Re: HP-35/45 Simulator for PDP-8)

Pete Turnbull pete at dunnington.plus.com
Wed Sep 14 09:20:32 CDT 2016


On 14/09/2016 03:12, Kyle Owen wrote:
> Need some more optimization fun? :) Vince and I were working on some code
> to add two signed 12 bit numbers and detect overflow, returning MAX_INT or
> MIN_INT in AC in the case of overflow, or the sum in AC otherwise. Here's
> what Vince came up with so far:
 >
 > [ 29 words, 25 instructions, 13 instructions if obviously no overflow]

OK, here's my take on it.  Not a huge saving, but 27 words, 23 
instructions, 11 if no obvious overflow:

chkovf, 0         / entry point, stores return address
TAD ovfa          / get first number
TAD ovfb          / add the second
DCA ovfsum        / save result

TAD ovfa          / get first number again
RAL               / save its sign bit
CLA RAR           / now just the first sign bit in AC (and L=0)
TAD ovfb          / add the second number, see if sign changes
SPA CLA           / if result is positive, both were the same sign
JMP done          / different signs, must be OK

           / both were same sign, so check for overflow (AC=0)
TAD ovfsum        / get the addition result
RAL               / save its sign bit
CLA RAR           / now just the sign bit in AC, and L=0
TAD ovfa          / add one of the operands, see if the sign changes
SMA CLA           / if now negative, sign is different from operands
JMP done          / positive = no overflow, no problem, done

           / fix for cases of overflow
CLA CLL CMA RAR   / make 3777 (MAX INT)
DCA ovfsum        / and save it
TAD ovfa          / get the sign of correct result
SPA CLA           / should it be negative?
ISZ ovfsum        / if yes, 3777 -> 4000 (MIN INT)
CLA               / really a NOP

done, TAD ovfsum  / get the result
JMP I chkovf      / fast return if all OK

ovfa, 0
ovfb, 0
ovfsum, 0



-- 
Pete
Pete Turnbull


More information about the cctalk mailing list