home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / math / rn_f.asm < prev   
Assembly Source File  |  1986-01-17  |  3KB  |  69 lines

  1. RN_F     PROC     NEAR
  2. ;********************************************************
  3. ;                      Rounds a floating point operand to
  4. ;                      the nearest integer value
  5. ;********************************************************
  6.          PUSH     SI              ;save index registers
  7.          PUSH     DI
  8.  
  9.          MOV      BX,CX           ;and BX
  10.          MOV      DX,[DI]+2       ;get operand in DX:AX  
  11.          MOV      AX,[DI]
  12. ;
  13.          SUB      CX,CX           ;clear CX
  14.          MOV      DI,10000000B    ;isolate sign in DI
  15.          AND      DI,DX
  16.          OR       DX,10000000B    ;restore leading one
  17. ;                             check range
  18.          CMP      DH,128          ;exponent bias is 128
  19.          JA       RN1
  20.          JB       RN7             ;if exponent<0 round to zero
  21. ;                             exponent is zero
  22.          CMP      DL,128          ;number >= .5 and < 1
  23.          JBE      RN7             ;if fraction=0 round to zero
  24.          MOV      DX,CX           ;round to +1-all zeros
  25.          MOV      AX,CX
  26.          MOV      BH,81H          ;and biased exponent of 1
  27.          JMP      RN6             ;restore sign and exit
  28. ;
  29. RN1:     MOV      BH,DH           ;exponent in BH in case we exit
  30.          CMP      DH,152          ;if exponent>=24, already integer
  31.          JAE      RN6             ;so restore sign and exit
  32. ;                             exponent is in range 1-23
  33.          MOV      CL,DH           ;put unbiased exponent in CX
  34.          SUB      CX,128
  35.          NEG      CX              ;COUNT is CX - 24
  36.          ADD      CX,24
  37. ;                             shift right COUNT bits
  38.          SUB      DH,DH           ;clear DH
  39.          MOV      BH,DH           ;and BH
  40. RN2:     SHR      DL,1            ;shift it right
  41.          RCR      AX,1
  42.          RCR      DH,1            ;rotate fraction into DH:BX
  43.          RCR      BX,1
  44.          LOOP     RN2
  45. ;                             round
  46.          OR       DH,DH           ;is guard bit on?
  47.          JNS      RN4             ;if not, rounding not needed
  48.          TEST     AX,1            ;if integer odd
  49.          JNZ      RN3             ;round up
  50.          CMP      DH,80H          ;if any trailing bits on
  51.          JA       RN3             ;round up
  52.          OR       BX,BX           
  53.          JZ       RN4
  54. RN3:     ADD      AX,1            ;round up
  55.          ADC      DX,0
  56. ;                             normalize--shift left
  57. RN4:     MOV      BH,152          ;set exponent to zero
  58. RN5:     SHL      AX,1            ;shift left
  59.          RCL      DL,1
  60.          DEC      BH              ;increment exponent
  61.          OR       DL,DL
  62.          JNS      RN5             ;shift again if not normalized
  63. ;
  64. RN6:     JMP      FINISH          ;restore sign and exit
  65. RN7:     SUB      DX,DX           ;zero result
  66.          MOV      AX,DX
  67.          MOV      BX,DX
  68.          JMP      EXIT
  69. RN_F     ENDP