Newsgroups: comp.sys.apple2.programmer
Path: news.uiowa.edu!news.uiowa.edu!chi-news.cic.net!brutus.bright.net!usenet.eel.ufl.edu!news.mathworks.com!tank.news.pipex.net!pipex!howland.reston.ans.net!vixen.cso.uiuc.edu!news.uoregon.edu!waikato!comp.vuw.ac.nz!actrix.gen.nz!dempson
From: dempson@atlantis.actrix.gen.nz (David Empson)
Subject: Re: wanted...code
Message-ID:
Sender: news@actrix.gen.nz (News Administrator)
Organization: Actrix - Internet Services
Date: Tue, 19 Sep 1995 12:14:06 GMT
References: <43et0h$gfg@styx.uwa.edu.au> <43fjnc$q0s@opal.southwind.net>
X-Nntp-Posting-Host: atlantis.actrix.gen.nz
Lines: 119
In article <43fjnc$q0s@opal.southwind.net>,
Randy Shackelford wrote:
> Joseph Turner (jturner@tartarus.uwa.edu.au) wrote:
>
> : 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.
>
> There's one in Eyes' and Lichty's Programming the 65816, but my copy is in a
> box in the closet. Maybe someone has their copy more handy and can post it.
It only produces a 16-bit result, and cannot be adapted directly to
produce a 32-bit result. I have rewritten it, using a similar style.
I've added some general comments on points of interest in the algorithm.
; 16-bit by 16-bit multiply for the 6502, producing a 32-bit result.
; operand 1: sixteen bits in locations MCAND1/MCAND1+1
; operand 2: sixteen bits in locations MCAND2/MCAND2+1
; result: thirty-two bits in locations RESULT/RESULT+1/RESULT+2/RESULT+3
;
; MCAND1/MCAND1+1 are destroyed. All registers are destroyed.
;
; No 65C02 instructions are used, so this will work on any Apple II.
; Ideally, the variables should be placed in zero page.
MULT START
lda #0 ; initialize result high word
sta RESULT+2 ; (low byte of high word)
sta RESULT+3 ; (high byte of high word)
; There is no need to initialize the low word, as the result gets shifted
; into it one bit at a time, and the original contents are never used.
ldx #16 ; bit count
MULT1 lsr MCAND1+1 ; get least significant bit
ror MCAND ; by doing a sixteen bit shift-right
bcc MULT2 ; if bit is clear, no addition needed
clc
lda RESULT+2 ; otherwise add operand 2 to partial result
adc MCAND2 ; (add to the high word, which gets shifted down)
sta RESULT+2
lda RESULT+3
adc MCAND2+1
sta RESULT+3
; If there is carry from this addition, it will be left in the carry
; flag, and will be rotated into the result by the following code.
MULT2 ror RESULT+3 ; shift the result down by one bit
ror RESULT+2 ; (initial carry is 0 if no addition was made,
ror RESULT+1 ; or carry from high byte of addition)
ror RESULT
; There will never be a useful shift-out at this point - one bit is
; shifted into the top of the low word for each loop, so the low word
; of the result is fully used after 16 shifts.
dex
bne MULT1 ; back around sixteen times
; After 16 shifts, the potential addition for the least significant bit
; of MCAND1 will have shifted down to the bottom bit of RESULT, giving
; the correct answer.
rts
END
For comparison, here is a 65816 version, assuming 16-bit native mode.
MULT START
lda #0 ; initialize result high word
; The high word of the partial result is left in A until the end of the
; algorithm.
; There is no need to initialize the low word, as the result gets shifted
; into it one bit at a time, and the original contents are never used.
ldx #16 ; bit count
MULT1 lsr MCAND1 ; get least significant bit
bcc MULT2 ; if bit is clear, no addition needed
clc
adc MCAND2 ; otherwise add operand 2 to partial result
; If there is carry from this addition, it will be left in the carry
; flag, and will be rotated into the result by the following code.
MULT2 ror A ; shift the result down by one bit
ror RESULT
; There will never be a useful shift-out at this point - one bit is
; shifted into the top of the low word for each loop, so the low word
; of the result is fully used after 16 shifts.
dex
bne MULT1 ; back around sixteen times
; After 16 shifts, the potential addition for the least significant bit
; of MCAND1 will have shifted down to the bottom bit of RESULT, giving
; the correct answer.
sta RESULT+2 ; store the high word
rts
END
--
David Empson
dempson@actrix.gen.nz
Snail mail: P.O. Box 27-103, Wellington, New Zealand