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
81 #define b0 (__modulong_PARM_2)
82 #define b1 (__modulong_PARM_2 + 1)
83 #define b2 (__modulong_PARM_2 + 2)
84 #define b3 (__modulong_PARM_2 + 3)
86 ; parameter a comes in a, b, dph, dpl
87 mov a3,a ; save parameter a3
89 mov a,b0 ; b == 0? avoid endless loop
96 clr c ; when loop1 jumps immediately to loop2
100 mov a,b3 ; if (!MSB_SET(b))
117 subb a,b0 ; here carry is always clear
142 loop2: ; clr c never set
157 mov a3,a ; -> yes; a = a - b;
178 mov a,a3 ; prepare the return value
185 #elif defined _MODULONG_ASM_SMALL_AUTO
188 _modlong_dummy (void) __naked
208 ar2 = 2 ; BUG register set is not considered
213 .globl __modlong ; entry point for __modslong
215 ; parameter a comes in a, b, dph, dpl
216 mov a3,a ; save parameter a3
219 add a,#-2-3 ; 2 bytes return address, 3 bytes param b
220 mov r0,a ; r1 points to b0
222 mov ar2,@r0 ; load b0
223 inc r0 ; r0 points to b1
230 __modlong: ; entry point for __modslong
231 ; a in r1, b, dph, dpl
232 ; b in r5, r4, r3, r2
236 mov a,b0 ; b == 0? avoid endless loop
243 clr c ; when loop1 jumps immediately to loop2
247 mov a,b3 ; if (!MSB_SET(b))
264 subb a,b0 ; here carry is always clear
288 loop2: ; clr c never set
302 mov a3,a ; -> yes; a = a - b;
325 mov a,a3 ; prepare the return value
332 #else // _MODULONG_ASM
334 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
337 _modulong (unsigned long a, unsigned long b)
339 unsigned char count = 0;
362 #endif // _MODULONG_ASM