]> git.sur5r.net Git - cc65/blob - libsrc/runtime/ludiv.s
replaced multiple lines of "rm" commands with a single "rm" line in the
[cc65] / libsrc / runtime / ludiv.s
1 ;
2 ; Ullrich von Bassewitz, 17.08.1998
3 ;
4 ; CC65 runtime: division for long unsigned ints
5 ;
6
7         .export         tosudiveax, getlop, udiv32
8         .import         addysp1
9         .importzp       sp, sreg, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
10
11 tosudiveax:
12         jsr     getlop          ; Get the paramameters
13         jsr     udiv32          ; Do the division
14         lda     ptr1            ; Result is in ptr1:sreg
15         ldx     ptr1+1
16         rts
17
18 ; Pop the parameters for the long division and put it into the relevant
19 ; memory cells. Called from the signed divisions also.
20
21 getlop: sta     ptr3            ; Put right operand in place
22         stx     ptr3+1
23         lda     sreg
24         sta     ptr4
25         lda     sreg+1
26         sta     ptr4+1
27
28         ldy     #0              ; Put left operand in place
29         lda     (sp),y
30         sta     ptr1
31         iny
32         lda     (sp),y
33         sta     ptr1+1
34         iny
35         lda     (sp),y
36         sta     sreg
37         iny
38         lda     (sp),y
39         sta     sreg+1
40         jmp     addysp1         ; Drop parameters
41
42 ; Do (ptr1:sreg) / (ptr3:ptr4) --> (ptr1:sreg), remainder in (ptr2:tmp3:tmp4)
43 ; This is also the entry point for the signed division
44
45 udiv32: lda     #0
46         sta     ptr2+1
47         sta     tmp3
48         sta     tmp4
49 ;       sta     ptr1+1
50         ldy     #32
51 L0:     asl     ptr1
52         rol     ptr1+1
53         rol     sreg
54         rol     sreg+1
55         rol     a
56         rol     ptr2+1
57         rol     tmp3
58         rol     tmp4
59
60 ; Do a subtraction. we do not have enough space to store the intermediate
61 ; result, so we may have to do the subtraction twice.
62
63         pha
64         cmp     ptr3
65         lda     ptr2+1
66         sbc     ptr3+1
67         lda     tmp3
68         sbc     ptr4
69         lda     tmp4
70         sbc     ptr4+1
71         bcc     L1
72
73 ; Overflow, do the subtraction again, this time store the result
74
75         sta     ptr4+1          ; We have the high byte already
76         pla
77         sbc     ptr3            ; byte 0
78         pha
79         lda     ptr2+1
80         sbc     ptr3+1
81         sta     ptr2+1          ; byte 1
82         lda     tmp3
83         sbc     ptr4
84         sta     tmp3            ; byte 2
85         inc     ptr1            ; Set result bit
86
87 L1:     pla
88         dey
89         bne     L0
90         sta     ptr2
91         rts
92
93