https://www.cemetech.net/forum/viewtopic.php?t=12833&start=0 fast modulo 10 ; This routine calculates 32-bit modulo 10, by first calculating HLDE mod 15. (25 bytes, 109cc ;Inputs: ; HL:DE 32-bit unsigned integer ;Outputs: ; A = HLDE mod 10 ; Z flag is set if divisible by 10 ;Destroys: ; HL ; 25 bytes, 109cc dwMod10: add hl,de ;add words, n mod 65535 (+1) ld a,h ;add bytes adc a,l adc a,0 ;n mod 255 (+1) ld h,a ;add nibbles rrca / rrca / rrca / rrca add a,h adc a,0 ;n mod 15 (+1) in both nibbles daa ld h,a sub e ; Test if quotient is even or odd rra sbc a,a and 5 add a,h daa and 0Fh ret ; This is the 16-bit version (24 bytes, 98cc). Comments in the routine shows changes for an 8-bit version (20 bytes, 83cc). ;Inputs: ; HL 16-bit unsigned integer ;Outputs: ; A = HL mod 10 ; Z flag is set if divisible by 10 ;Destroys: ; HL ; 24 bytes, 98cc wMod10: ld a,h ;add bytes add a,l adc a,0 ;n mod 255 (+1) ; -- 8-bit version (bMod10) starts here with input in A. 20 bytes, 83cc : -- Note that H and L must be swapped in the last part. ld h,a ;add nibbles rrca / rrca / rrca / rrca add a,h adc a,0 ;n mod 15 (+1) in both nibbles daa ld h,a ; -- h -> l for 8-bit sub l ; -- l -> h for 8-bit. Test if quotient is even or odd rra sbc a,a and 5 add a,h ; -- h -> l for 8-bit daa and 0Fh ret u16 / 5: https://comp.os.cpm.narkive.com/iupP3HdG/z80-divide-by-five Hi Jack It isn't about code that works, it is about running code as fast as the processor can go. I believe your code is fine generic code but it isn't the fastest for dividing by 5. I've written a version that does 16 bits since no one it happy with only 8 bits. If you find your code, you can run a speed test. Here is unsigned 16 bits divided by 5: Input in hl result in de xor a push hl add hl,hl adc a,#0 pop de add hl de adc a,#0 push hl ld c,a add hl,hl adc a,a add hl,hl adc a,a add hl,hl adc a,a add hl,hl adc a,a pop de add hl,de adc a,c ; ahl = ahl<<4 + ahl ld c,a ; c=a ld d,a ld e,h ; de = ahl>>8 add hl,de \ adc a,#0 ; ahl += de (ahl>>8) ld b,0 add hl,bc \ adc a,#0 ; ahl += c (ahl>>16) ld c,#2 add hl,bc \ adc a,#0 ; ahl += 2 (magic smoke) ld d,a ld e,h Later Dwight divide by 5 is the same as multiply with 0.0011001100110011 or multiply with 0011001100110011 and take the upper u16 of the u32 result. ahl = hl<<1 + hl ahl = ahl<<4 + ahl hlxx = ahl<<8 + ahl + ah + a + ... => hlxx = ahl0 + 0ahl + 00ah + 000a + ... xor a ld de,hl add hl,hl \ adc a add hl,de \ adc a,0 ahl = ahl*3 ld xl,a ld de,hl add hl,hl \ adc a add hl,hl \ adc a add hl,hl \ adc a add hl,hl \ adc a add hl,de \ adc a,xl ahl = ahl<<4+ahl ld l,h ld h,a add l ld l,a