Skip to content

Instantly share code, notes, and snippets.

@djmips
Created April 10, 2017 10:21
Show Gist options
  • Save djmips/99e41ede86708899198f4ba64956ed73 to your computer and use it in GitHub Desktop.
Save djmips/99e41ede86708899198f4ba64956ed73 to your computer and use it in GitHub Desktop.
6502 unwound unsigned 8x8->16 multiply
mul1 * mul2 + A/2 -> A:mul1 (mul2 is unchanged)
; inner loop credit Supercat
MUL:
dec mul2 ;5 ; decrement mul2 because we will be adding with carry set for speed (an extra one)
ror mul1 ;5 \
bcc b1 ;2/3 \ Best case 8 Worst case 10
adc mul2 ;3 /
b1: ror ;2 \
ror mul1 ;5 \
bcc b2 ;2/3 / Best case 10 Worst case 12
adc mul2 ;3 /
b2: ror
ror mul1
bcc b3
adc mul2 ; 10 or 12
b3: ror
ror mul1
bcc b4
adc mul2 ; 10 or 12
b4: ror
ror mul1
bcc b5
adc mul2 ; 10 or 12
b5: ror
ror mul1
bcc b6
adc mul2 ; 10 or 12
b6: ror
ror mul1
bcc b7
adc mul2 ; 10 or 12
b7: ror
ror mul1
bcc b8
adc mul2 ; 10 or 12
b8: ror ; 2
ror mul1 ; 5
inc mul2 ; 5
; total cycles worst case = 7 + 10 + (7 * 12) + 12 = 113
; total cycles best case = 7 + 8 + (7*10) + 12 = 97
; avg = 105
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment