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