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) && !defined (SDCC_PARMS_IN_BANK1)
36 # define _MODULONG_ASM_SMALL_AUTO
38 # define _MODULONG_ASM_SMALL
44 #if defined _MODULONG_ASM_SMALL
47 _modlong_dummy (void) _naked
60 #if defined(SDCC_PARMS_IN_BANK1)
66 #if defined(SDCC_NOOVERLAY)
72 .globl __modulong_PARM_2
73 .globl __modslong_PARM_2
82 #define b0 (__modulong_PARM_2)
83 #define b1 (__modulong_PARM_2 + 1)
84 #define b2 (__modulong_PARM_2 + 2)
85 #define b3 (__modulong_PARM_2 + 3)
87 ; parameter a comes in a, b, dph, dpl
88 mov a3,a ; save parameter a3
90 mov a,b0 ; b == 0? avoid endless loop
97 clr c ; when loop1 jumps immediately to loop2
101 mov a,b3 ; if (!MSB_SET(b))
118 subb a,b0 ; here carry is always clear
143 loop2: ; clr c never set
158 mov a3,a ; -> yes; a = a - b;
179 mov a,a3 ; prepare the return value
186 #elif defined _MODULONG_ASM_SMALL_AUTO
189 _modlong_dummy (void) _naked
209 ar2 = 2 ; BUG register set is not considered
214 .globl __modlong ; entry point for __modslong
216 ; parameter a comes in a, b, dph, dpl
217 mov a3,a ; save parameter a3
220 add a,#-2-3 ; 2 bytes return address, 3 bytes param b
221 mov r0,a ; r1 points to b0
224 mov ar2,@r0 ; load b0
225 inc r0 ; r0 points to b1
232 __modlong: ; entry point for __modslong
233 ; a in r1, b, dph, dpl
234 ; b in r5, r4, r3, r2
238 mov a,b0 ; b == 0? avoid endless loop
245 clr c ; when loop1 jumps immediately to loop2
249 mov a,b3 ; if (!MSB_SET(b))
266 subb a,b0 ; here carry is always clear
291 loop2: ; clr c never set
305 mov a3,a ; -> yes; a = a - b;
328 mov a,a3 ; prepare the return value
335 #else // _MODULONG_ASM
337 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
340 _modulong (unsigned long a, unsigned long b)
342 unsigned char count = 0;
365 #endif // _MODULONG_ASM