Path: news.uiowa.edu!news.uiowa.edu!uunet!in2.uu.net!usc!howland.reston.ans.net!vixen.cso.uiuc.edu!news.uoregon.edu!cie-2.uoregon.edu!nparker From: nparker@cie-2.uoregon.edu (Neil Parker) Newsgroups: comp.sys.apple2.programmer Subject: Re: wanted...code Date: 18 Sep 1995 09:38:14 GMT Organization: University of Oregon Campus Information Exchange Lines: 58 Message-ID: <43jem6$6aq@pith.uoregon.edu> References: <43et0h$gfg@styx.uwa.edu.au> NNTP-Posting-Host: cie-2.uoregon.edu In article <43et0h$gfg@styx.uwa.edu.au> jturner@tartarus.uwa.edu.au (Joseph Turner) writes: > G'day, I am doing some machine language programming on my apple2c and I >have suddenly come across the need for a 2 byte multiplier(not FP) ie so >the answer is a 4 byte number. I have been stressing my brains out trying >to get one working by adapting the normal multiplication routine to two >bytes, but it is deciding to give me nothing but trouble. Here's a 16-bit multiplication routine: ; ; Necessary variables ; We can put these anywhere, but zero page is best for speed. ; A1L EQU $3C ;1st multiplicand A1H EQU $3D A2L EQU $3E ;2nd multiplicand & low bytes of product A2H EQU $3F A3L EQU $40 ;high bytes of product A3H EQU $41 ; ; Here's the routine. ; Entry: 1st multiplicand in A1L and A1H ; 2nd multiplicand in A2L and A2H ; Exit: Product in A2L (low byte) through A3H (high byte) ; 2nd multiplicand is destroyed ; MULT16 LDA #0 ;Clear product STA A3L STA A3H LDY #$10 ;Shift 16 bits LOOP LDA A2L ;Get low bit of 2nd multiplicand LSR ;(Some assemblers require "LSR A" here) BCC SHIFT ;If 0, skip addition CLC ;Otherwise add 1st multiplicand to product LDA A1L ADC A3L STA A3L LDA A1H ADC A3H STA A3H SHIFT ROR A3H ;Shift product (& 1st multiplicand) down ROR A3L ROR A2H ROR A2L DEY ;Done enough bits yet? BNE LOOP ;If not, go back for more RTS ;Else we're done. Beware--I typed this in directly from my terminal, without first assembling it and trying it out on my Apple. - Neil Parker -- Neil Parker, nparker@{cie-2,cie}.uoregon.edu, http://cie-2.uoregon.edu/~nparker "Evolution is vastly overrated." -- Ambassador Delenn, _Babylon_5_