HI i am making a calculator in masm I already have the gui done and all the buttons and most functions work.  BUt i am having trouble with calculating e^x .  I have to do it using series so e^x = 1+x+x^2/2!+x^3/3!... to infinity here is my code for calculating it.  The logic is correct i think but the FPU gets out of  control or soemthing so can anyone help me please write any comments .  I know i might be doing stuff that i dont need but i was trying anything and nothing worked.  Here is part of the code to caluculate e^x  when i compile it i dont get any errors it just doesnt do anything when the user press the e^x button

.if wParam == 520                                                ;    computes e^x 
mov number9,5                                    ; number of decimals
invoke GetWindowText,hDspl,ADDR buffer,512 ;gets the number from display
finit  ;initilizes FPU
invoke FpuAtoFL,addr buffer,0,DEST_FPU ;puts the number from dispaly onto FPU
fld1                                      ;loads 1 to fpu
fadd                            ;adds 1 and x so this takes care of the 1+x of the series
fst trig                                ; stores the number in trig
mov eax,2                          ;one of the counters
finit                                                ;initilizes fpu
invoke FpuAtoFL,addr buffer,0,DEST_FPU
fstp numx                              ;stores the number x that the user entered

.while eax < 100 
mov loopcounter, eax                      ; saves outer loopcounter
finit
fld numx                    ;loads x that the user eneterd
.while (eax > 1)                  ; calculates x^n power(numerator)
fmul st,st         ;squares the number
dec eax              ;eax-1
.endw
fstp num                          ;stores the number from the loop into num
mov eax,loopcounter              ;restores the outer loopcounter
finit ;initlizes FPU
fild loopcounter                      ;loads loopcounter
.while loopcounter > 1                  ;calculates denom of the series(factorial part)
dec loopcounter            ;dec loopcounter
fild loopcounter                ;loads it to st
fmul st,st(1)                ; multiplies 2*1 first time trough the outer loop . second time trough the outer loop  3*2*1 etc.
.endw
fst denom                                    ;stores real number into denom
finit                                                ; initilizes FPU
fld num                      ;loads numerator
fdiv denom                  ;divides it by denom
fadd trig                              ;adds it to a running sum of trig
fst trig                                  ; pops the number into trig
inc eax                                  ; inc eax
.endw
invoke FpuFLtoA, 0,ADDR number9, ADDR result_0, SRC1_FPU or SRC2_DMEM
invoke SetWindowText,hDspl,ADDR result_0                  ; for displaying
mov check,0                                            ; checks for other stuff not important
mov check2,0
mov numberofdec,0
fld negative
fabs
fst decten
.endif





I think it has to do something with FPU because i am not preserving it and something with the while loop it somehow screws up the FPU. 

thank you guys for your help

Posted on 2005-11-16 02:41:37 by slim515
If you are using the FpuAtoFL function, you thus have the FPULIB. Computing the e^x would thus be a lot easier by using the FpuEexpX function in the same FPULIB, or adapting the code from the source file.

If this is strictly an exercise in computing the e^x according to the power series, your code seems to be fully loaded with errors. The best way to correct them would be to run the code in a debugger with a known input and watch how the variables progress after each operation as compared to how they should if you were performing the computation with paper and pencil.

Raymond
Posted on 2005-11-16 21:53:40 by Raymond