1 /*-------------------------------------------------------------------------
2 _modulong.c - routine for modulus of 32 bit unsigned long
4 Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
5 Bug fixes by Martijn van Balen, aed@iae.nl
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22 As a special exception, if you link this library with other files,
23 some of which are compiled with SDCC, to produce an executable,
24 this library does not by itself cause the resulting executable to
25 be covered by the GNU General Public License. This exception does
26 not however invalidate any other reasons why the executable file
27 might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------*/
30 /* Assembler-functions are provided for:
32 mcs51 small stack-auto
37 #if !defined(__SDCC_USE_XSTACK) && !defined(_SDCC_NO_ASM_LIB_FUNCS)
38 # if defined(__SDCC_mcs51)
39 # if defined(__SDCC_MODEL_SMALL)
40 # if defined(__SDCC_STACK_AUTO) && !defined (__SDCC_PARMS_IN_BANK1)
41 # define _MODULONG_ASM_SMALL_AUTO
43 # define _MODULONG_ASM_SMALL
49 #if defined _MODULONG_ASM_SMALL
52 _modlong_dummy (void) __naked
65 #if defined(__SDCC_PARMS_IN_BANK1)
71 #if defined(__SDCC_NOOVERLAY)
77 .globl __modulong_PARM_2
78 .globl __modslong_PARM_2
86 #define b0 (__modulong_PARM_2)
87 #define b1 (__modulong_PARM_2 + 1)
88 #define b2 (__modulong_PARM_2 + 2)
89 #define b3 (__modulong_PARM_2 + 3)
91 ; parameter a comes in a
, b
, dph
, dpl
92 mov a3
,a
; save parameter a3
94 mov a
,b0
; b
== 0? avoid endless loop
101 clr c
; when loop1 jumps immediately to loop2
105 mov a
,b3
; if (!MSB_SET(b
))
122 subb a
,b0
; here carry is always clear
147 loop2
: ; clr c never set
162 mov a3
,a
; -> yes
; a
= a
- b
;
183 mov a
,a3
; prepare the
return value
190 #elif defined _MODULONG_ASM_SMALL_AUTO
193 _modlong_dummy (void) __naked
213 ar2
= 2 ; BUG
register set is
not considered
218 .globl __modlong
; entry point
for __modslong
220 ; parameter a comes in a
, b
, dph
, dpl
221 mov a3
,a
; save parameter a3
224 add a
,#-2-3 ; 2 bytes return address, 3 bytes param b
225 mov r0
,a
; r1 points to b0
227 mov ar2
,@r0
; load b0
228 inc r0
; r0 points to b1
235 __modlong
: ; entry point
for __modslong
236 ; a in r1
, b
, dph
, dpl
237 ; b in r5
, r4
, r3
, r2
241 mov a
,b0
; b
== 0? avoid endless loop
248 clr c
; when loop1 jumps immediately to loop2
252 mov a
,b3
; if (!MSB_SET(b
))
269 subb a
,b0
; here carry is always clear
293 loop2
: ; clr c never set
307 mov a3
,a
; -> yes
; a
= a
- b
;
330 mov a
,a3
; prepare the
return value
337 #else // _MODULONG_ASM
339 #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1)
342 _modulong (unsigned long a
, unsigned long b
) __SDCC_NONBANKED
344 unsigned char count
= 0;
367 #endif // _MODULONG_ASM