News:

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

Problems calculing Bessel function in assembler.

Started by hanzo86, November 01, 2009, 08:19:57 PM

Previous topic - Next topic

hanzo86

Hi everybody!

I'm programming a implementation of the Bessel j0 function. Pseudo code given to me that I have to translate to assembles is:

float bessj0(float x)
{
   float ax,z;
   double xx,y,ans,ans1,ans2;
   if ((ax=fabs(x)) < 8.0) {
      y=x*x;
      ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7+y*(-11214424.18+y*(77392.33017+y*(-184.9052456)))));
      ans2=57568490441.0+y*(1029532985.0+y*(9494680.718+y*(59272.64853+y*(267.8532712+y*1.0))));
      ans=ans1/ans2;
   } else {
      z=8.0/ax;
      y=z*z;
      xx=ax-0.785398164;
      ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4+y*(-0.2073370639e-5+y*0.2093887211e-6)));
      ans2=-0.1562499995e-1+y*(0.1430488765e-3+y*(-0.6911147651e-5+y*(0.7621095161e-6-y*0.934935152e-7)));
      ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
   }
   return ans;
}


I have assembled this pseudocode to the follow assembly function. I had debugged the program step by step and the results given by the function are correct. The problem that I have is if I delete two lines in my code that invoke a MessageBox (I use these lines for debugging) the function always returns 0 for values of x >= 8.0 If I don't remove these two MessageBox lines of the code the functions works perfectly, What am I doing wrong?


Thanks for any help. And sorry about my mistakes in English. ;)

dedndave

you had posted some code earlier - it is gone now - i was going to have a look at it
if you have a long piece of code like that - put it in an attachment (ZIP file)
below the box where you type your reply is an Additional Options link - click that and upload the ZIP'ed code

ecube

use the FPULIB functions in the masm32 sdk to do this, has all the functions to do the math easily. it's only missing FPUMOD which is just an altered div function,i have code somwhere if you need it.

hanzo86

Sorry for erase of the code function. I have been cleaning the code for been attached here. Now is more readable and understandable I think. As I said before the code is the masm implementation of the pseudocode given in the first post.

dsouza123

I recoded the psuedo code in Pascal, compiled, then commented the code produced.

It may give you some ideas, it only does a fwait/wait after a store.

Other than x and ans, what about using double for everything ?

Before your first MessageBox


fld     qword ptr [off_004044A4] p[  0]
fdiv    qword ptr [off_00404474] ax
fstp    qword ptr [off_0040447C] z =
fwait

fld     qword ptr [off_0040447C] z
fmul    qword ptr [off_0040447C] z
fstp    qword ptr [off_0040448C] y =
wait

fld     qword ptr [off_00404474] ax
fsub    qword ptr [off_004047A4] p[ 96]
fstp    qword ptr [off_00404484] xx =
fwait

The first messagebox would have been here

; ans1 calc following (only the first 3 instructions shown here)
;======================================
fld     qword ptr [off_0040448C] y
fmul    qword ptr [off_004047E4] p[104]
fadd    qword ptr [off_00404824] p[112]

hanzo86

I have removed unnecessary fwait instructions but it still doesn't work :S

jj2007

First, it would be very helpful if you posted a complete program starting with include \masm32\include\masm32rt.inc rather than trusting that we all have so much time that we enjoy adding these little bits for you.

Second, if your result are incorrect after calling MessageBox, you shouldn't be surprised. MessageBox trashes the FPU. Use print str$() instead.

hanzo86

My results are INCORRECT if I don't call MessageBox. If I call it in the two lines of the invoke process then the results are incorrect.

jj2007

Ok, post the complete code, and we may be able to help you. By the way, editing posts to let code disappear is considered antisocial in this forum.

hanzo86

Ok. I'll prepare it and I'll send it. Sorry for deleting code. I delete it because I considered that the post is very long so I edit the post and put the code in attachment. It's the same code. Sorry about that.

hanzo86

I have resolved the problem that I have. I was invoking in the while loop that invoke my function some text functions that initialize the FPU. I saved the FPU state before calling these methods and my program works. :D

Thanks anyway for your help :)