fpuround.asm

来自「工欲善其事」· 汇编 代码 · 共 112 行

ASM
112
字号
; #########################################################################
;
;                             FpuRound
;
;##########################################################################

  ; -----------------------------------------------------------------------
  ; This procedure was written by Raymond Filiatreault, December 2002
  ;
  ; This FpuRound function rounds an 80-bit REAL number (Src) to the
  ; nearest integer and returns the integer portion at the specified
  ; destination (the FPU itself, a TBYTE memory variable, or a LONG integer
  ; memory variable), unless an invalid operation is reported by the FPU
  ; or the definition of the parameters (with uID) is invalid.
  ;
  ; The source can only be an 80-bit REAL number from the FPU itself or
  ; from memory.
  ;
  ; The source is not checked for validity. This is the programmer's
  ; responsibility.
  ;
  ; Only EAX is used to return error or success. All other registers are
  ; preserved.
  ;
  ; -----------------------------------------------------------------------

    .486
    .model flat, stdcall  ; 32 bit memory model
    option casemap :none  ; case sensitive

    include Fpu.inc

    .data
    
    oldcw   dw    0
    stword  dw    0

    .code

; #########################################################################

FpuRound proc public lpSrc:DWORD, lpDest:DWORD, uID:DWORD
        
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
; Because a library is assembled before its functions are called, all
; references to external memory data must be qualified for the expected
; size of that data so that the proper code is generated.
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; the FPU will be initialized only if the source parameter is not taken
; from the FPU itself.

      fclex                   ;clear exception flags on FPU
      test  uID,SRC1_FPU      ;is Src taken from FPU?
      jnz   dest0             ;go complete process
      finit
      
;----------------------------------------
;check source for Src and load it to FPU
;----------------------------------------

      test  uID,SRC1_REAL     ;is Src an 80-bit REAL in memory?
      jz    srcerr
      mov   eax,lpSrc
      fld   tbyte ptr [eax]
      jmp   dest0             ;go complete process

srcerr:
      finit
      xor   eax,eax
      ret

dest0:
      fstcw oldcw             ;get current control word
      fwait
      mov   ax,oldcw
      and   ax,0f3ffh         ;code it for rounding
      mov   stword,ax
      fldcw stword            ;change rounding code of FPU to round

      frndint                 ;round the number
      fldcw oldcw             ;load back the former control word
      
      fstsw stword            ;retrieve exception flags from FPU
      fwait
      test  stword,1          ;test for invalid operation
      jnz   srcerr            ;clean-up and return error

      test  uID,DEST_FPU      ;check where result should be stored
      jnz   finish            ;leave result on FPU if so indicated

      mov   eax,lpDest
      test  uID,DEST_IMEM
      jnz   @F
      fstp  tbyte ptr [eax]   ;store result as a REAL10
      jmp   finish

@@:
      fistp dword ptr [eax]   ;store result as an integer

finish:
      or    al,1              ;to insure EAX!=0
      ret
    
FpuRound endp

; #########################################################################

end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?