-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmult26.a
More file actions
52 lines (47 loc) · 1.16 KB
/
mult26.a
File metadata and controls
52 lines (47 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
; mult26.a
; 'frantik' from https://www.nesdev.org/wiki/8-bit_Multiply
; with bug fix
;
; 8 bit x 8 bit multiply, 16 bit result
; Average cycles: 278
; 47 bytes
value1 = $02
value2 = $03
ret = $04 ; return value (2 bytes)
temp = $06 ; temp storage
* = $0200
; ***************************************************************************************
; On Entry:
; value1: multiplier
; value2: multiplicand
; On Exit:
; ret: low byte of product
; ret+1: high byte of product
mult
lda #0 ; clear temporary variables
sta ret
sta ret+1
sta temp
lda value2
beq end ; BUGFIX: beq not bne
jmp start
loop
asl value1 ; double first value
rol temp ; using 16bit precision
lsr value2 ; halve second vale
start
lda value2 ;
and #1 ; is new 2nd value an odd number?
beq loop ;
clc ; if so, add new 1st value to running total
lda ret ;
adc value1 ;
sta ret ;
lda ret+1 ;
adc temp ;
sta ret+1 ;
lda value2 ;
cmp #1 ; is 2nd value 1? if so, we're done
bne loop ; otherwise, loop
end
rts ;