]> git.sur5r.net Git - cc65/blob - libsrc/common/divt.s
un-remove TABs in doc/using-make.sgml
[cc65] / libsrc / common / divt.s
1 ; divt.s
2 ;
3 ; 2002-10-22, Greg King
4 ;
5 ; This signed-division function returns both the quotient and the remainder,
6 ; in this structure:
7 ;
8 ; typedef struct {
9 ;     int rem, quot;
10 ; } div_t;
11 ;
12 ; div_t __fastcall__ div (int numer, int denom);
13 ;
14
15         .export         _div
16
17         .import         tosdivax, negax
18         .importzp       sreg, ptr1, tmp1
19
20 _div:   jsr     tosdivax        ; Division-operator does most of the work
21         
22         ldy     sreg            ; low byte remainder from sreg
23         sta     sreg            ; store low byte quotient to sreg
24         
25         lda     sreg+1          ; high byte remainder from sreg
26         stx     sreg+1          ; store high byte quotient to sreg
27
28         tax                     ; high byte remainder to x
29         tya                     ; low byte remainder to a
30
31 ; Adjust the sign of the remainder.
32 ; It must be the same as the sign of the dividend.
33 ;
34         bit     tmp1            ; Load high-byte of left argument
35         bpl     Pos             ; Jump if sign-of-result is positive
36         jmp     negax           ; Result is negative, adjust the sign
37
38 Pos:    rts