Hello fellow Commodore-64'er!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Steam

Multiply

oooo
edited March 2019 in Coding
; General 8bit * 8bit = 8bit multiply
; Multiplies "multiplyNum1" by "multiplyNum2" and returns result in .A

; by White Flame (aka David Holz) 20030207
; modified by Olli Oikarinen (aka oo) 20131216

; Input variables:
;   multiplyNum1 (multiplicand)
;   multiplyNum2 (multiplier), should be small for speed
;   Signedness should not matter

; .X and .Y are preserved
; multiplyNum1 and multiplyNum2 get clobbered

; Instead of using a bit counter, this routine ends when multiplyNum2 reaches zero, thus saving iterations.

multiply:
lda #$00
beq multiplyEnterLoop

multiplyDoAdd:
 clc
 adc multiplyNum1

multiplyLoop:
 asl multiplyNum1
multiplyEnterLoop: ;For an accumulating multiply (.A = .A + multiplyNum1*multiplyNum2), set up multiplyNum1 and multiplyNum2, then enter here
 lsr multiplyNum2
 bcs multiplyDoAdd
 bne multiplyLoop

rts

multiplyNum1:
.byte 0
multiplyNum2:
.byte 0
Sign In or Register to comment.