home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Assembler / DVD!OMA2.DMS / in.adf / Examples / Library / MathFunctions / Math.asm < prev   
Encoding:
Assembly Source File  |  1994-10-13  |  3.3 KB  |  233 lines

  1.  
  2. ; Routinen für Mathematik.  Compress-Tab 8.
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. ; 16-Bit-Arithmetik.  Alle Werte werden    als 32-Bit-Werte gestackt.
  14.  
  15.     IFD    MulDiv16
  16.  
  17.     XDEF    _IDivS,_IDivU,_IMulS,_IMulU,_sdiv,_smuls,_umuls
  18.  
  19.  
  20.  
  21. ; IDivS    (arg1, arg2)
  22. ; Teile    arg1 (32 Bit) durch arg2 (16 Bit) vorzeichenbehaftet.
  23. ; Rückgabe: 32-Bit-Ergebnis vorzeichenbehaftet.
  24.  
  25. _sdiv
  26. _IDivS
  27.     move.l    4(SP),d0
  28.     divs    8+2(SP),d0
  29.     ext.l    d0
  30.     rts
  31.  
  32.  
  33.  
  34. ; IDivU    (arg1, arg2)
  35. ; Teile    arg1 (32 Bit) durch arg2 (16 Bit) vorzeichenlos.
  36. ; Rückgabe: 32-Bit-Ergebnis vorzeichenlos.
  37.  
  38. _IDivU
  39.     move.l    4(SP),d0
  40.     divu    8+2(SP),d0
  41.     swap    d0
  42.     clr.w    d0
  43.     swap    d0
  44.     rts
  45.  
  46.  
  47.  
  48. ; IMulS    (arg1, arg2)
  49. ; Multipliziere    arg1 (16 Bit) mit arg2 (16 Bit)    vorzeichenbehaftet.
  50. ; Rückgabe: 32-Bit-Ergebnis vorzeichenbehaftet.
  51.  
  52. _smuls
  53. _IMulS
  54.     move    4+2(SP),d0
  55.     muls    8+2(SP),d0
  56.     rts
  57.  
  58.  
  59.  
  60. ; IMulU    (arg1, arg2)
  61. ; Multipliziere    arg1 (16 Bit) mit arg2 (16 Bit)    vorzeichenlos.
  62. ; Rückgabe: 32-Bit-Ergebnis vorzeichenlos.
  63.  
  64. _umuls
  65. _IMulU
  66.     move    4+2(SP),d0
  67.     mulu    8+2(SP),d0
  68.     rts
  69.  
  70.     ENDC
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. ; 32-Bit-Arithmetik für    Assembler-Anwendung.  Das erste    Argument wird in D0
  82. ; übergeben, das zweite    in D1.    Rückgabe in D0.
  83.  
  84.     IFD    MulDiv32
  85.  
  86.     XDEF    lmult,ulmult,ldivt,uldivt,lmodt,ulmodt
  87.  
  88.  
  89.  
  90.  
  91.  
  92. lmult
  93. ulmult
  94.     ; D0 = D0 * D1    vorzeichenlos oder vorzeichenbehaftet.
  95.     movem.l    d1-d3,-(SP)
  96.     move.l    d0,d2
  97.     swap    d2
  98.     move.l    d1,d3
  99.     swap    d3
  100.     mulu    d1,d2            ; Kreuzprodukt 1
  101.     mulu    d0,d3            ; Kreuzprodukt 2
  102.     mulu    d1,d0            ; unteres Langwort
  103.     add.l    d3,d2            ; addiere Kreuzprodukte
  104.     swap    d2            ; verwende nur unteres Wort
  105.     clr.w    d2
  106.     add.l    d2,d0            ; unteres Langwort + Kreuzprodukt-Summe
  107.     movem.l    (SP)+,d1-d3
  108.     rts
  109.  
  110.  
  111.  
  112.  
  113.  
  114. ldivt
  115.     ; D0 = D0 / D1    vorzeichenbehaftet.
  116.     ; Ergebnis negativ, wenn ENTWEDER Dividend ODER    Divisor    negativ.
  117.     movem.l    d1-d4,-(SP)
  118.     move.l    d0,d4
  119.     bpl.s    1$
  120.     neg.l    d0
  121. 1$    eor.l    d1,d4            ; ergibt Vorzeichenbit in Bit 31
  122.     tst.l    d1
  123.     bpl.s    2$
  124.     neg.l    d1
  125. 2$    bsr.s    Divide
  126.     tst.l    d4            ; Ergebnis negativ ?
  127.     bpl.s    3$
  128.     neg.l    d0            ; ja
  129. 3$    movem.l    (SP)+,d1-d4
  130.     rts
  131.  
  132.  
  133.  
  134.  
  135.  
  136. lmodt
  137.     ; D0 = D0 mod D1  vorzeichenbehaftet.
  138.     ; Ergebnis negativ, wenn Dividend negativ.
  139.     movem.l    d1-d4,-(SP)
  140.     move.l    d0,d4
  141.     bpl.s    1$
  142.     neg.l    d0
  143. 1$    tst.l    d1
  144.     bpl.s    2$
  145.     neg.l    d1
  146. 2$    bsr.s    Divide
  147.     move.l    d1,d0
  148.     tst.l    d4            ; Ergebnis negativ ?
  149.     bpl.s    3$
  150.     neg.l    d0            ; ja
  151. 3$    movem.l    (SP)+,d1-d4
  152.     rts
  153.  
  154.  
  155.  
  156.  
  157.  
  158. uldivt
  159.     ; D0 = D0 / D1    vorzeichenlos.    Zusätzlich: Modulo in D1.
  160.     movem.l    d2-d3,-(SP)
  161.     bsr.s    Divide
  162.     movem.l    (SP)+,d2-d3
  163.     rts
  164.  
  165.  
  166.  
  167.  
  168.  
  169. ulmodt
  170.     ; D0 = D0 mod D1  vorzeichenlos.  Zusätzlich: Quotient in D1.
  171.     movem.l    d2-d3,-(SP)
  172.     bsr.s    Divide
  173.     exg    d1,d0            ; Modulo
  174.     movem.l    (SP)+,d2-d3
  175.     rts
  176.  
  177.  
  178.  
  179.  
  180.  
  181. Divide
  182.     ; Hilfsroutine.     D0 = D0 / D1  vorzeichenlos.  D1 enthält den Modulo.
  183.     move.l    d1,d2            ; Divisor nach D2
  184.     swap    d1
  185.     tst.w    d1
  186.     bne.s    1$            ; wenn Divisor größer $FFFF
  187.     move.l    d0,d3
  188.     clr.w    d0
  189.     swap    d0
  190.     divu    d2,d0
  191.     move.l    d0,d1
  192.     swap    d0            ; oberer Quotient nach oben
  193.     move.w    d3,d1
  194.     divu    d2,d1
  195.     move.w    d1,d0            ; ergibt 32-Bit-Quotient
  196.     clr.w    d1
  197.     swap    d1            ; Modulo
  198.     rts
  199.  
  200.     ; Divisor > $FFFF.  Die    Division ergibt    einen 16-Bit-Wert.
  201. 1$    move.l    d0,d1
  202.     swap    d0            ; beginne schon    mit 16-Bit-Verschiebung    !
  203.     clr.w    d0
  204.     clr.w    d1
  205.     swap    d1
  206.     moveq    #16-1,d3        ; schiebe nur noch 16 mal für den Rest
  207. 3$    add.l    d0,d0            ; ADD ist schneller als    LSL #1 !
  208.     addx.l    d1,d1            ; Übertrag berücksichtigen
  209.     cmp.l    d1,d2            ; kann schon eine Eins ins Ergebnis ?
  210.     bhi.s    2$
  211.     addq    #1,d0            ; ja, setze die    Eins
  212.     sub.l    d2,d1            ; und kürze den    Modulo
  213. 2$    dbra    d3,3$
  214.     rts
  215.  
  216.  
  217.  
  218.  
  219.  
  220.     ENDC
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.     END
  232.  
  233.