Floating Point calculation

On to the next section: floating point calculation.  I'm a bit confused on how to do this.  I wanted to find the volume of a sphere.  Obviously, it's 3/4 PI r^3.  So, I attempted to do this:

      EXTRN      getfp: NEAR, putfp: NEAR
      include mymacros.txt
      .MODEL      SMALL
      .586
      .STACK      100h

      .DATA
      radius      dd ?
      volume  dd 1
      .386

      .CODE
SPHERE      PROC
      startup
      display      "Please enter a radius: "
      infp

      fld      st                        ; load user input
      fmul
      fldpi      
      fmul
      fmul
      fmul      4
      fdiv      3

      display "The volume of the sphere is: "      
      outfp

      endup
SPHERE      ENDP

END      SPHERE
luna621Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

luna621Author Commented:
error A2001: immediate operand not allowed
error A2001: immediate operand not allowed

It's from these:

     fmul     4
     fdiv     3
luna621Author Commented:
Okay, some changes to code:

      EXTRN      getfp: NEAR, putfp: NEAR, dec2fp: NEAR, fp2dec: NEAR, bin2dec: NEAR
      include mymacros.txt
      .MODEL      SMALL
      .586
      .STACK      100h

      .DATA
      radius      dd ?
      volume  dd 1
      four      dd 4
      three      dd 3
      .386

      .CODE
SPHERE      PROC
      startup
      display      "Please enter a radius: "
      infp

      fld      st                        ; load user input
      fmul
      fldpi      
      fmul
      fmul
      fmul      four
      fdiv      three

      display "The volume of the sphere is: "      
      outfp

      endup
SPHERE      ENDP

END      SPHERE
luna621Author Commented:
If you need to see these let me know: getfp.asm, putfp.asm, dec2fp.asm, fp2dec.asm, bin2dec.asm, mymacros.txt
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

luna621Author Commented:
Okay, I changed the middle to this:

      radius      dd ?
      four      dd 4
      three      dd 3
      .386

      .CODE
SPHERE      PROC
      startup
      display      "Please enter a radius: "
      infp

      fstp      radius                        ; load user input
      fld      radius
      fld      radius
      fmul
      fldpi      
      fmul
      fld      four
      fmul
      fld      three
      fdiv

      display "The volume of the sphere is: "      
      outfp
mzvikaCommented:
what is the problem? compilation?

'fstp' will pop the value of st0 from the "stack".
so, your calculation above is

(radius ^ 2) * pi * 4 / 3

you need another
    fld radius
    fmul

or use 'fst' instead of 'fstp', and add just another 'fmul'.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
luna621Author Commented:
Oops, sorry.  Compiles fine.  But, the number is wrong.
luna621Author Commented:
fstp      radius                        ; load user input
      fld      radius
      fld      radius
      fld      radius
      fmul                              ; r^3
      fldpi      
      fmul                              ; PI(r^3)
      fld      four
      fmul                              ;4PI(r^3)
      fld      three
      fdiv                              ;(4/3)PI(r^3)
luna621Author Commented:
This didn't work either...

      fst      radius                        ; load user input
      fld      radius
      fld      radius
      fld      radius
      fmul                              ; r^3
      fldpi      
      fmul                              ; PI(r^3)
      fld      four
      fmul                              ;4PI(r^3)
      fld      three
      fdiv                              ;(4/3)PI(r^3)
luna621Author Commented:
Got it!

      fstp      radius                  ; load user input
      fld      radius
      fld      radius
      fmul
      fld      radius
      fmul                        ; r^3
      fldpi
      fmul                        ; PI(r^3)
      fld     X                  ; X = 4
      fmul                        ; 4PI(r^3)
      fld      Y                  ; Y = 3
      fdiv                        ;(4/3)PI(r^3)
mzvikaCommented:
good job
luna621Author Commented:
I'm gonna start on the next section.  But, I probably won't finish today.  Will continue tomorrow, and will post if I have any q's again.  Thanks for helping all the time :-)
grg99Commented:
Um, don't your constants have to be floating-point numbers?    "dd" if I recall generates 32-bit integers.
luna621Author Commented:
Ah, I knew something was funny with my program.  I know constants shouldn't be used, so how would I get the 3 and 4 in my program?  I tried:

 fld     4

and of course, there was an error.  Thanks :)
mzvikaCommented:
not accurate.
the compiler will generate floating point constants when asked to:

mydword   dd 1023             ; DWORD
myqword   dq 0               ; QWORD
myfloat      dd 1.5                ; low-precision "float" is of a dword size
mydouble   dq 3.141592654  ; "double" is qword size

if you want to force a floating point variable with an integer value, use '3.0'.
this, at least, works for NASM...
luna621Author Commented:
Ah, I understand.  Thanks.  (I found a chapter about drawing!!  I'm going to try that now.  Will probably be posting a new question soon if I don't get it.) :-)
mzvikaCommented:
graphics - now, this is the stuff!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.