]> git.sur5r.net Git - cc65/commitdiff
Special cased the division 16 by 8.
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Wed, 19 Aug 2009 14:51:16 +0000 (14:51 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Wed, 19 Aug 2009 14:51:16 +0000 (14:51 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@4043 b7a2c559-68d2-44c3-8de9-860c34a00d81

libsrc/runtime/udiv.s

index e82d5bf21c7398caae87ab629959afe365852310..03151f94c669e759b7c5d00612f2b4e5ebbd6859 100644 (file)
@@ -5,11 +5,12 @@
 ;
 
                .export         tosudiva0, tosudivax, udiv16
-       .import         popsreg
-       .importzp       sreg, ptr1, ptr4
+       .import         popsreg
+       .importzp       sreg, ptr1, ptr4
+
 
 tosudiva0:
-               ldx     #0
+        ldx     #$00            ; Clear high byte
 tosudivax:
                sta     ptr4
                stx     ptr4+1          ; Save right operand
@@ -25,30 +26,61 @@ tosudivax:
        ldx     sreg+1
        rts
 
-; Do (sreg/ptr4) -> sreg (see mult-div.s from "The Fridge").
+;---------------------------------------------------------------------------
+; 16by16 division. Divide sreg by ptr4. Result is in sreg, remainder in ptr1
+; (see mult-div.s from "The Fridge").
 ; This is also the entry point for the signed division
 
 udiv16:        lda     #0
        sta     ptr1+1
        ldy     #16
+        ldx     sreg+1
+        beq     udiv16by8a
+
 L0:    asl     sreg
        rol     sreg+1
        rol     a
        rol     ptr1+1
+
        pha
        cmp     ptr4
        lda     ptr1+1
        sbc     ptr4+1
-       bcc     L1
-       sta     ptr1+1
-       pla
-       sbc     ptr4
-       pha
-       inc     sreg
+       bcc     L1
+
+       sta     ptr1+1
+       pla
+       sbc     ptr4
+       pha
+       inc     sreg
+
 L1:    pla
-       dey
-       bne     L0
-       sta     ptr1
-       rts
+       dey
+       bne     L0
+       sta     ptr1
+       rts
+
+
+;---------------------------------------------------------------------------
+; 16by8 division
+
+udiv16by8:
+        lda    #0
+       sta     ptr1+1
+       ldy     #16
+udiv16by8a:
+@L0:   asl     sreg
+       rol     sreg+1
+       rol     a
+        bcs     @L1
+
+       cmp     ptr4
+       bcc     @L2
+@L1:    sbc    ptr4
+       inc     sreg
 
+@L2:   dey
+       bne     @L0
+       sta     ptr1
+       rts