2021-12-23 00:43:10 -05:00
|
|
|
( math32.tal )
|
|
|
|
( )
|
2021-12-27 20:35:23 -05:00
|
|
|
( This library supports arithmetic on 32-bit unsigned integers. )
|
2021-12-23 00:43:10 -05:00
|
|
|
( 32-bit integers are represented by two 16-bit integers )
|
|
|
|
( x** means xhi* xlo* )
|
|
|
|
|
|
|
|
%DEBUG { #ff #0e DEO }
|
|
|
|
%RTN { JMP2r }
|
2021-12-25 23:07:41 -05:00
|
|
|
%TOR { ROT ROT } ( a b c -> c a b )
|
2021-12-23 00:43:10 -05:00
|
|
|
%TOR2 { ROT2 ROT2 }
|
2021-12-27 15:04:41 -05:00
|
|
|
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 } )
|
2021-12-25 23:07:41 -05:00
|
|
|
|
2021-12-26 21:01:22 -05:00
|
|
|
( bitcount: number of bits needed to represent number )
|
|
|
|
( equivalent to floor[log2[x]] + 1 )
|
2021-12-26 00:47:13 -05:00
|
|
|
|
2021-12-24 16:33:59 -05:00
|
|
|
@bitcount8 ( x^ -> n^ )
|
|
|
|
#00 SWP ( n x )
|
|
|
|
&loop
|
|
|
|
DUP #00 EQU ( n x x=0 )
|
|
|
|
,&done JCN ( n x )
|
|
|
|
#01 SFT ( n x>>1 )
|
|
|
|
SWP INC SWP ( n+1 x>>1 )
|
|
|
|
,&loop JMP
|
|
|
|
&done
|
|
|
|
POP ( n )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
@bitcount16 ( x* -> n^ )
|
|
|
|
SWP ( xlo xhi )
|
|
|
|
;bitcount8 JSR2 ( xlo nhi )
|
|
|
|
DUP #00 NEQ ( xlo nhi nhi!=0 )
|
|
|
|
,&hi-set JCN ( xlo nhi )
|
|
|
|
SWP ;bitcount8 JSR2 ADD ( nhi+nlo )
|
|
|
|
RTN
|
|
|
|
&hi-set
|
|
|
|
SWP POP #08 ADD ( nhi+8 )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
@bitcount32 ( x** -> n^ )
|
|
|
|
SWP2 ( xlo* xhi* )
|
|
|
|
;bitcount16 JSR2 ( xlo* nhi )
|
|
|
|
DUP #00 NEQ ( xlo* nhi nhi!=0 )
|
|
|
|
,&hi-set JCN ( xlo* nhi )
|
|
|
|
TOR ;bitcount16 JSR2 ADD RTN ( nhi+nlo )
|
|
|
|
&hi-set
|
|
|
|
TOR POP2 #10 ADD ( nhi+16 )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
2021-12-26 21:01:22 -05:00
|
|
|
( equality )
|
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x == y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
2021-12-28 16:40:11 -05:00
|
|
|
ROT2 EQU2 STH
|
|
|
|
EQU2 STHr AND RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x != y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
2021-12-28 16:40:11 -05:00
|
|
|
ROT2 NEQ2 STH
|
|
|
|
NEQ2 STHr ORA RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x == 0 )
|
|
|
|
@is-zero32 ( x** -> bool^ )
|
|
|
|
ORA2 #0000 EQU2 RTN
|
|
|
|
|
|
|
|
( x != 0 )
|
2021-12-26 01:56:43 -05:00
|
|
|
@non-zero32 ( x** -> bool^ )
|
2021-12-27 15:09:49 -05:00
|
|
|
ORA2 #0000 NEQ2 RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
2021-12-26 21:01:22 -05:00
|
|
|
( comparisons )
|
|
|
|
|
|
|
|
( x < y )
|
|
|
|
@lt32 ( x** y** -> bool^ )
|
|
|
|
ROT2 SWP2 ( xhi yhi xlo ylo )
|
|
|
|
LTH2 ,<-lo JCN ( xhi yhi )
|
|
|
|
LTH2 RTN
|
|
|
|
<-lo
|
|
|
|
GTH2 #00 EQU RTN
|
|
|
|
|
|
|
|
( x <= y )
|
|
|
|
@lteq32 ( x** y** -> bool^ )
|
|
|
|
ROT2 SWP2 ( xhi yhi xlo ylo )
|
|
|
|
GTH2 ,>-lo JCN ( xhi yhi )
|
|
|
|
GTH2 #00 EQU RTN
|
|
|
|
>-lo
|
|
|
|
LTH2 RTN
|
|
|
|
|
|
|
|
( x > y )
|
|
|
|
@gt32 ( x** y** -> bool^ )
|
|
|
|
ROT2 SWP2 ( xhi yhi xlo ylo )
|
|
|
|
GTH2 ,>-lo JCN ( xhi yhi )
|
|
|
|
GTH2 RTN
|
|
|
|
>-lo
|
|
|
|
LTH2 #00 EQU RTN
|
|
|
|
|
|
|
|
( x > y )
|
|
|
|
@gteq32 ( x** y** -> bool^ )
|
|
|
|
ROT2 SWP2 ( xhi yhi xlo ylo )
|
|
|
|
LTH2 ,<-lo JCN ( xhi yhi )
|
|
|
|
LTH2 #00 EQU RTN
|
|
|
|
<-lo
|
|
|
|
GTH2 RTN
|
|
|
|
|
|
|
|
( bitwise operations )
|
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x & y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
2021-12-28 16:40:11 -05:00
|
|
|
ROT2 AND2 STH2 AND2 STH2r RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x | y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
2021-12-28 16:40:11 -05:00
|
|
|
ROT2 ORA2 STH2 ORA2 STH2r RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x ^ y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
2021-12-28 16:40:11 -05:00
|
|
|
ROT2 EOR2 STH2 EOR2 STH2r RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( ~x )
|
2021-12-23 19:50:46 -05:00
|
|
|
@complement32 ( x** -> ~x** )
|
2021-12-27 15:09:49 -05:00
|
|
|
COMPLEMENT32 RTN
|
2021-12-23 19:50:46 -05:00
|
|
|
|
2021-12-27 15:27:54 -05:00
|
|
|
( temporary registers )
|
2021-12-27 15:51:59 -05:00
|
|
|
( used by most operations, except mul32 and div32 )
|
2021-12-28 15:33:42 -05:00
|
|
|
@sh [ &x0 $1 &x1 $1 &x2 $1 &x3 $1
|
2021-12-27 15:51:59 -05:00
|
|
|
&y0 $1 &y1 $1 &y2 $1 &y3 $1
|
|
|
|
&z0 $1 &z1 $1 &z2 $1 &z3 $1
|
|
|
|
&a0 $1 &a1 $1 &a2 $2 ]
|
2021-12-27 15:27:54 -05:00
|
|
|
|
2021-12-26 21:01:22 -05:00
|
|
|
( bit shifting )
|
2021-12-23 19:50:46 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x >> n )
|
2021-12-26 01:56:43 -05:00
|
|
|
@right-shift ( x** n^ -> x<<n )
|
|
|
|
DUP #08 LTH ;right-shift0 JCN2 ( x n )
|
|
|
|
DUP #10 LTH ;right-shift1 JCN2 ( x n )
|
|
|
|
DUP #18 LTH ;right-shift2 JCN2 ( x n )
|
|
|
|
;right-shift3 JMP2 ( x n )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-26 01:56:43 -05:00
|
|
|
( shift right by 0-7 bits )
|
|
|
|
@right-shift0 ( x** n^ -> x<<n )
|
2021-12-28 15:25:26 -05:00
|
|
|
STHk SFT ;sh/z3 STA ( write z3 )
|
2021-12-28 15:04:51 -05:00
|
|
|
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
|
|
|
#00 STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( write z1,z2 )
|
2021-12-28 15:25:26 -05:00
|
|
|
#00 STHr SFT2 #00 ;sh/z1 LDA ORA2 ( compute z0,z1 )
|
|
|
|
;sh/z2 LDA2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-26 01:56:43 -05:00
|
|
|
|
|
|
|
( shift right by 8-15 bits )
|
|
|
|
@right-shift1 ( x** n^ -> x<<n )
|
2021-12-28 15:25:26 -05:00
|
|
|
#08 SUB STH POP
|
|
|
|
STHkr SFT ;sh/z3 STA ( write z3 )
|
|
|
|
#00 STHkr SFT2 #00 ;sh/z3 LDA ORA2 ;sh/z2 STA2 ( write z2,z3 )
|
|
|
|
#00 STHr SFT2 #00 ;sh/z2 LDA ORA2 ( compute z1,z2 )
|
|
|
|
#00 TOR ;sh/z3 LDA
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-26 01:56:43 -05:00
|
|
|
|
|
|
|
( shift right by 16-23 bits )
|
|
|
|
@right-shift2 ( x** n^ -> x<<n )
|
2021-12-28 15:25:26 -05:00
|
|
|
#10 SUB STH POP2
|
|
|
|
STHkr SFT ;sh/z3 STA ( write z3 )
|
|
|
|
#00 STHr SFT2 #00 ;sh/z3 LDA ORA2 ( compute z2,z3 )
|
|
|
|
#0000 SWP2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-26 01:56:43 -05:00
|
|
|
|
|
|
|
( shift right by 16-23 bits )
|
|
|
|
@right-shift3 ( x** n^ -> x<<n )
|
2021-12-28 15:25:26 -05:00
|
|
|
#18 SUB STH POP2 POP ( x0 )
|
|
|
|
#00 SWP #0000 SWP2 ( 00 00 00 x0 )
|
|
|
|
STHr SFT
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-26 01:56:43 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x << n )
|
2021-12-24 16:33:59 -05:00
|
|
|
@left-shift ( x** n^ -> x<<n )
|
|
|
|
DUP #08 LTH ;left-shift0 JCN2 ( x n )
|
|
|
|
DUP #10 LTH ;left-shift1 JCN2 ( x n )
|
|
|
|
DUP #18 LTH ;left-shift2 JCN2 ( x n )
|
|
|
|
;left-shift3 JMP2 ( x n )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
( shift left by 0-7 bits )
|
|
|
|
@left-shift0 ( x** n^ -> x<<n )
|
2021-12-28 15:33:42 -05:00
|
|
|
#40 SFT STH ( stash n<<4 )
|
|
|
|
#00 SWP STHkr SFT2 ;sh/z2 STA2 ( store z2,z3 )
|
|
|
|
#00 SWP STHkr SFT2 #00 ;sh/z2 LDA ORA2 ;sh/z1 STA2 ( store z1,z2 )
|
|
|
|
#00 SWP STHkr SFT2 #00 ;sh/z1 LDA ORA2 ;sh/z0 STA2 ( store z0,z1 )
|
|
|
|
STHr SFT ;sh/z0 LDA ORA ( calculate z0 )
|
|
|
|
;sh/z1 LDA ;sh/z2 LDA2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
( shift left by 8-15 bits )
|
|
|
|
@left-shift1 ( x** n^ -> x<<n )
|
2021-12-28 15:39:20 -05:00
|
|
|
#08 SUB #40 SFT STH ( stash [n-8]<<4 )
|
|
|
|
#00 SWP STHkr SFT2 ;sh/z1 STA2 ( store z1,z2 )
|
|
|
|
#00 SWP STHkr SFT2 #00 ;sh/z1 LDA ORA2 ;sh/z0 STA2 ( store z0,z1 )
|
|
|
|
STHr SFT ;sh/z0 LDA ORA ( calculate z0 )
|
2021-12-28 15:45:15 -05:00
|
|
|
SWP POP ( x0 unused )
|
2021-12-28 15:39:20 -05:00
|
|
|
;sh/z1 LDA2 #00
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
( shift left by 16-23 bits )
|
|
|
|
@left-shift2 ( x** n^ -> x<<n )
|
2021-12-28 15:45:15 -05:00
|
|
|
#10 SUB #40 SFT STH ( stash [n-16]<<4 )
|
|
|
|
#00 SWP STHkr SFT2 ;sh/z0 STA2 ( store z0,z1 )
|
|
|
|
STHr SFT ;sh/z0 LDA ORA ( calculate z0 )
|
|
|
|
STH POP2 STHr
|
|
|
|
;sh/z1 LDA #0000
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
|
|
|
( shift left by 24-31 bits )
|
|
|
|
@left-shift3 ( x** n^ -> x<<n )
|
2021-12-28 15:45:15 -05:00
|
|
|
#18 SUB #40 SFT ( x0 x1 x2 x3 r=[n-24]<<4 )
|
2021-12-24 16:33:59 -05:00
|
|
|
SFT ( x0 x1 x2 x3<<r )
|
|
|
|
SWP2 POP2 SWP POP #0000 #00
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
|
2021-12-26 21:01:22 -05:00
|
|
|
( arithmetic )
|
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x + y )
|
2021-12-23 00:43:10 -05:00
|
|
|
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
|
2021-12-27 15:27:54 -05:00
|
|
|
;sh/y2 STA2 ;sh/y0 STA2 ( save ylo, yhi )
|
|
|
|
;sh/x2 STA2 ;sh/x0 STA2 ( save xlo, xhi )
|
|
|
|
#0000 #0000 ;sh/z0 STA2 ;sh/z2 STA2 ( reset zhi, zlo )
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x3 + y3 => z2z3 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x3 LDA #00 ;sh/y3 LDA ADD2 ;sh/z2 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x2 + y2 + z2 => z1z2 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x2 LDA ;sh/z1 LDA2 ADD2 ;sh/z1 STA2
|
|
|
|
#00 ;sh/y2 LDA ;sh/z1 LDA2 ADD2 ;sh/z1 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x1 + y1 + z1 => z0z1 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x1 LDA ;sh/z0 LDA2 ADD2 ;sh/z0 STA2
|
|
|
|
#00 ;sh/y1 LDA ;sh/z0 LDA2 ADD2 ;sh/z0 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x0 + y0 + z0 => z0 )
|
2021-12-27 15:27:54 -05:00
|
|
|
;sh/x0 LDA ;sh/z0 LDA ADD ;sh/z0 STA
|
|
|
|
;sh/y0 LDA ;sh/z0 LDA ADD ;sh/z0 STA
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( load zhi,zlo )
|
2021-12-27 15:27:54 -05:00
|
|
|
;sh/z0 LDA2 ;sh/z2 LDA2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( -x )
|
2021-12-26 21:01:22 -05:00
|
|
|
@negate32 ( x** -> -x** )
|
|
|
|
COMPLEMENT32
|
|
|
|
INC2 ( ~xhi -xlo )
|
|
|
|
DUP2 #0000 NEQ2 ( ~xhi -xlo non-zero? )
|
|
|
|
,&done JCN ( xlo non-zero => don't inc hi )
|
|
|
|
SWP2 INC2 SWP2 ( -xhi -xlo )
|
|
|
|
&done
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-26 21:01:22 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x - y )
|
2021-12-23 19:50:46 -05:00
|
|
|
@sub32 ( x** y** -> z** )
|
2021-12-27 15:09:49 -05:00
|
|
|
;negate32 JSR2 ;add32 JSR2 RTN
|
2021-12-23 19:50:46 -05:00
|
|
|
|
2021-12-27 15:51:59 -05:00
|
|
|
( 16-bit multiplication )
|
2021-12-23 00:43:10 -05:00
|
|
|
@mul16 ( x* y* -> z** )
|
2021-12-27 15:27:54 -05:00
|
|
|
;sh/y1 STA ;sh/y0 STA ( save ylo, yhi )
|
|
|
|
;sh/x1 STA ;sh/x0 STA ( save xlo, xhi )
|
|
|
|
#0000 #00 ;sh/z1 STA2 ;sh/z3 STA ( reset z1,z2,z3 )
|
|
|
|
#0000 #00 ;sh/a0 STA2 ;sh/a2 STA ( reset a0,a1,a2 )
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x1 * y1 => z1z2 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x1 LDA #00 ;sh/y1 LDA MUL2 ;sh/z2 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x0 * y1 => z0z1 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x0 LDA #00 ;sh/y1 LDA MUL2 ;sh/z1 LDA2 ADD2 ;sh/z1 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x1 * y0 => a1a2 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x1 LDA #00 ;sh/y0 LDA MUL2 ;sh/a1 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
|
|
|
( x0 * y0 => a0a1 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/x0 LDA #00 ;sh/y0 LDA MUL2 ;sh/a0 LDA2 ADD2 ;sh/a0 STA2
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-23 01:01:09 -05:00
|
|
|
( add z and a<<8 )
|
2021-12-27 15:27:54 -05:00
|
|
|
#00 ;sh/z1 LDA2 ;sh/z3 LDA
|
|
|
|
;sh/a0 LDA2 ;sh/a2 LDA #00
|
2021-12-23 00:43:10 -05:00
|
|
|
;add32 JSR2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x * y )
|
2021-12-27 15:51:59 -05:00
|
|
|
@mul32 ( x** y** -> z** )
|
|
|
|
,&y1 STR2 ,&y0 STR2 ( save ylo, yhi )
|
|
|
|
,&x1 STR2 ,&x0 STR2 ( save xlo, xhi )
|
|
|
|
,&y1 LDR2 ,&x1 LDR2 ;mul16 JSR2 ( [x1*y1] )
|
|
|
|
,&z1 STR2 ,&z0 STR2 ( sum = x1*y1, save zlo, zhi )
|
2021-12-23 19:50:46 -05:00
|
|
|
|
2021-12-27 15:51:59 -05:00
|
|
|
,&y1 LDR2 ,&x0 LDR2 MUL2 ( [x0*y1]<<16 )
|
|
|
|
,&y0 LDR2 ,&x1 LDR2 MUL2 ( [x1*y0]<<16 )
|
2021-12-23 00:43:10 -05:00
|
|
|
( [x0*y0]<<32 will completely overflow )
|
2021-12-27 15:51:59 -05:00
|
|
|
ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 )
|
|
|
|
,&z1 LDR2
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-27 15:51:59 -05:00
|
|
|
[ &x0 $2 &x1 $2
|
|
|
|
&y0 $2 &y1 $2
|
|
|
|
&z0 $2 &z1 $2 ]
|
2021-12-23 00:43:10 -05:00
|
|
|
|
2021-12-28 16:43:05 -05:00
|
|
|
( x / y )
|
2021-12-27 01:20:24 -05:00
|
|
|
@div32 ( x** y** -> q** )
|
|
|
|
( store y and x for repeated use )
|
|
|
|
;div32/div1 STA2 ;div32/div0 STA2 ( y -> div )
|
|
|
|
;div32/rem1 STA2 ;div32/rem0 STA2 ( x -> rem )
|
|
|
|
|
|
|
|
( if x < y then the answer is 0 )
|
|
|
|
;div32/rem0 LDA2 ;div32/rem1 LDA2
|
|
|
|
;div32/div0 LDA2 ;div32/div1 LDA2
|
|
|
|
;lt32 JSR2 ,&is-zero JCN ,¬-zero JMP
|
|
|
|
&is-zero
|
|
|
|
#0000 #0000 RTN
|
|
|
|
|
|
|
|
( x >= y so the answer is >= 1 )
|
|
|
|
¬-zero
|
|
|
|
#0000 ;div32/quo0 STA2 #0000 ;div32/quo1 STA2 ( 0 -> quo )
|
|
|
|
|
|
|
|
( bitcount[x] - bitcount[y] determines the largest multiple of y to try )
|
|
|
|
;div32/rem0 LDA2 ;div32/rem1 LDA2 ;bitcount32 JSR2 ( rbits^ )
|
|
|
|
;div32/div0 LDA2 ;div32/div1 LDA2 ;bitcount32 JSR2 ( rbits^ dbits^ )
|
|
|
|
SUB ( shift=rbits-dits )
|
|
|
|
#00 DUP2 ( shift 0 shift 0 )
|
|
|
|
|
|
|
|
( 1<<shift -> cur )
|
|
|
|
#0000 #0001 ROT2 POP
|
|
|
|
;left-shift JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2
|
|
|
|
|
|
|
|
( div<<shift -> div )
|
|
|
|
;div32/div0 LDA2 ;div32/div1 LDA2 ROT2 POP
|
|
|
|
;left-shift JSR2 ;div32/div1 STA2 ;div32/div0 STA2
|
|
|
|
|
|
|
|
&loop
|
|
|
|
( if rem >= the current divisor, we can subtract it and add to quotient )
|
|
|
|
,&rem0 LDR2 ,&rem1 LDR2 ,&div0 LDR2 ,&div1 LDR2 ;lt32 JSR2 ( rem<div? )
|
|
|
|
,&rem-lt JCN ( if rem < div skip this iteration )
|
|
|
|
|
|
|
|
( if rem >= div, then we have found a multiple of y that divides x )
|
|
|
|
,&rem0 LDR2 ,&rem1 LDR2 ,&div0 LDR2 ,&div1 LDR2 ;sub32 JSR2 ,&rem1 STR2 ,&rem0 STR2 ( rem -= div )
|
|
|
|
,&quo0 LDR2 ,&quo1 LDR2 ,&cur0 LDR2 ,&cur1 LDR2 ;add32 JSR2 ,&quo1 STR2 ,&quo0 STR2 ( quo += cur )
|
|
|
|
|
|
|
|
&rem-lt
|
|
|
|
,&div0 LDR2 ,&div1 LDR2 #01 ;right-shift JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 )
|
|
|
|
,&cur0 LDR2 ,&cur1 LDR2 #01 ;right-shift JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 )
|
|
|
|
,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done )
|
|
|
|
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
|
2021-12-27 15:09:49 -05:00
|
|
|
RTN
|
2021-12-24 16:33:59 -05:00
|
|
|
[ &div0 $2 &div1 $2
|
|
|
|
&rem0 $2 &rem1 $2
|
|
|
|
&quo0 $2 &quo1 $2
|
2021-12-27 01:20:24 -05:00
|
|
|
&cur0 $2 &cur1 $2 ]
|