BigEd wrote:
Welcome, WynnSmith!
As your input is only barely over a byte in size, I wonder if we could start by comparing and subtracting 384 to get the top bit of the result, and then use the minimal bytesize version to get the rest.
(It's clever to use multiplication for division in the case that multiplication is fast but division is not, but I'm not sure it's always optimal when multiplication is also not fast. In general, a non-restoring division, perhaps with pre-calculated shifted divisors, might be a win.) ...
If there's somewhere between 512 bytes and 1KB available but not more, so 1KB in table is not an option, you could do a trial subtract of $180, and use the result to select from one of two 256 byte tables, one for $100-$17F dividends, and another for the $00-$FF dividends. I think it'd be about 25 clocks if it's an 8bit dividend (because if DIVHI is 0, you can skip the trial subtract), around 45 clocks if you do the trial subtract.
Edit: OK, because 3x$80 is $180, you don't need to do a trial subtract, you can just examine whether bit 1 of of DIVHI is set, or else both bit0 of DIVHI and bit7 of DIVLO. So given two 256 byte tables, RES8BIT if the tabled value is for a dividend of $00-$FF, and RES9BIT if the table value is for a dividend of $100-$17F, I think it is something along the following lines. 43 bytes, and if I am thinking correctly, the RES9BIT table only needs 128 bytes, so it might be possible to fit this in with under 512bytes total ... less if one or more useful routines can be fit in between the divide by three routine and the start of the RES9BIT table at START+$80.
Code:
; Divide by 3, 10bit number guaranteed to have 8bit or lower result
; DIVLO - low 8bits of 10 bit dividend
; DIVHI -- high two bits of 10 bit dividend, bits2-7 clear
; RESULT -- stores result
; Put where you wish, I have a 12 byte "U" area at $34-$3F.
RESULT = $3D
DIVLO = $3E
DIVHI = $3F
DIVBY3 = $0400 ;
RES9BIT = DIVBY3 + $80
RES8BIT = DIVBY3 + $100
* = DIVBY3
LDY DIVHI
LDA #0
BNE +
LDX DIVLO
- ORA RES8BIT,X
STA RESULT
RTS
+ CPY #2
BPL SUBTRACT_180
LDX DIVLO
BMI SUBTRACT_180
-- ORA RES9BIT,X
STA RESULT
RTS
SUBTRACT_180:
LDA DIVLO
SEC
SBC $80
TAX
LDA #$80 ; bit7 is set if we are down here
BCS + ; no borrow
DEY ; borrow
+ DEY ; subtract $100
BEQ - ; If Y=0, use RES8BIT
BRA -- ; otherwise use RES9BIT
* = RES9BIT
!byte $55,$55,$56,$56,$56,$57,$57,$57,$58,$58,$58,$59,$59,$59,$5A,$5A
!byte $5A,$5B,$5B,$5B,$5C,$5C,$5C,$5D,$5D,$5D,$5E,$5E,$5E,$5F,$5F,$5F
!byte $60,$60,$60,$61,$61,$61,$62,$62,$62,$63,$63,$63,$64,$64,$64,$65
!byte $65,$65,$66,$66,$66,$67,$67,$67,$68,$68,$68,$69,$69,$69,$6A,$6A
!byte $6A,$6B,$6B,$6B,$6C,$6C,$6C,$6D,$6D,$6D,$6E,$6E,$6E,$6F,$6F,$6F
!byte $70,$70,$70,$71,$71,$71,$72,$72,$72,$73,$73,$73,$74,$74,$74,$75
!byte $75,$75,$76,$76,$76,$77,$77,$77,$78,$78,$78,$79,$79,$79,$7A,$7A
!byte $7A,$7B,$7B,$7B,$7C,$7C,$7C,$7D,$7D,$7D,$7E,$7E,$7E,$7F,$7F,$7F
* = RES8BIT
!byte 0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5 ; ...
!byte ; ...