home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / MAXONC3_6OF8.DMS / in.adf / LIBSRC.LHA / LIBSRC / integer.asm < prev    next >
Encoding:
Assembly Source File  |  1994-04-08  |  2.4 KB  |  150 lines

  1.  *
  2.  * Maxon C++ Library:
  3.  *
  4.  * Modul "integer"
  5.  *
  6.  * Jens Gelhar 28.05.92, 08.04.94
  7.  *
  8.  
  9.     xdef    _inttostr,_uinttostr
  10.     xdef    intdiv,intmult,uintdiv,uintmult
  11.     xdef    lib_div_int,lib_div_uint
  12.  
  13. _inttostr:    ; char *inttostr (int i, char* buffer, short base)
  14.     tst.l 4(a7)
  15.     bpl.b _uinttostr
  16.     ; negative Zahl:
  17.     movem.l d1/d2/a0,-(a7)
  18.     movem.l 3*4+4(a7),d0/a0;        Zahl/Buffer
  19.     neg.l d0
  20.     move.b #'-',(a0)+
  21.     bra.b Einsprung
  22.  
  23. _uinttostr:    ; ebenso, aber vorzeichenlos
  24.     movem.l d1/d2/a0,-(a7)
  25.     movem.l 3*4+4(a7),d0/a0;        Zahl/Buffer
  26. Einsprung:
  27.     moveq #0,d1
  28.     move.w 3*4+4+2*4(a7),d1       ; Basis
  29.     move.l d1,d2
  30.     clr.w -(a7)             ; Endmarkierung
  31. Loop:    move.l d2,d1
  32.     bsr uintdiv
  33.     add.w #'0',d1
  34.     cmp.w #'9',d1
  35.     bls.b Digit
  36.     add.w #'a'-'0'-10,d1
  37. Digit:    move.w d1,-(a7)         ; Ziffern auf Stack zwischenspeichern
  38.     tst.l d0
  39.     bne.b Loop
  40. Copy:    move.w (a7)+,d0
  41.     move.b d0,(a0)+
  42.     bne.b Copy
  43.     movem.l (a7)+,d1/d2/a0
  44.     move.l 8(a7),d0         ; Zeiger auf Buffer zurückgeben
  45.     rts
  46.  
  47. intmult:    ; d0.l *= d1.l
  48. uintmult:    ; d0.l *= d1.l
  49.     move.l d3,-(a7)
  50.     move.l d2,-(a7)
  51.     move.l d1,d3
  52.     mulu d0,d3              ; d3 = L0*L1
  53.     move.l d1,d2
  54.     swap d2
  55.     mulu d0,d2              ; d2 = L0*H1
  56.     swap d0
  57.     mulu d1,d0              ; d0 = H0*L1
  58.     add.l d2,d0
  59.     swap d0
  60.     clr.w d0
  61.     add.l d3,d0
  62.     move.l (a7)+,d2
  63.     move.l (a7)+,d3
  64.     rts
  65.  
  66.  
  67. lib_div_int:
  68.     tst.l d0
  69.     bmi.b ldi1
  70.     tst.l d1
  71.     bpl.b lib_div_uint
  72.     neg.l d1
  73. ldi0:    bsr.b lib_div_uint
  74.     neg.l d0
  75.     rts
  76. ldi1:    neg.l d0
  77.     tst.l d1
  78.     bpl.b ldi0
  79.     neg.l d1
  80. lib_div_uint:
  81.     swap d1
  82.     tst.w d1
  83.     bne.b div_slow
  84.     move.l d3,-(a7)
  85.     move.l d2,-(a7)
  86.     swap d1
  87.     move.w d1,d3
  88.     move.w d0,d2
  89.     clr.w d0
  90.     swap d0
  91.     divu d3,d0
  92.     move.l d0,d1
  93.     swap d0
  94.     move.w d2,d1
  95.     divu d3,d1
  96.     move.w d1,d0
  97.     clr.w d1
  98.     swap d1
  99.     move.l (a7)+,d2
  100.     move.l (a7)+,d3
  101.     rts
  102. div_slow:
  103.     swap d1
  104.  
  105. uintdiv:    ; d0.l durch d1.l teilen, Ergebnis nach d0, Rest nach d1
  106.     movem.l d2-d4,-(a7)
  107.     moveq #31,d2            ; d2: Zähler
  108.     moveq #0,d3             ; Ergebnis
  109.     moveq #0,d4             ; Hi-Bits von d0
  110. divLoop:    asl.l #1,d3
  111.     asl.l #1,d0
  112.     roxl.l #1,d4
  113.     cmp.l d1,d4
  114.     blo.b div1
  115.     sub.l d1,d4
  116.     addq.l #1,d3
  117. div1:    dbra d2,divLoop
  118.     move.l d4,d1
  119.     move.l d3,d0
  120.     movem.l (a7)+,d2-d4
  121.     rts
  122.  
  123. intdiv:    ; d0.l/=d1.l, Rest nach d1.l
  124.     tst.l d0
  125.     bmi.b idi1
  126.     ; d0 >= 0:
  127.     tst.l d1
  128.     bpl.b uintdiv
  129.     ; +/-:
  130.     neg.l d1
  131.     bsr.b uintdiv
  132.     neg.l d0
  133.     rts
  134. idi1:    ; d0 < 0:
  135.     neg.l d0
  136.     tst.l d1
  137.     bpl.b idi2
  138.     ; -/-:
  139.     neg.l d1
  140.     bsr.b uintdiv
  141.     neg.l d1
  142.     rts
  143. idi2:    ; -/+:
  144.     bsr.b uintdiv
  145.     neg.l d0
  146.     neg.l d1
  147.     rts
  148.  
  149.     end
  150.