News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Working with FPU

Started by ToutEnMasm, March 01, 2008, 02:45:38 PM

Previous topic - Next topic

ToutEnMasm

Hello,
Working with fpu isn't so easy as it seem's.
- The FPU must be leave in is original state (stack = 0)
- load must be in the same number as store
- The function is right after further call with the same or other's function.

That's a sample who follow this rules.It work.The method to clean the stack is the one used by c++.
  (fistp dword ptr [esp]) .
The function calcul the % of a number.
source = the number in which we want extract the %

percent is the number of % to extract
eax is the result
Perhap's some more things are to be said about that ?


Quote
;################################################################
PourCent proc source:DWORD, percent:DWORD
;    invoke   PourCent,Xresolution,80         ;80 % de Xresolution
   LOCAL var1:DWORD

   mov var1, 100   ; to divide by 100
   ;FINIT          ; defaut instruction if the fpu stack isn't clean
   fild source     ; load source integer
   fild var1       ; load 100
   fdiv            ; divide source by 100
   fild percent    ; load required percentage
   fmul            ; multiply 1% by required percentage
   fistp var1      ; store result in variable
   push eax
   ;--------- leave the fpu with nothing in stack
   fistp dword ptr [esp]
   fistp dword ptr [esp]
   pop eax
   mov eax, var1   
   ret
PourCent endp
; #####################





Darrel

You can always use fsave at the beginning of your procedure and frstor at the end. You can save a couple steps with fidiv var1 and fimul percent.

Regards,

Darrel

ToutEnMasm

Thanks,
seem more simple like that
Quote
;################################################################
PourCent proc source:DWORD, percent:DWORD
;    invoke   PourCent,Xresolution,80         ;80 % de Xresolution
   LOCAL var1:DWORD
   mov var1, 100   ; to divide by 100
   fild source     ; load source integer       + stack
   fidiv var1      ; divide source by 100
   fimul percent   ; multiply 1% by required percentage
   fistp var1      ; store result in variable  - stack
   mov eax,var1    ;return result by eax   
   ret
PourCent endp
; #####################

raymond

QuoteYou can save a couple steps with fidiv var1 and fimul percent

You don't really save any steps because those steps would still be taken internally by the FPU. However, you do save a lot of useless typing!!! :wink And it provides for cleaner code. :clap:

In ToutEnMasm's first post, the ending fistp dword ptr [esp] instructions were also useless typing because there was nothing left in those FPU registers. That was taken care of by the fdiv and fmul instructions which, without parameters, are equivalent to performing the operation and popping the stack.

Whenever necessary, the cleanest way of clearing the top FPU register is still fstp st which does not need any conversion to other data types nor needs any memory access.

The fsave and frstor instructions are very slow and require memory access. They must be avoided unless it is absolutely necessary to preserve whatever data is in the FPU registers while having access to free registers to perform required operations.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com