Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Created November 8, 2025 14:04
Show Gist options
  • Select an option

  • Save donnaken15/cb3871c082d8ad8946f23a8c1540b089 to your computer and use it in GitHub Desktop.

Select an option

Save donnaken15/cb3871c082d8ad8946f23a8c1540b089 to your computer and use it in GitHub Desktop.
6502: multiply using shift+add, no lookup tables
; done in michal kowalski's 6502 macroassembler & simulator
putc = $e001
puth = $e003
*=$100
lda #$10
ldx #8
jsr pmul
lda #3
ldx #3
jsr pmul
lda #4
ldx #4
jsr pmul
lda #$10
ldx #3
jsr pmul
lda #4
ldx #$20
jsr pmul
lda #$FF
ldx #1
jsr pmul
brk
m_prem ldy #0
sty sad
sty ffs
iny
sty why
dey
; @ start: A=1 X=2
sta die
m_main txa
bit why
beq no_bit
; add shifted var
lda die
ldx sad
m_add asl
dex
bne m_add
adc ffs
sta ffs
no_bit inc sad
asl why
lda why
asl
cmp #0
bne m_main
lda ffs
ldx #2
stx ffs
rts
pmul jsr pax ; print problem
jsr mul ; calc
jsr pv ; print answer
rts
pax sta puth
ldy #'*'
sty putc
stx puth
rts
pv ldy #'='
sty putc
sta puth
ldy #$0D
sty putc
ldy #$0A
sty putc
rts
mul sta why ; can't do a-x directly, why
cpx why ; hi/lo order check
bcs m_lh_o ;
bpl m_hl_o ; WTF
bmi m_hl_o ;
m_lh_o txa
ldx why
m_hl_o cpx #4 ; too simple to waste cycles on
bmi m_ft_i ; there has to be 1 branch
bcc m_prem ; instruction for this
bne m_prem ; instruction for this
m_ft_i tay
txa
asl
asl
sta why
tya
jmp god
*=$1C0
god .db $4C
why .db 0
ffs .db 2
sad .db 0
die .db 0
*=$200
m_ft_0 ; fast ops table
lda #0 ; 0x0
rts ; 0x2
nop ; 0x3
; if * 1
rts
nop
nop
nop
; if * 2
asl
rts
nop
nop
; if * 3
jmp m_ft_3
nop
m_ft_4 asl
asl
rts
nop
; hate me
m_ft_3 tax
asl
stx why
adc why
rts
@donnaken15
Copy link
Author

last edited 9/15/2025

any real world work done on this CPU that was made in assembly is a miracle in itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment