; 6502 8-bit × 8-bit → 16-bit unsigned multiply
; -------------------------------------------------
; On entry:
; 00=multiplicand(A);01 = multiplier (B)
; On exit:
; 02=lowbyteofproduct(A×B);03 = high byte of product
; All registers are free to be clobbered.
; No other RAM is touched.
.org $0200 ; load address (arbitrary)
mult8: lda #00;clearhighbyteofresultsta03
lda 01 ; Y = multiplier (loop counter)
beq done ; if B = 0, product = 0
ldx #$00 ; X = low byte of partial product (will become $02)
loop: cpy #00;whileY=0beqstorelsr01 ; shift multiplier right → carry = next bit
bcc skip_add ; if carry clear, skip addition
clc
txa ; add current multiplicand to partial product
adc 03 ; add carry to high byte
adc #03
skip_add: asl 03 ; propagate carry into high byte
dey
bne loop
store: stx $02 ; store low byte of result
done: rts
; -------------------------------------------------
; Assembled size: 34 bytes
; Cycles (worst case, B = 255): ~1,300
; -------------------------------------------------