]> git.sur5r.net Git - cc65/blob - libsrc/runtime/lmod.s
cfg/atari-xex.cfg: fix typo in comment
[cc65] / libsrc / runtime / lmod.s
1 ;
2 ; Ullrich von Bassewitz, 07.08.1998
3 ; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
4 ; CC65 runtime: modulo operation for long signed ints
5 ;
6
7 ; When negating values, we will ignore the possibility here, that one of the
8 ; values if $8000, in which case the negate will fail.
9
10         .export         tosmod0ax, tosmodeax
11         .import         poplsargs, udiv32, negeax
12         .importzp       sreg, ptr1, ptr2, tmp1, tmp3, tmp4
13
14         .macpack        cpu
15
16 tosmod0ax:
17 .if (.cpu .bitand ::CPU_ISET_65SC02)
18         stz     sreg
19         stz     sreg+1
20 .else
21         ldy     #$00
22         sty     sreg
23         sty     sreg+1
24 .endif
25
26 tosmodeax:                         
27         jsr     poplsargs       ; Get arguments from stack, adjust sign
28         jsr     udiv32          ; Do the division, remainder is in (ptr2:tmp3:tmp4)
29
30 ; Load the result
31
32         lda     ptr2
33         ldx     ptr2+1
34         ldy     tmp3
35         sty     sreg
36         ldy     tmp4
37         sty     sreg+1
38
39 ; Check the sign of the result. It is the sign of the left operand.
40
41         bit     tmp1            ; Check sign of left operand
42         bpl     Pos             ; Jump if result is positive
43
44 ; Result is negative
45
46         jmp     negeax          ; Negate result
47
48 ; Result is positive
49
50 Pos:    rts                     ; Done
51