1 /*-------------------------------------------------------------------------
2 _modulong.c - routine for modulus of 32 bit unsigned long
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 Bug fixes by Martijn van Balen, aed@iae.nl
8 This library is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Library General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
27 /* Assembler-functions are provided for:
29 mcs51 small stack-auto
32 #if !defined(SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
33 # if defined(SDCC_mcs51)
34 # if defined(SDCC_MODEL_SMALL)
35 # if defined(SDCC_STACK_AUTO)
36 # define _MODULONG_ASM_SMALL_AUTO
38 # define _MODULONG_ASM_SMALL
44 #if defined _MODULONG_ASM_SMALL
47 _modlong_dummy (void) _naked
55 #if defined(SDCC_NOOVERLAY)
61 .globl __modulong_PARM_2
62 .globl __modslong_PARM_2
77 #define b0 (__modulong_PARM_2)
78 #define b1 (__modulong_PARM_2 + 1)
79 #define b2 (__modulong_PARM_2 + 2)
80 #define b3 (__modulong_PARM_2 + 3)
82 ; parameter a comes in a, b, dph, dpl
83 mov a3,a ; save parameter a3
85 mov a,b0 ; b == 0? avoid endless loop
92 clr c ; when loop1 jumps immediately to loop2
96 mov a,b3 ; if (!MSB_SET(b))
113 subb a,b0 ; here carry is always clear
138 loop2: ; clr c never set
153 mov a3,a ; -> yes; a = a - b;
174 mov a,a3 ; prepare the return value
181 #elif defined _MODULONG_ASM_SMALL_AUTO
184 _modlong_dummy (void) _naked
204 ar2 = 2 ; BUG register set is not considered
209 .globl __modlong ; entry point for __modslong
211 ; parameter a comes in a, b, dph, dpl
212 mov a3,a ; save parameter a3
215 add a,#-2-3 ; 2 bytes return address, 3 bytes param b
216 mov r0,a ; r1 points to b0
219 mov ar2,@r0 ; load b0
220 inc r0 ; r0 points to b1
227 __modlong: ; entry point for __modslong
228 ; a in r1, b, dph, dpl
229 ; b in r5, r4, r3, r2
233 mov a,b0 ; b == 0? avoid endless loop
240 clr c ; when loop1 jumps immediately to loop2
244 mov a,b3 ; if (!MSB_SET(b))
261 subb a,b0 ; here carry is always clear
286 loop2: ; clr c never set
300 mov a3,a ; -> yes; a = a - b;
323 mov a,a3 ; prepare the return value
330 #else // _MODULONG_ASM
332 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
335 _modulong (unsigned long a, unsigned long b)
337 unsigned char count = 0;
360 #endif // _MODULONG_ASM