"Mark Percival" <mark@syndicomm.com> = wrote in=20 message news:lmlk31p6td4hvu0bcf5fe88bip9ope7vn9@4ax.com...

> = Laine,=20 thanks for the source code.  I probably won't use it in this=20 program
> but I am going to keep it for future.  = Multiplication and=20 division is
> always a problem in 6502 assembly code.
> =
>=20 Mark R. Percival - RTC Host - Syndicomm Online Apple II Forum
> = "Midweek=20 Madness!" Every Wednesday Night : 7:00 PM - 9:00 PM Pacific Time
> =
>
http://www.syndicomm.com

Mark it is handy.
Since I was already that far along I went = ahead and=20 assembled and tested the routines.

Just jump down to the bottom and cut them=20 out.

=     =20 LIST=20 OFF
=    =20 KEEP TEST

* TEST MULTIPLY AND=20 DIVIDE
***************************************
TESTDRIVE  = ;   =20 START

&n= bsp;  =20 USING=20 COMMON
&nb= sp;   =20 MSB  ON

PRINTBYTE      GEQU=20 \$FDDA
HOME          = GEQU=20 \$FC58
COUT          = GEQU=20 \$FDED
HDVND1         = EQU =20 \$EB            = ;EC

&n= bsp;  =20 JSR =20 HOME
= ;   =20 JSR  ZEROALL

&n= bsp;  =20 LDA =20 #\$45
= ;   =20 STA =20 MULTIPLIER
= ;    =20 LDA =20 #\$23
= ;   =20 STA =20 MULTIPLIER+1
&nb= sp;    =20 LDA =20 #\$01
= ;   =20 STA  MULTIPLIER+2

&n= bsp;  =20 LDA =20 #\$34
= ;   =20 STA =20 MULTIPLICAND
&nb= sp;    =20 LDA =20 #\$12
= ;   =20 STA  MULTIPLICAND+1

&n= bsp;  =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'Multiply \$12345 * \$1234',H'8D 00'

&n= bsp;  =20 JSR =20 MULTIPLY
&= nbsp;   =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'Result =3D=20 \$',H'00'
&= nbsp;   =20 LDX  = LENGTH
PBMLP         =20 DEX
=    =20 BMI =20 RETURN
&nb= sp;   =20 LDA =20 MULTIPLIER,X
&nb= sp;    =20 JSR =20 PRINTBYTE
=     =20 CLC
=    =20 BCC  = PBMLP
RETURN        =20 JSR =20 SAYIT
&nbs= p;   =20 DC   H'8D8D00'

&n= bsp;  =20 JSR =20 ZEROALL
&n= bsp;   =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'Divide 0 / 0 ',H'8D=20 00'
=    =20 JSR  DIVIDE         ;TEST = DIVISION=20 BY=20 0
&n= bsp;  =20 BCC  = MORE          =20 ;NOT!, FALL=20 THROUGH
&n= bsp;   =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'Division by zero error.',H'8D 8D=20 00'
MORE          =20 LDA =20 #\$04
= ;   =20 STA =20 DIVIDEND
&= nbsp;   =20 LDA =20 #\$04
= ;   =20 STA =20 DIVIDEND+1
= ;    =20 LDA =20 #\$B6
= ;   =20 STA =20 DIVIDEND+2
= ;    =20 LDA =20 #\$14
= ;   =20 STA  DIVIDEND+3

&n= bsp;  =20 LDA =20 #\$34
= ;   =20 STA =20 DIVISOR
&n= bsp;   =20 LDA =20 #\$12
= ;   =20 STA  DIVISOR+1

&n= bsp;  =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'Divide \$14B60404 / \$1234',H'8D=20 00'
=    =20 JSR =20 DIVIDE
&nb= sp;   =20 BCS =20 ERROR
&nbs= p;   =20 JSR =20 SAYIT
&nbs= p;   =20 DC   C'   Result =3D=20 \$',H'00'
&= nbsp;   =20 LDX =20 LENGTH
PBLP          = ;=20 DEX
=    =20 BMI =20 RMDR
= ;   =20 LDA =20 DIVIDEND,X
= ;    =20 JSR =20 PRINTBYTE
=     =20 CLC
=    =20 BCC =20 PBLP
RMDR           = JSR =20 SAYIT
&nbs= p;   =20 DC   H'8D',C'Remainder =3D=20 \$',H'00'
&= nbsp;   =20 LDY  = LENGTH
PB1LP         =20 DEY
=    =20 BMI =20 PBDONE
&nb= sp;   =20 LDA =20 (HDVND1),Y
= ;    =20 JSR =20 PRINTBYTE
=     =20 CLC
=    =20 BCC  PB1LP

PBDONE        =20 RTS
ERROR         =20 BRK           &nbs= p;    =20 ;CRASH AND DIE, SHOULDN'T HAPPEN

&n= bsp;  =20 MSB  OFF

&n= bsp;  =20 END
***************************************

*ZERO DATA=20 BUFFERS
***************************************
ZEROALL  =      =20 START

&n= bsp;  =20 USING COMMON

&n= bsp;  =20 LDA =20 #<LENGTH
&nbs= p;    =20 SEC
=    =20 SBC =20 #<ARRAY1
&nbs= p;    =20 TAX
=    =20 DEX
=    =20 LDA  #0
ZEROLP         = STA =20 ARRAY1,X
&= nbsp;   =20 DEX
=    =20 BPL =20 ZEROLP
&nb= sp;   =20 RTS

&n= bsp;  =20 END
***************************************

*TEXT=20 PRINTER
***************************************
SAYIT  &n= bsp;      =20 START

RTN           = ;=20 EQU  \$06

&n= bsp;  =20 PLA           &nbs= p;    =20 ;GET RTN=20 ADDRESS
&n= bsp;   =20 CLC
=    =20 ADC =20 #1            = ;=20 ;CORRECT=20 RTN
=    =20 STA  = RTN           =20 ;USE AS=20 INDEX
&nbs= p;   =20 PLA           &nbs= p;    =20 ;TO=20 DATA
= ;   =20 ADC =20 #0
&= nbsp;  =20 STA  RTN+1

&n= bsp;  =20 LDX =20 #0
&= nbsp;  =20 LDY =20 #0            = ; ;=20 PRINT THE=20 MSG
LOOP          =20 LDA =20 (RTN),Y
&n= bsp;   =20 BEQ =20 DONE
= ;   =20 JSR =20 COUT
= ;   =20 INX
=    =20 INY
=    =20 BNE =20 LOOP
DONE           = TXA
=    =20 CLC
=    =20 TAX
=    =20 LDA =20 RTN+1
&nbs= p;   =20 ADC =20 #0
&= nbsp;  =20 PHA
=    =20 TXA
=    =20 PHA
=    =20 RTS

&n= bsp;  =20 END
***************************************

*DATA=20 BUFFERS
***************************************
COMMON  &= nbsp;     =20 DATA

DIVIDEND      =20 ANOP
MULTIPLIER    =20 ANOP
ARRAY1         = DC  =20 H'00'         =20 ;LSByte
&n= bsp;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC   = H'00'          ;7=20 MSByte

DIVISOR       =20 ANOP
MULTIPLICAND  =20 ANOP
ARRAY2         = DC  =20 H'00'         =20 ;LSByte
&n= bsp;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC  =20 H'00'
&nbs= p;   =20 DC   = H'00'          ;7=20 MSByte

HIGHPROD      =20 ANOP
HIGHDVND1     =20 ANOP
BUFFER1        DS   = 7            =  =20 ;MATCH LENGTH

HIGHDVND2     =20 ANOP
BUFFER2        DS   = 7            =  =20 ;MATCH LENGTH

COUNT          = DS  =20 2            =  =20 ;# OF BITS +1

LENGTH         = DC  =20 H'07'          ;NUMBER OF = BYTES IN=20 ARRAY

&n= bsp;  =20 END
***************************************

*MULTIPLY=20 ROUTINE
***************************************
MULTIPLY  = ;    =20 START

&n= bsp;  =20 USING COMMON

MPLIER         EQU =20 \$FA           =20 ;FB
MCAND          = EQU =20 \$FC            = ;FD

&n= bsp;  =20 LDA =20 #<MULTIPLIER
=      =20 SEC
=    =20 SBC =20 #1            = ;=20 ;INDEX BY 1 NOT=20 0
&n= bsp;  =20 STA =20 MPLIER
&nb= sp;   =20 LDA =20 #>MULTIPLIER
=      =20 SBC =20 #0
&= nbsp;  =20 STA  MPLIER+1

&n= bsp;  =20 LDA =20 #<MULTIPLICAND
&nbs= p;     =20 SEC
=    =20 SBC =20 #1            = ;=20 ;INDEX BY 1 NOT=20 0
&n= bsp;  =20 STA =20 MCAND
&nbs= p;   =20 LDA =20 #>MULTIPLICAND
&nbs= p;     =20 SBC =20 #0
&= nbsp;  =20 STA  MCAND+1

;COUNT =3D LENGTH * 8=20 +1
&= nbsp;  =20 LDA =20 LENGTH
&nb= sp;   =20 STA =20 COUNT
&nbs= p;   =20 LDA =20 #0
&= nbsp;  =20 ASL  COUNT          = ;COUNT=20 *2
&= nbsp;  =20 ROL =20 A            =  =20 ;A WILL BE UPPER=20 BYTE
= ;   =20 ASL  COUNT          = ;COUNT=20 *4
&= nbsp;  =20 ROL =20 A
&n= bsp;  =20 ASL  COUNT          = ;COUNT=20 *8
&= nbsp;  =20 ROL =20 A
&n= bsp;  =20 STA =20 COUNT+1
&n= bsp;   =20 INC  COUNT          = ;YOUR=20 +1
&= nbsp;  =20 BNE =20 ZEROPROD
&= nbsp;   =20 INC  COUNT+1        ;IF LOW BYTE = BECOMES=20 0

;ZERO HIPROD
ZEROPROD       = LDX =20 LENGTH
&nb= sp;   =20 LDA  = #0
ZROLP         =20 STA  HIGHPROD-1,X   ;-1 FOR INDEXING FROM=20 1
&n= bsp;  =20 DEX
=    =20 BNE  ZROLP

; ARRAY1 WILL BE THE MULTIPLIER = AND ARRAY2=20 THE MULTIPLICAND

&n= bsp;  =20 CLC           &nbs= p;    =20 ;FIRST TIME THROUGH

;SHIFT CARRY INTO HIPROD AND THE LEAST SIGNIFICANT
; BIT OF = HIPROD INTO=20 CARRY

LOOP           = LDX =20 LENGTH
ROLLLP         = ROR =20 HIGHPROD-1,X
&nb= sp;    =20 DEX
=    =20 BNE  ROLLLP         ;TILL X = =3D=20 0

;SHIFT CARRY WHICH IS THE NEXT BIT OF THE LOWER PRODUCT INTO THE = MOST
;=20 SIGNIFICANT BIT OF ARRAY1. THIS IS NEXT BIT OF THE PRODUCT,
; THIS = ALSO=20 SHIFTS THE NEXT BIT OF THE MULTIPLIER TO CARRY

&n= bsp;  =20 LDY  LENGTH
SHIFTLP        = LDA =20 (MPLIER),Y
= ;    =20 ROR =20 A
&n= bsp;  =20 STA =20 (MPLIER),Y
= ;    =20 DEY
=    =20 BNE  SHIFTLP        ;TILL = Y=3D0

;IF THE NEXT BIT OF THE MULTIPLIER IS 1 THEN
&nb= sp;   =20 BCC  DECCNT         ;BRANCH = IF NEXT=20 BIT IS 0

&n= bsp;  =20 LDY =20 #1
&= nbsp;  =20 LDX =20 LENGTH
&nb= sp;   =20 CLC
&nb= sp;    =20 STA =20 HIGHPROD-1,Y
&nb= sp;    =20 INY
=    =20 DEX

;DEC BIT COUNT AND EXIT IF DONE
; CARRY=20 UNCHANGED
DECCNT         = DEC =20 COUNT
&nbs= p;   =20 BNE =20 LOOP
= ;   =20 LDX =20 COUNT+1
&n= bsp;   =20 BEQ =20 DONE
= ;   =20 DEX
=    =20 STX =20 COUNT+1
&n= bsp;   =20 JMP  LOOP

DONE           = RTS

&n= bsp;  =20 END
***************************************

*DIVIDE=20 ROUTINE
***************************************
DIVIDE  &= nbsp;     =20 START

&n= bsp;  =20 USING COMMON

DVND           = EQU =20 \$FA           =20 ;FB
DVSR          =20 EQU  = \$FC           =20 ;FD
HDVND1         EQU =20 \$EB           =20 ;EC
HDVND2         EQU =20 \$ED            = ;EE

&n= bsp;  =20 LDA =20 #<DIVISOR
&nb= sp;    =20 STA =20 DVSR
= ;   =20 LDA =20 #>DIVISOR
&nb= sp;    =20 STA  DVSR+1

&n= bsp;  =20 LDA =20 #<DIVIDEND
&n= bsp;    =20 STA =20 DVND
= ;   =20 LDA =20 #>DIVIDEND
&n= bsp;    =20 STA  DVND+1

&n= bsp;  =20 LDA =20 #<HIGHDVND1
&= nbsp;    =20 STA =20 HDVND1
&nb= sp;   =20 LDA =20 #>HIGHDVND1
&= nbsp;    =20 STA  HDVND1+1

&n= bsp;  =20 LDA =20 #<HIGHDVND2
&= nbsp;    =20 STA =20 HDVND2
&nb= sp;   =20 LDA =20 #>HIGHDVND2
&= nbsp;    =20 STA  HDVND2+1

;COUNT =3D LENGTH * 8=20 +1
&= nbsp;  =20 LDA =20 LENGTH
&nb= sp;   =20 STA =20 COUNT
&nbs= p;   =20 LDA =20 #0
&= nbsp;  =20 ASL  COUNT          = ;COUNT=20 *2
&= nbsp;  =20 ROL =20 A            =  =20 ;A WILL BE UPPER=20 BYTE
= ;   =20 ASL  COUNT          = ;COUNT=20 *4
&= nbsp;  =20 ROL =20 A
&n= bsp;  =20 ASL  COUNT          = ;COUNT=20 *8
&= nbsp;  =20 ROL =20 A
&n= bsp;  =20 STA =20 COUNT+1
&n= bsp;   =20 INC  COUNT          = ;YOUR=20 +1
&= nbsp;  =20 BNE =20 ZEROHIGH
&= nbsp;   =20 INC  COUNT+1        ;IF LOW BYTE = BECOME=20 0

;ZERO HIGH = DIVIDENDS
ZEROHIGH      =20 LDX =20 LENGTH
&nb= sp;   =20 LDA  = #0
ZROHI         =20 STA  HIGHDVND1-1,X  ;-1 FOR INDEXING FROM 1 TO=20 LENGTH
&nb= sp;   =20 STA =20 HIGHDVND2-1,X
&n= bsp;    =20 DEX
=    =20 BNE  ZROHI

;CHECK IF DIVISION BY=20 0
&n= bsp;  =20 LDX =20 LENGTH
&nb= sp;   =20 LDY =20 #0
&= nbsp;  =20 TYA
DB0LP          = ORA =20 (DVSR),Y
&= nbsp;   =20 INY
=    =20 DEX
=    =20 BNE =20 DB0LP
&nbs= p;   =20 CMP =20 #0
&= nbsp;  =20 BEQ  ERROR

;DIVIDE USING TRIAL=20 SUBTRACTION
&nbs= p;    =20 CLC           &nbs= p;    =20 ;FIRST TIME THROUGH

;SHIFT CARRY INTO LOWER DIVIDEND ARRAY AS THE NEXT BIT OF = QUOTIENT
; AND=20 THE MOST SIGNIFICANT BIT OF THE LOWER DIVIDEND TO CARRY

LOOP           = LDX =20 LENGTH
&nb= sp;   =20 LDY  #0
SHIFTLP        = LDA =20 (DVND),Y
&= nbsp;   =20 ROL =20 A
&n= bsp;  =20 STA =20 (DVND),Y
&= nbsp;   =20 INY
=    =20 DEX
=    =20 BNE  SHIFTLP

DECCNT         DEC =20 COUNT
&nbs= p;   =20 BNE =20 MORE
= ;   =20 LDX =20 COUNT+1
&n= bsp;   =20 BEQ =20 DONE
= ;   =20 DEX
=    =20 STX  COUNT+1

;SHIFT THE CARRY INTO THE LEAST SIGNIFICANT BIT OF THE UPPER = DIVIDEND

MORE           = LDX =20 LENGTH
&nb= sp;   =20 LDY  #0
SHIFT2LP       LDA =20 (HDVND1),Y
= ;    =20 ROL =20 A
&n= bsp;  =20 STA =20 (HDVND1),Y
= ;    =20 INY
=    =20 DEX
=    =20 BNE  SHIFT2LP

;SUBTRACT ARRAY2 FROM HDVND1 PLACING THE DIFFERENCE INTO=20 HDVND2
&nb= sp;   =20 LDY =20 #0
&= nbsp;  =20 LDX =20 LENGTH
&nb= sp;   =20 SEC
SUBLP          = LDA =20 (HDVND1),Y
= ;    =20 SBC  (DVSR),Y       ;SUBTRACT THE=20 BYTES
&nbs= p;   =20 STA  (HDVND2),Y     ;STORE THE=20 DIFFERENCE
= ;    =20 INY
=    =20 DEX
=    =20 BNE  SUBLP

;IF NO CARRY IS GENERATED FROM THE SUBTRACTION THEN THE = HDVND1
; IS=20 LESS THAN ARRAY 2 SO THE NEXT BIT OF THE QUOTIENT IS 0.
; IF THE = CARRY IS SET=20 THEN THE NEXT BIT OF THE QUOTIENT IS 1
; AND WE REPLACE THE DIVIDEND = WITH=20 REMAINDER BY SWITCHING POINTERS

&nbs= p;   =20 BCC  = LOOP           ;WAS=20 TRIAL SUBTRACTION=20 SUCESSFUL?
= ;    =20 LDY  HDVND1         ;YES, = EXCHANGE=20 POINTERS THUS=20 REPLACING
=     =20 LDX  HDVND1+1       = ;    =20 DIVIDEND WITH=20 REMAINDER
=     =20 LDA =20 HDVND2
&nb= sp;   =20 STA =20 HDVND1
&nb= sp;   =20 LDA =20 HDVND2+1
&= nbsp;   =20 STA =20 HDVND1+1
&= nbsp;   =20 STY =20 HDVND2
&nb= sp;   =20 STX  HDVND2+1
;CONTINUE WITH NEXT BIT A 1 (CARRY =3D=20 1)
&= nbsp;  =20 JMP  LOOP

DONE          =20 CLC
=    =20 BCC  = EXIT
ERROR         =20 SEC
EXIT          =20 RTS

;ARRAY1 IS THE QUOTIENT
;HDVND1 POINTS TO THE REMAINDER

&n= bsp;  =20 END
***************************************
------=_NextPart_000_0031_01C52C6E.4F2E8280--