There's this routine from z80 heaven that calculates l mod 3:
Code:
It always takes at least 108 cycles, and it's hard for me to believe that a routine that has only three possible outputs can take that long. Since I'm new to assembly, I've been unable to save a single cycle off of this without a LUT. Is there a significantly faster mod-3 routine for the z80 in, say, less than 64 bytes?
Quote:
Code:
;Outputs:
; HL is preserved
; A is the remainder
; destroys DE,BC
; z flag if divisible by 3, else nz
ld bc,030Fh
;Now we need to add the upper and lower nibble in a
ld a,l
and c
ld e,a
ld a,l
rlca
rlca
rlca
rlca
and c
add a,e
sub c
jr nc,$+3
add a,c
;add the lower half nibbles ;at this point, we have l_mod_15
ld d,a
sra d
sra d
and b
add a,d
sub b
ret nc
add a,b
ret
;at most 117 cycles, at least 108, 28 bytes
It always takes at least 108 cycles, and it's hard for me to believe that a routine that has only three possible outputs can take that long. Since I'm new to assembly, I've been unable to save a single cycle off of this without a LUT. Is there a significantly faster mod-3 routine for the z80 in, say, less than 64 bytes?