]> git.sur5r.net Git - cc65/blob - libsrc/runtime/mod.s
0706ba3159bc83b8a27fa65d89b36e685f7328da
[cc65] / libsrc / runtime / mod.s
1 ;
2 ; Christian Krueger, 24-May-2018
3 ;
4 ; CC65 runtime: modulo operation for signed ints
5 ;
6
7 ; When negating values, we will ignore the possibility here, that one of the
8 ; values is $8000, in which case the negate will fail.
9
10         .export         tosmoda0, tosmodax
11         .import         absvaludiv16, negax
12         .importzp       sp, sreg, tmp1
13
14 tosmoda0:
15         ldx     #0
16 tosmodax:
17
18 ; Prepare adjustment of the sign of the result. The sign of the result of the
19 ; modulo operation is the same as that of the left operand.
20
21         pha                         
22         ldy     #1                  ; Prepare lhs operant hi-byte fetch
23         lda     (sp),y             
24         sta     tmp1                ; Save post negation indicator to tmp1
25         pla                         ; Back to entry accu
26         jsr     absvaludiv16
27         ldx     sreg+1              ; Remainder to return registers
28         lda     sreg
29         ldy     tmp1                ; Fetch indicator
30         bmi     negate
31         rts
32 negate: jmp     negax