home *** CD-ROM | disk | FTP | other *** search
/ Fractal Creations (Second Edition) / FRACTALS_2E.iso / frasrc.exe / MPMATH_A.ASM < prev    next >
Assembly Source File  |  1992-12-19  |  17KB  |  1,060 lines

  1. TITLE mpmath_a.asm (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2. SUBTTL All rights reserved.
  3. ;
  4. ;  Code may be used in any program provided the author is credited
  5. ;    either during program execution or in the documentation.  Source
  6. ;    code may be distributed only in combination with public domain or
  7. ;    shareware source code.  Source code may be modified provided the
  8. ;    copyright notice and this message is left unchanged and all
  9. ;    modifications are clearly documented.
  10. ;
  11. ;    I would appreciate a copy of any work which incorporates this code,
  12. ;    however this is optional.
  13. ;
  14. ;    Mark C. Peterson
  15. ;    405-C Queen St., Suite #181
  16. ;    Southington, CT 06489
  17. ;    (203) 276-9721
  18. ;
  19. ;  Note: Remark statements following floating point commands generally indicate
  20. ;     the FPU stack contents after the command is completed.
  21. ;
  22. ;  References:
  23. ;     80386/80286 Assembly Language Programming
  24. ;        by William H. Murray, III and Chris H. Pappas
  25. ;        Published by Osborne McGraw-Hill, 1986
  26. ;        
  27. ;
  28. ;
  29.  
  30.  
  31. IFDEF ??version
  32. MASM51
  33. QUIRKS
  34. ENDIF
  35.  
  36. .model medium, c
  37.  
  38.  
  39. .data
  40.  
  41. extrn cpu:WORD
  42.  
  43.  
  44. MP STRUC
  45.    Exp   DW    0
  46.    Mant  DD    0
  47. MP ENDS
  48.  
  49.  
  50. PUBLIC MPOverflow
  51. MPOverflow  DW        0
  52.  
  53. Ans         MP       <?>
  54. Double      DQ        ? 
  55.  
  56.  
  57. .code
  58.  
  59. .8086
  60.  
  61. fg2MP086    PROC     x:DWORD, fg:WORD
  62.    mov   ax, WORD PTR [x]
  63.    mov   dx, WORD PTR [x+2]
  64.    mov   cx, ax
  65.    or    cx, dx
  66.    jz    ExitFg2MP
  67.  
  68.    mov   cx, 1 SHL 14 + 30
  69.    sub   cx, fg
  70.  
  71.    or    dx, dx
  72.    jns   BitScanRight
  73.  
  74.    or    ch, 80h
  75.    not   ax
  76.    not   dx
  77.    add   ax, 1
  78.    adc   dx, 0
  79.  
  80. BitScanRight:
  81.    shl   ax, 1
  82.    rcl   dx, 1
  83.    dec   cx
  84.    or    dx, dx
  85.    jns   BitScanRight
  86.  
  87. ExitFg2MP:
  88.    mov   Ans.Exp, cx
  89.    mov   WORD PTR Ans.Mant+2, dx
  90.    mov   WORD PTR Ans.Mant, ax
  91.    lea   ax, Ans
  92.    mov   dx, ds
  93.    ret
  94. fg2MP086    ENDP
  95.  
  96.  
  97.  
  98. MPcmp086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  99. LOCAL Rev:WORD, Flag:WORD
  100.    mov   Rev, 0
  101.    mov   Flag, 0
  102.    mov   ax, xExp
  103.    mov   dx, WORD PTR [xMant]
  104.    mov   si, WORD PTR [xMant+2]
  105.    mov   bx, yExp
  106.    mov   cx, WORD PTR [yMant]
  107.    mov   di, WORD PTR [yMant+2]
  108.    or    ax, ax
  109.    jns   AtLeastOnePos
  110.  
  111.    or    bx, bx
  112.    jns   AtLeastOnePos
  113.  
  114.    mov   Rev, 1
  115.    and   ah, 7fh
  116.    and   bh, 7fh
  117.  
  118. AtLeastOnePos:
  119.    cmp   ax, bx
  120.    jle   Cmp1
  121.  
  122.    mov   Flag, 1
  123.    jmp   ChkRev
  124.  
  125. Cmp1:
  126.    je    Cmp2
  127.  
  128.    mov   Flag, -1
  129.    jmp   ChkRev
  130.  
  131. Cmp2:
  132.    cmp   si, di
  133.    jbe   Cmp3
  134.  
  135.    mov   Flag, 1
  136.    jmp   ChkRev
  137.  
  138. Cmp3:
  139.    je    Cmp4
  140.  
  141.    mov   Flag, -1
  142.    jmp   ChkRev
  143.  
  144. Cmp4:
  145.    cmp   dx, cx
  146.    jbe   Cmp5
  147.  
  148.    mov   Flag, 1
  149.    jmp   ChkRev
  150.  
  151. Cmp5:
  152.    je    ChkRev
  153.  
  154.    mov   Flag, -1
  155.  
  156. ChkRev:
  157.     or    Rev, 0
  158.    jz    ExitCmp
  159.  
  160.    neg   Flag
  161.  
  162. ExitCmp:
  163.    mov   ax, Flag
  164.    ret
  165. MPcmp086    ENDP
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. MPmul086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  173.    mov   ax, xExp
  174.    mov   bx, yExp
  175.    xor   ch, ch
  176.    shl   bh, 1
  177.    rcr   ch, 1
  178.    shr   bh, 1
  179.    xor   ah, ch
  180.  
  181.    sub   bx, (1 SHL 14) - 2
  182.    add   ax, bx
  183.    jno   NoOverflow
  184.  
  185. Overflow:
  186.    or    WORD PTR [xMant+2], 0
  187.    jz    ZeroAns
  188.    or    WORD PTR [yMant+2], 0
  189.    jz    ZeroAns
  190.  
  191.    mov   MPOverflow, 1
  192.  
  193. ZeroAns:
  194.    xor   ax, ax
  195.    xor   dx, dx
  196.    mov   Ans.Exp, ax
  197.    jmp   StoreMant
  198.  
  199. NoOverflow:
  200.    mov   Ans.Exp, ax
  201.  
  202.    mov   si, WORD PTR [xMant+2]
  203.    mov   bx, WORD PTR [xMant]
  204.    mov   di, WORD PTR [yMant+2]
  205.    mov   cx, WORD PTR [yMant]
  206.  
  207.    mov   ax, si
  208.    or    ax, bx
  209.    jz    ZeroAns
  210.  
  211.    mov   ax, di
  212.    or    ax, cx
  213.    jz    ZeroAns
  214.  
  215.    mov   ax, cx
  216.    mul   bx
  217.    push  dx
  218.  
  219.    mov   ax, cx
  220.    mul   si
  221.    push  ax
  222.    push  dx
  223.  
  224.    mov   ax, bx
  225.    mul   di
  226.    push  ax
  227.    push  dx
  228.  
  229.    mov   ax, si
  230.    mul   di
  231.    pop   bx
  232.    pop   cx
  233.    pop   si
  234.    pop   di
  235.  
  236.    add   ax, bx
  237.    adc   dx, 0
  238.    pop   bx
  239.    add   di, bx
  240.    adc   ax, 0
  241.    adc   dx, 0
  242.    add   di, cx
  243.    adc   ax, si
  244.    adc   dx, 0
  245.  
  246.    or    dx, dx
  247.    js    StoreMant
  248.  
  249.    shl   di, 1
  250.    rcl   ax, 1
  251.    rcl   dx, 1
  252.    sub   Ans.Exp, 1
  253.    jo    Overflow
  254.  
  255. StoreMant:
  256.    mov   WORD PTR Ans.Mant+2, dx
  257.    mov   WORD PTR Ans.Mant, ax
  258.  
  259.    lea   ax, Ans
  260.    mov   dx, ds
  261.    ret
  262. MPmul086    ENDP
  263.  
  264.  
  265.  
  266. d2MP086     PROC     uses si di, x:QWORD
  267.    mov   dx, WORD PTR [x+6]
  268.    mov   ax, WORD PTR [x+4]
  269.    mov   bx, WORD PTR [x+2]
  270.    mov   cx, WORD PTR [x]
  271.    mov   si, dx
  272.    shl   si, 1
  273.    or    si, bx
  274.    or    si, ax
  275.    or    si, dx
  276.    or    si, cx
  277.    jnz   NonZero
  278.  
  279.    xor   ax, ax
  280.    xor   dx, dx
  281.    jmp   StoreAns
  282.  
  283. NonZero:
  284.    mov   si, dx
  285.    shl   si, 1
  286.    pushf
  287.    mov   cl, 4
  288.    shr   si, cl
  289.    popf
  290.    rcr   si, 1
  291.    add   si, (1 SHL 14) - (1 SHL 10)
  292.  
  293.    mov   di, ax                           ; shl dx:ax:bx 12 bits
  294.    mov   cl, 12
  295.    shl   dx, cl
  296.    shl   ax, cl
  297.    mov   cl, 4
  298.    shr   di, cl
  299.    shr   bx, cl
  300.    or    dx, di
  301.    or    ax, bx
  302.    stc
  303.    rcr   dx, 1
  304.    rcr   ax, 1
  305.  
  306. StoreAns:
  307.    mov   Ans.Exp, si
  308.    mov   WORD PTR Ans.Mant+2, dx
  309.    mov   WORD PTR Ans.Mant, ax
  310.  
  311.    lea   ax, Ans
  312.    mov   dx, ds
  313.    ret
  314. d2MP086     ENDP
  315.  
  316.  
  317.  
  318. MP2d086     PROC     uses si di, xExp:WORD, xMant:DWORD
  319.    sub   xExp, (1 SHL 14) - (1 SHL 10)
  320.    jo    Overflow
  321.  
  322.    mov   bx, xExp
  323.    and   bx, 0111100000000000b
  324.    jz    InRangeOfDouble
  325.  
  326. Overflow:
  327.    mov   MPOverflow, 1
  328.    xor   ax, ax
  329.    xor   dx, dx
  330.    xor   bx, bx
  331.    jmp   StoreAns
  332.  
  333. InRangeOfDouble:
  334.    mov   si, xExp
  335.    mov   ax, si
  336.    mov   cl, 5
  337.    shl   si, cl
  338.    shl   ax, 1
  339.    rcr   si, 1
  340.  
  341.    mov   dx, WORD PTR [xMant+2]
  342.    mov   ax, WORD PTR [xMant]
  343.    shl   ax, 1
  344.    rcl   dx, 1
  345.  
  346.    mov   bx, ax
  347.    mov   di, dx
  348.    mov   cl, 12
  349.    shr   dx, cl
  350.    shr   ax, cl
  351.    mov   cl, 4
  352.    shl   bx, cl
  353.    shl   di, cl
  354.    or    ax, di
  355.    or    dx, si
  356.  
  357. StoreAns:
  358.    mov   WORD PTR Double+6, dx
  359.    mov   WORD PTR Double+4, ax
  360.    mov   WORD PTR Double+2, bx
  361.    xor   bx, bx
  362.    mov   WORD PTR Double, bx
  363.  
  364.    lea   ax, Double
  365.    mov   dx, ds
  366.    ret
  367. MP2d086     ENDP
  368.  
  369.  
  370.  
  371.  
  372. MPadd086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  373.    mov   si, xExp
  374.    mov   dx, WORD PTR [xMant+2]
  375.    mov   ax, WORD PTR [xMant]
  376.  
  377.    mov   di, yExp
  378.  
  379.    mov   cx, si
  380.    xor   cx, di
  381.    js    Subtract
  382.    jz    SameMag
  383.  
  384.    cmp   si, di
  385.    jg    XisGreater
  386.  
  387.    xchg  si, di
  388.    xchg  dx, WORD PTR [yMant+2]
  389.    xchg  ax, WORD PTR [yMant]
  390.  
  391. XisGreater:
  392.    mov   cx, si
  393.    sub   cx, di
  394.    cmp   cx, 32
  395.    jl    ChkSixteen
  396.    jmp   StoreAns
  397.  
  398. ChkSixteen:
  399.    cmp   cx, 16
  400.    jl    SixteenBitShift
  401.  
  402.    sub   cx, 16
  403.    mov   bx, WORD PTR [yMant+2]
  404.    shr   bx, cl
  405.    mov   WORD PTR [yMant], bx
  406.    mov   WORD PTR [yMant+2], 0
  407.    jmp   SameMag
  408.  
  409. SixteenBitShift:
  410.    mov   bx, WORD PTR [yMant+2]
  411.    shr   WORD PTR [yMant+2], cl
  412.    shr   WORD PTR [yMant], cl
  413.    neg   cl
  414.    add   cl, 16
  415.    shl   bx, cl
  416.    or    WORD PTR [yMant], bx
  417.  
  418. SameMag:
  419.    add   ax, WORD PTR [yMant]
  420.    adc   dx, WORD PTR [yMant+2]
  421.    jc    ShiftCarry
  422.    jmp   StoreAns
  423.  
  424. ShiftCarry:
  425.    rcr   dx, 1
  426.    rcr   ax, 1
  427.    add   si, 1
  428.    jo    Overflow
  429.    jmp   StoreAns
  430.  
  431. Overflow:
  432.    mov   MPOverflow, 1
  433.  
  434. ZeroAns:
  435.    xor   si, si
  436.    xor   ax, ax
  437.    xor   dx, dx
  438.    jmp   StoreAns
  439.  
  440. Subtract:
  441.    xor   di, 8000h
  442.    mov   cx, si
  443.    sub   cx, di
  444.    jnz   DifferentMag
  445.  
  446.    cmp   dx, WORD PTR [yMant+2]
  447.    jg    SubtractNumbers
  448.    jne   SwapNumbers
  449.  
  450.    cmp   ax, WORD PTR [yMant]
  451.    jg    SubtractNumbers
  452.    je    ZeroAns
  453.  
  454. SwapNumbers:   
  455.    xor   si, 8000h
  456.    xchg  ax, WORD PTR [yMant]
  457.    xchg  dx, WORD PTR [yMant+2]
  458.    jmp   SubtractNumbers
  459.  
  460. DifferentMag:
  461.    or    cx, cx
  462.    jns   NoSwap
  463.  
  464.    xchg  si, di
  465.    xchg  ax, WORD PTR [yMant]
  466.    xchg  dx, WORD PTR [yMant+2]
  467.    xor   si, 8000h
  468.    neg   cx
  469.  
  470. NoSwap:
  471.    cmp   cx, 32
  472.    jge   StoreAns
  473.  
  474.    cmp   cx, 16
  475.    jl    SixteenBitShift2
  476.  
  477.    sub   cx, 16
  478.    mov   bx, WORD PTR [yMant+2]
  479.    shr   bx, cl
  480.    mov   WORD PTR [yMant], bx
  481.    mov   WORD PTR [yMant+2], 0
  482.    jmp   SubtractNumbers
  483.  
  484. SixteenBitShift2:
  485.    mov   bx, WORD PTR [yMant+2]
  486.    shr   WORD PTR [yMant+2], cl
  487.    shr   WORD PTR [yMant], cl
  488.    neg   cl
  489.    add   cl, 16
  490.    shl   bx, cl
  491.    or    WORD PTR [yMant], bx
  492.  
  493. SubtractNumbers:
  494.    sub   ax, WORD PTR [yMant]
  495.    sbb   dx, WORD PTR [yMant+2]
  496.  
  497. BitScanRight:
  498.    or    dx, dx
  499.    js    StoreAns
  500.  
  501.    shl   ax, 1
  502.    rcl   dx, 1
  503.    sub   si, 1
  504.    jno   BitScanRight
  505.    jmp   Overflow
  506.  
  507. StoreAns:
  508.    mov   Ans.Exp, si
  509.    mov   WORD PTR Ans.Mant+2, dx
  510.    mov   WORD PTR Ans.Mant, ax
  511.  
  512.    lea   ax, Ans
  513.    mov   dx, ds
  514.    ret
  515. MPadd086    ENDP
  516.  
  517.  
  518.  
  519. MPdiv086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, \
  520.                      yMant:DWORD
  521.    mov   ax, xExp
  522.    mov   bx, yExp
  523.    xor   ch, ch
  524.    shl   bh, 1
  525.    rcr   ch, 1
  526.    shr   bh, 1
  527.    xor   ah, ch
  528.  
  529.    sub   bx, (1 SHL 14) - 2
  530.    sub   ax, bx
  531.    jno   NoOverflow
  532.  
  533. Overflow:
  534.    mov   MPOverflow, 1
  535.  
  536. ZeroAns:
  537.    xor   ax, ax
  538.    xor   dx, dx
  539.    mov   Ans.Exp, dx
  540.    jmp   StoreMant
  541.  
  542. NoOverflow:
  543.    mov   Ans.Exp, ax
  544.  
  545.    mov   dx, WORD PTR [xMant+2]
  546.    mov   ax, WORD PTR [xMant]
  547.    or    dx, dx
  548.    jz    ZeroAns
  549.  
  550.    mov   cx, WORD PTR [yMant+2]
  551.    mov   bx, WORD PTR [yMant]
  552.    or    cx, cx
  553.    jz    Overflow
  554.  
  555.    cmp   dx, cx
  556.    jl    Divide
  557.  
  558.    shr   dx, 1
  559.    rcr   ax, 1
  560.    add   Ans.Exp, 1
  561.    jo    Overflow
  562.  
  563. Divide:
  564.    div   cx
  565.    mov   si, dx
  566.    mov   dx, bx
  567.    mov   bx, ax
  568.    mul   dx
  569.    xor   di, di
  570.    cmp   dx, si
  571.    jnc   RemReallyNeg
  572.  
  573.    xchg  ax, di
  574.    xchg  dx, si
  575.    sub   ax, di
  576.    sbb   dx, si
  577.  
  578.    shr   dx, 1
  579.    rcr   ax, 1
  580.    div   cx
  581.    mov   dx, bx
  582.    shl   ax, 1
  583.    adc   dx, 0
  584.    jmp   StoreMant
  585.  
  586. RemReallyNeg:
  587.    sub   ax, di
  588.    sbb   dx, si
  589.  
  590.    shr   dx, 1
  591.    rcr   ax, 1
  592.    div   cx
  593.    mov   dx, bx
  594.    mov   bx, ax
  595.    xor   ax, ax
  596.    xor   cx, cx
  597.    shl   bx, 1
  598.    rcl   cx, 1
  599.    sub   ax, bx
  600.    sbb   dx, cx
  601.    jno   StoreMant
  602.  
  603.    shl   ax, 1
  604.    rcl   dx, 1
  605.    dec   Ans.Exp
  606.  
  607. StoreMant:
  608.    mov   WORD PTR Ans.Mant, ax
  609.    mov   WORD PTR Ans.Mant+2, dx
  610.    lea   ax, Ans
  611.    mov   dx, ds
  612.    ret
  613. MPdiv086    ENDP
  614.  
  615.  
  616. MPcmp386    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  617.    mov   si, 0
  618.    mov   di, si
  619. .386
  620.    mov   ax, xExp
  621.    mov   edx, xMant
  622.    mov   bx, yExp
  623.    mov   ecx, yMant
  624.    or    ax, ax
  625.    jns   AtLeastOnePos
  626.  
  627.    or    bx, bx
  628.    jns   AtLeastOnePos
  629.  
  630.    mov   si, 1
  631.    and   ah, 7fh
  632.    and   bh, 7fh
  633.  
  634. AtLeastOnePos:
  635.    cmp   ax, bx
  636.    jle   Cmp1
  637.  
  638.    mov   di, 1
  639.    jmp   ChkRev
  640.  
  641. Cmp1:
  642.    je    Cmp2
  643.  
  644.    mov   di, -1
  645.    jmp   ChkRev
  646.  
  647. Cmp2:
  648.    cmp   edx, ecx
  649.    jbe   Cmp3
  650.  
  651.    mov   di, 1
  652.    jmp   ChkRev
  653.  
  654. Cmp3:
  655.    je    ChkRev
  656.  
  657.    mov   di, -1
  658.  
  659. ChkRev:
  660.     or    si, si
  661.    jz    ExitCmp
  662.  
  663.    neg   di
  664.  
  665. ExitCmp:
  666.    mov   ax, di
  667. .8086
  668.    ret
  669. MPcmp386    ENDP
  670.  
  671.  
  672.  
  673. d2MP386     PROC     uses si di, x:QWORD
  674.    mov   si, WORD PTR [x+6]
  675. .386
  676.    mov   edx, DWORD PTR [x+4]
  677.    mov   eax, DWORD PTR [x]
  678.  
  679.    mov   ebx, edx
  680.    shl   ebx, 1
  681.    or    ebx, eax
  682.    jnz   NonZero
  683.    
  684.    xor   si, si
  685.    xor   edx, edx
  686.    jmp   StoreAns
  687.  
  688. NonZero:
  689.    shl   si, 1
  690.    pushf
  691.    shr   si, 4
  692.    popf
  693.    rcr   si, 1
  694.    add   si, (1 SHL 14) - (1 SHL 10)
  695.  
  696.    shld  edx, eax, 12
  697.    stc
  698.    rcr   edx, 1
  699.  
  700. StoreAns:
  701.    mov   Ans.Exp, si
  702.    mov   Ans.Mant, edx
  703.  
  704.    lea   ax, Ans
  705.    mov   dx, ds
  706. .8086
  707.    ret
  708. d2MP386     ENDP
  709.  
  710.  
  711.  
  712.  
  713. MP2d386     PROC     uses si di, xExp:WORD, xMant:DWORD
  714.    sub   xExp, (1 SHL 14) - (1 SHL 10)
  715. .386
  716.    jo    Overflow
  717.  
  718.    mov   bx, xExp
  719.    and   bx, 0111100000000000b
  720.    jz    InRangeOfDouble
  721.  
  722. Overflow:
  723.    mov   MPOverflow, 1
  724.    xor   eax, eax
  725.    xor   edx, edx
  726.    jmp   StoreAns
  727.  
  728. InRangeOfDouble:
  729.    mov   bx, xExp
  730.    mov   ax, bx 
  731.    shl   bx, 5
  732.    shl   ax, 1
  733.    rcr   bx, 1
  734.    shr   bx, 4
  735.  
  736.    mov   edx, xMant
  737.    shl   edx, 1
  738.    xor   eax, eax
  739.    shrd  eax, edx, 12
  740.    shrd  edx, ebx, 12
  741.  
  742. StoreAns:
  743.    mov   DWORD PTR Double+4, edx
  744.    mov   DWORD PTR Double, eax
  745.  
  746.    lea   ax, Double
  747.    mov   dx, ds
  748. .8086
  749.    ret
  750. MP2d386     ENDP
  751.  
  752.  
  753.  
  754. MPmul386    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, \
  755.                      yMant:DWORD
  756.    mov   ax, xExp
  757.    mov   bx, yExp
  758. .386
  759.    xor   ch, ch
  760.    shl   bh, 1
  761.    rcr   ch, 1
  762.    shr   bh, 1
  763.    xor   ah, ch
  764.  
  765.    sub   bx, (1 SHL 14) - 2
  766.    add   ax, bx
  767.    jno   NoOverflow
  768.  
  769. Overflow:
  770.    or    WORD PTR [xMant+2], 0
  771.    jz    ZeroAns
  772.    or    WORD PTR [yMant+2], 0
  773.    jz    ZeroAns
  774.  
  775.    mov   MPOverflow, 1
  776.  
  777. ZeroAns:
  778.    xor   edx, edx
  779.    mov   Ans.Exp, dx
  780.    jmp   StoreMant
  781.  
  782. NoOverflow:
  783.    mov   Ans.Exp, ax
  784.  
  785.    mov   eax, xMant
  786.    mov   edx, yMant
  787.    or    eax, eax
  788.    jz    ZeroAns
  789.  
  790.    or    edx, edx
  791.    jz    ZeroAns
  792.  
  793.    mul   edx
  794.  
  795.    or    edx, edx
  796.    js    StoreMant
  797.  
  798.    shld  edx, eax, 1
  799.    sub   Ans.Exp, 1
  800.    jo    Overflow
  801.  
  802. StoreMant:
  803.    mov   Ans.Mant, edx
  804.    lea   ax, Ans
  805.    mov   dx, ds
  806. .8086
  807.    ret
  808. MPmul386    ENDP
  809.  
  810.  
  811.  
  812. MPadd386    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, \
  813.                      yMant:DWORD
  814.    mov   si, xExp
  815. .386
  816.    mov   eax, xMant
  817.  
  818.    mov   di, yExp
  819.    mov   edx, yMant
  820.  
  821.    mov   cx, si
  822.    xor   cx, di
  823.    js    Subtract
  824.    jz    SameMag
  825.  
  826.    cmp   si, di
  827.    jg    XisGreater
  828.  
  829.    xchg  si, di
  830.    xchg  eax, edx
  831.  
  832. XisGreater:
  833.    mov   cx, si
  834.    sub   cx, di
  835.    cmp   cx, 32
  836.    jge   StoreAns
  837.  
  838.    shr   edx, cl
  839.  
  840. SameMag:
  841.    add   eax, edx
  842.    jnc   StoreAns
  843.  
  844.    rcr   eax, 1
  845.    add   si, 1
  846.    jno   StoreAns
  847.  
  848. Overflow:
  849.    mov   MPOverflow, 1
  850.  
  851. ZeroAns:
  852.    xor   si, si
  853.    xor   edx, edx
  854.    jmp   StoreAns
  855.  
  856. Subtract:
  857.    xor   di, 8000h
  858.    mov   cx, si
  859.    sub   cx, di
  860.    jnz   DifferentMag
  861.  
  862.    cmp   eax, edx
  863.    jg    SubtractNumbers
  864.    je    ZeroAns
  865.  
  866.    xor   si, 8000h
  867.    xchg  eax, edx
  868.    jmp   SubtractNumbers
  869.  
  870. DifferentMag:
  871.    or    cx, cx
  872.    jns   NoSwap
  873.  
  874.    xchg  si, di
  875.    xchg  eax, edx
  876.    xor   si, 8000h
  877.    neg   cx
  878.  
  879. NoSwap:
  880.    cmp   cx, 32
  881.    jge   StoreAns
  882.  
  883.    shr   edx, cl
  884.  
  885. SubtractNumbers:
  886.    sub   eax, edx
  887.    bsr   ecx, eax
  888.    neg   cx
  889.    add   cx, 31
  890.    shl   eax, cl
  891.    sub   si, cx
  892.    jo    Overflow
  893.  
  894. StoreAns:
  895.    mov   Ans.Exp, si
  896.    mov   Ans.Mant, eax
  897.  
  898.    lea   ax, Ans
  899.    mov   dx, ds
  900. .8086
  901.    ret
  902. MPadd386    ENDP
  903.    
  904.  
  905.    
  906.  
  907.  
  908. MPdiv386    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, \
  909.                      yMant:DWORD
  910.    mov   ax, xExp
  911.    mov   bx, yExp
  912. .386
  913.    xor   ch, ch
  914.    shl   bh, 1
  915.    rcr   ch, 1
  916.    shr   bh, 1
  917.    xor   ah, ch
  918.  
  919.    sub   bx, (1 SHL 14) - 2
  920.    sub   ax, bx
  921.    jno   NoOverflow
  922.  
  923. Overflow:
  924.    mov   MPOverflow, 1
  925.  
  926. ZeroAns:
  927.    xor   eax, eax
  928.    mov   Ans.Exp, ax
  929.    jmp   StoreMant
  930.  
  931. NoOverflow:
  932.    mov   Ans.Exp, ax
  933.  
  934.    xor   eax, eax
  935.    mov   edx, xMant
  936.    mov   ecx, yMant
  937.    or    edx, edx
  938.    jz    ZeroAns
  939.  
  940.    or    ecx, ecx
  941.    jz    Overflow
  942.  
  943.    cmp   edx, ecx
  944.    jl    Divide
  945.  
  946.    shr   edx, 1
  947.    rcr   eax, 1
  948.    add   Ans.Exp, 1
  949.    jo    Overflow
  950.  
  951. Divide:
  952.    div   ecx
  953.  
  954. StoreMant:
  955.    mov   Ans.Mant, eax
  956.    lea   ax, Ans
  957.    mov   dx, ds
  958. .8086
  959.    ret
  960. MPdiv386    ENDP
  961.  
  962.  
  963. fg2MP386    PROC     x:DWORD, fg:WORD
  964.    mov   bx, 1 SHL 14 + 30
  965.    sub   bx, fg
  966. .386
  967.    mov   edx, x
  968.  
  969.    or    edx, edx
  970.    jnz   ChkNegMP
  971.  
  972.    mov   bx, dx
  973.    jmp   StoreAns
  974.  
  975. ChkNegMP:
  976.    jns   BitScanRight
  977.  
  978.    or    bh, 80h
  979.    neg   edx
  980.  
  981. BitScanRight:
  982.    bsr   ecx, edx
  983.    neg   cx
  984.    add   cx, 31
  985.    sub   bx, cx
  986.    shl   edx, cl
  987.  
  988. StoreAns:
  989.    mov   Ans.Exp, bx
  990.    mov   Ans.Mant, edx
  991. .8086
  992.    lea   ax, Ans
  993.    mov   dx, ds
  994.    ret
  995. fg2MP386    ENDP
  996.  
  997.  
  998.  
  999. PUBLIC MPcmp, MPmul, MPadd, MPdiv, MP2d, d2MP, fg2MP
  1000.  
  1001. fg2MP:
  1002.    cmp   cpu, 386
  1003.    je    Use386fg2MP
  1004.    jmp   fg2MP086
  1005.  
  1006. Use386fg2MP:
  1007.    jmp   fg2MP386
  1008.  
  1009. MPcmp:
  1010.    cmp   cpu, 386
  1011.    je    Use386cmp
  1012.    jmp   MPcmp086
  1013.  
  1014. Use386cmp:
  1015.    jmp   MPcmp386
  1016.  
  1017. MPmul:
  1018.    cmp   cpu, 386
  1019.    je    Use386mul
  1020.    jmp   MPmul086
  1021.  
  1022. Use386mul:
  1023.    jmp   MPmul386
  1024.  
  1025. d2MP:
  1026.    cmp   cpu, 386
  1027.    je    Use386d2MP
  1028.    jmp   d2MP086
  1029.  
  1030. Use386d2MP:
  1031.    jmp   d2MP386
  1032.  
  1033. MPadd:
  1034.    cmp   cpu, 386
  1035.    je    Use386add
  1036.    jmp   MPadd086
  1037.  
  1038. Use386add:
  1039.    jmp   MPadd386
  1040.  
  1041. MPdiv:
  1042.    cmp   cpu, 386
  1043.    je    Use386div
  1044.    jmp   MPdiv086
  1045.  
  1046. Use386div:
  1047.    jmp   MPdiv386
  1048.  
  1049. MP2d:
  1050.    cmp   cpu, 386
  1051.    je    Use386MP2d
  1052.    jmp   MP2d086
  1053.  
  1054. Use386MP2d:
  1055.    jmp   MP2d386
  1056.  
  1057.  
  1058. END
  1059.  
  1060.