Re: Old Guy with Special Case Need to Divide by Three
Posted: Sun Feb 25, 2024 5:37 pm
BillG wrote:
BigDumbDinosaur wrote:
Proxy wrote:
honestly with how small the value range is (10 bit input, 8 bit output), i'd just use a table.
I second that. These days, memory is less valuable than time, so I’d go with the table approach for such a specific requirement.
Size and speed are subjective terms, and there will be a wide spectrum of possible solutions, many of which may not be suitable for the task at hand. Here's another ... 22 bytes + rts, 400ish clocks, lightly tested, and the remainder is a free bonus.
Code: Select all
; - - - - - - - - - - - - - - - - - - - - - - - - - - -
; 16-bit unsigned division by 3 routine
; T /= 3, A = remainder, Y = low byte of quotient
;
div3:
lda #0 ; remainder
ldy #16 ; loop counter
div3b:
asl T ; T is gradually replaced
rol T+1 ; with the quotient
rol ; A is gradually replaced
; with the remainder
cmp #3 ; partial remainder >= 3?
bcc div3c
sbc #3 ; yes: update partial
; remainder, set low bit
inc T ; in partial quotient
div3c:
dey
bne div3b ; loop 16 times
ldy T ; quotient:l
rts