home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / convert2.inc < prev    next >
Text File  |  1988-08-28  |  6KB  |  145 lines

  1. ;*********************************************************
  2. ;* Show87 - (C) Copyright 1988 by Borland International  *
  3. ;* CONVERT2.INC - Include module for Show87              *
  4. ;*********************************************************
  5. ;
  6. ;=============================================================================
  7. ; 8087 Number Conversion Routines
  8. ;
  9. ; These are routines to convert from floating point to integer numbers. All
  10. ; registers are preserved except those used to return parameters. All
  11. ; parameters are passed through registers. It is assumed that DS = ES = CS.
  12.  
  13. ;================================================
  14. ; Convert a floating point real number to a
  15. ; decimal integer and a base ten exponent.
  16. ; Expects the number to be converted in ST and
  17. ; the number of significant bits (i.e. bits to
  18. ; place in the significand) in AX.  The
  19. ; significand integer value is returned in ST
  20. ; and the signed exponent is returned in AX.
  21. ;===================================================
  22. ;
  23. Flt2dec proc near
  24.         push    bp
  25.         sub     sp, 8
  26.         mov     bp, sp
  27.  
  28.         fstcw   word ptr [bp]                   ;save control word
  29.         mov     word ptr [bp+2], 03bfh          ;new control word, round
  30.                                                 ;  to nearest
  31.         fldcw   word ptr [bp+2]                 ;load control word
  32.         mov     word ptr [bp+4], ax
  33.  
  34.          ;--- convert number
  35.  
  36.         fld     st(0)
  37.         fxtract                                 ;extract exponent
  38.         fstp    st(0)                           ;pop top (significand)
  39.         fisubr  word ptr [bp+4]                 ;subtract bits for significand
  40.         fldl2t                                  ;load log2(10)
  41.         fdiv                                    ;divide, get 10**X
  42.         frndint                                 ;round to nearest
  43.         fist    word ptr [bp+6]                 ;save exponent
  44.         call    Exp10                           ;get exponent factor
  45.         fmul                                    ;adjust real number for exponent
  46.  
  47.         ;--- finished
  48.  
  49.         fldcw   word ptr [bp]                   ;restore control word
  50.         add     sp, 6
  51.         pop     ax
  52.         neg     ax
  53.         pop     bp
  54.         ret
  55.  Endp           ;Flt2dec
  56.  
  57. ;================================================
  58. ; Calculate 10 to the power of ST. Return result
  59. ; in ST. Uses 10**N = 2**(N*Log2(10)).
  60.  
  61. Exp10 proc near
  62.         fldl2t                                  ;load Log2(10)
  63.         fmul                                    ;multiply
  64.         call    Exp2                            ;raise 2 to ST power.
  65.         ret
  66.  Endp           ;Exp10
  67.  
  68. ;================================================
  69. ; Calculate 2 to the power of ST. Return result
  70. ; in ST(0).
  71.  
  72. Exp2 proc near
  73.         push    bp
  74.         sub     sp, 6                           ;room for local data
  75.         mov     bp, sp                          ;start of local data
  76.  
  77.         fstcw   word ptr [bp]                   ;save control word
  78.         mov     word ptr [bp+2], 03bfh          ;new control word, round
  79.                                                 ;   to nearest
  80.         fldcw   word ptr [bp+2]                 ;load control word
  81.  
  82. ;--- for 2**X, where X = W + F or X = W - F, where W is a whole number and
  83. ;--- F is between 0 and .5 (inclusive), ST gets F and ST(1) gets W
  84.  
  85.         fld     st(0)                           ;duplicate number
  86.         frndint                                 ;round to integer,
  87.                                                 ;  ST => W
  88.         fxch                                    ;exchange
  89.         fsub    st, st(1)                       ;get difference,
  90.                                                 ;  ST => F
  91.  
  92. ;--- check sign of fractional portion (F)
  93.  
  94.         ftst                                    ;set flags
  95.         fstsw   word ptr [bp+4]                 ;save flags
  96.         fwait
  97.         and     byte ptr [bp+5], 45h            ;mask relevant bits
  98.         cmp     byte ptr [bp+5], 1              ;check negative bit
  99.         ja      Exp2err                         ;jump if other bits
  100.                                                 ;   set, error
  101.         je      Exp2neg                         ;jump if negative
  102.  
  103. ;--- fractional part is positive for
  104.  
  105.         f2xm1                                   ;ST => (2**F) - 1
  106.         fld1                                    ;load one
  107.         fadd                                    ;ST => 2**F
  108.         fxch                                    ;put whole portion (W)
  109.                                                 ;   on top
  110.         fld1                                    ;load one
  111.         fscale                                  ;ST => 2**W
  112.         fmulp   st(2), st                       ;ST(2) => (2**F) * (2**W)
  113.         fstp    st(0)                           ;pop stack
  114.         jmp     short Exp2don
  115.  
  116. ;--- fractional part is negative
  117.  
  118. Exp2neg :
  119.         fabs                                    ;take absolute value
  120.         f2xm1                                   ;ST => (2**F) - 1
  121.         fld1                                    ;load one
  122.         fadd                                    ;ST => 2**F
  123.         fxch                                    ;put whole portion (W) on top
  124.         fld1                                    ;load one
  125.         fscale                                  ;ST => 2**W
  126.         fdivrp  st(2), st                       ;ST(2) => (2**W) / (2**F)
  127.         fstp    st(0)                           ;pop stack
  128.  
  129. ;--- finished
  130.  
  131. Exp2don :
  132.         fldcw   word ptr [bp]                   ;restore control word
  133.         add     sp, 6
  134.         pop     bp
  135.         ret
  136.  
  137. ;--- zero or error
  138.  
  139. Exp2err :
  140.         fstp    st(0)
  141.         fstp    st(0)                           ;clear stack
  142.         fld1                                    ;return one
  143.         jmp     Exp2don
  144.  endp           ;Exp2
  145.