home *** CD-ROM | disk | FTP | other *** search
- *
- * Maxon C++ Library:
- *
- * Modul "integer"
- *
- * Jens Gelhar 28.05.92, 08.04.94
- *
-
- xdef _inttostr,_uinttostr
- xdef intdiv,intmult,uintdiv,uintmult
- xdef lib_div_int,lib_div_uint
-
- _inttostr: ; char *inttostr (int i, char* buffer, short base)
- tst.l 4(a7)
- bpl.b _uinttostr
- ; negative Zahl:
- movem.l d1/d2/a0,-(a7)
- movem.l 3*4+4(a7),d0/a0; Zahl/Buffer
- neg.l d0
- move.b #'-',(a0)+
- bra.b Einsprung
-
- _uinttostr: ; ebenso, aber vorzeichenlos
- movem.l d1/d2/a0,-(a7)
- movem.l 3*4+4(a7),d0/a0; Zahl/Buffer
- Einsprung:
- moveq #0,d1
- move.w 3*4+4+2*4(a7),d1 ; Basis
- move.l d1,d2
- clr.w -(a7) ; Endmarkierung
- Loop: move.l d2,d1
- bsr uintdiv
- add.w #'0',d1
- cmp.w #'9',d1
- bls.b Digit
- add.w #'a'-'0'-10,d1
- Digit: move.w d1,-(a7) ; Ziffern auf Stack zwischenspeichern
- tst.l d0
- bne.b Loop
- Copy: move.w (a7)+,d0
- move.b d0,(a0)+
- bne.b Copy
- movem.l (a7)+,d1/d2/a0
- move.l 8(a7),d0 ; Zeiger auf Buffer zurückgeben
- rts
-
- intmult: ; d0.l *= d1.l
- uintmult: ; d0.l *= d1.l
- move.l d3,-(a7)
- move.l d2,-(a7)
- move.l d1,d3
- mulu d0,d3 ; d3 = L0*L1
- move.l d1,d2
- swap d2
- mulu d0,d2 ; d2 = L0*H1
- swap d0
- mulu d1,d0 ; d0 = H0*L1
- add.l d2,d0
- swap d0
- clr.w d0
- add.l d3,d0
- move.l (a7)+,d2
- move.l (a7)+,d3
- rts
-
-
- lib_div_int:
- tst.l d0
- bmi.b ldi1
- tst.l d1
- bpl.b lib_div_uint
- neg.l d1
- ldi0: bsr.b lib_div_uint
- neg.l d0
- rts
- ldi1: neg.l d0
- tst.l d1
- bpl.b ldi0
- neg.l d1
- lib_div_uint:
- swap d1
- tst.w d1
- bne.b div_slow
- move.l d3,-(a7)
- move.l d2,-(a7)
- swap d1
- move.w d1,d3
- move.w d0,d2
- clr.w d0
- swap d0
- divu d3,d0
- move.l d0,d1
- swap d0
- move.w d2,d1
- divu d3,d1
- move.w d1,d0
- clr.w d1
- swap d1
- move.l (a7)+,d2
- move.l (a7)+,d3
- rts
- div_slow:
- swap d1
-
- uintdiv: ; d0.l durch d1.l teilen, Ergebnis nach d0, Rest nach d1
- movem.l d2-d4,-(a7)
- moveq #31,d2 ; d2: Zähler
- moveq #0,d3 ; Ergebnis
- moveq #0,d4 ; Hi-Bits von d0
- divLoop: asl.l #1,d3
- asl.l #1,d0
- roxl.l #1,d4
- cmp.l d1,d4
- blo.b div1
- sub.l d1,d4
- addq.l #1,d3
- div1: dbra d2,divLoop
- move.l d4,d1
- move.l d3,d0
- movem.l (a7)+,d2-d4
- rts
-
- intdiv: ; d0.l/=d1.l, Rest nach d1.l
- tst.l d0
- bmi.b idi1
- ; d0 >= 0:
- tst.l d1
- bpl.b uintdiv
- ; +/-:
- neg.l d1
- bsr.b uintdiv
- neg.l d0
- rts
- idi1: ; d0 < 0:
- neg.l d0
- tst.l d1
- bpl.b idi2
- ; -/-:
- neg.l d1
- bsr.b uintdiv
- neg.l d1
- rts
- idi2: ; -/+:
- bsr.b uintdiv
- neg.l d0
- neg.l d1
- rts
-
- end
-