]> git.sur5r.net Git - cc65/blob - libsrc/runtime/idiv32by16r16.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / runtime / idiv32by16r16.s
1 ;
2 ; Ullrich von Bassewitz, 2009-11-04
3 ;
4 ; CC65 runtime: 32by16 => 16 signed division
5 ;
6
7         .export         idiv32by16r16
8         .import         negax, udiv32by16r16m
9
10         .include        "zeropage.inc"
11
12
13 ;---------------------------------------------------------------------------
14 ; 32by16 division. Divide ptr1:ptr2 by ptr3. Result is in ptr1, remainder
15 ; in sreg.
16 ;
17 ;   lhs         rhs           result      result also in    remainder
18 ; -----------------------------------------------------------------------
19 ;   ptr1:ptr2   ptr3          ax          ptr1              sreg
20 ;
21
22
23 idiv32by16r16:
24         stx     tmp1
25         cpx     #0
26         bpl     @L1
27         jsr     negax
28 @L1:    sta     ptr3
29         stx     ptr3+1
30
31         lda     ptr2+1
32         eor     tmp1
33         sta     tmp1
34         bit     ptr2+1
35         bpl     @L3
36
37 ; Negate the value in ptr1:ptr2
38
39         ldx     #0
40         ldy     #4
41         sec
42 @L2:    lda     ptr1,x
43         eor     #$FF
44         adc     #$00
45         sta     ptr1,x
46         inx
47         dey
48         bne     @L2
49
50 ; Call the unsigned division routine
51
52 @L3:    jsr     udiv32by16r16m
53
54 ; Check the sign of the result
55
56         bit     tmp1
57         bmi     @L4
58         rts
59
60 ; Negate the result. We do this here only for the result, not for the
61 ; remainder!
62
63 @L4:    jmp     negax
64