Division

The eZ8 does not have a division instruction. To overcome this you need to use a division subroutine.

8 Bit division routine

Here is an example of a 8/8 bits division routine.

;Routine: div8
; R4 = R0/R1
; remainder: R2
    SCOPE
div8:
    ld R2,#0
    ld R3,#8
$loop:
    rlc R0
    rlc R2        ; Shift R0 into R2
    cp R1,R2    ; If R2 is less than...
    jr nz,$F    ; or equal to R1            (if Z : set C)
    scf
$$:
    rlc R4
    cp R1,R2
    jr ugt,$F
    sub R2,R1  ; Subtract R1 from R2
$$:
    djnz R3,$loop
    ret

16 Bit division routine

This is an example of a 16/16 bits division routine. On division by 0 the quotient is 0 and the remainder will be equal to the dividend. Keep this in mind when using this routine.

;Routine: div16
;RR6 = RR0 / RR2
;remainder: RR4
;uses R0-7 and R15
    SCOPE
div16:
    ld R4,#0
    ld R5,#0
    ld R15,#16
$loop:
    rlc R1
    rlc R0
    rlc R5
    rlc R4
    cp R3,R5
    cpc R2,R4
    jr nz,$F
    scf
$$:
    rlc R7
    rlc R6
    cp R3,R5
    cpc R2,R4
    jr ugt,$F
    sub R5,R3
    sbc R4,R2
$$:
    djnz R15,$loop
    ret

Please submit corrections, suggestions, and new documentation here: Submit a ticket
← Return to Main Site
This is not an official Zilog website. Zilog nor 8times8 are liable for any damage done to equipment. Tutorial and reference copyright ©8times8 Creative Commons CC-0 License (public domain.) This reference uses content from Zilog's technical datasheets. Fair use only. Datasheets copyright © 2013 Zilog®, Inc. Z8, Z8 Encore!, Z8 Encore! XP and Z8 Encore! MC are trademarks or registered trademarks of Zilog, Inc. Read Zilog's terms at zilog.com

Website design by Koen van Vliet. Hosted by sourceforge.net