problem when trying to calculate the radius of a circle using Floating point

Started by starsiege, May 16, 2009, 06:06:54 PM

Previous topic - Next topic

starsiege

Hi guys im working on a problem that requires the user to enter the radius of a circle. the program then computes it and prints it out.

im supposed to use the WriteFloat; and ReadFloat  commands from irvine library as well as the fldpi for the pi constant.


here is my code. it prints out a value and im not sure its right...also..is there way for me to convert this floating point answer and print it as a decimal one?
im new to the floating points in masm so pardon any silly mistakes i might have made. thanks in advance :)



INCLUDE Irvine32.inc
INCLUDE macros.inc

.data
prompt1 BYTE " Please enter the radius of the circle",0
prompt2 BYTE " The area of the circle is",0
radius REAL8 ?
area   REAL8 ?

.code
main PROC
finit

mov edx, OFFSET prompt1
call writeString
call crlf
call crlf

call ReadFloat
fld st
fmul
fldpi
fmul

;call ShowFPUStack

;mov edx, OFFSET prompt2
;call writeString

call Writefloat

call crlf
ret
main ENDP

END main


the answer i get is in the form of  +1.2566370E+001 or similar..


ps; also is there a way to load floating point values from the stack into memory operands? if so how?

starsiege

lol! seems like im getting the correct answer!  :red


so my only question now is how to get this floating point number displayed in decimal. :)


dedndave

that is decimal
it is a perfectly acceptable answer for class, i am sure
+1.2566370E+001 is:
+1.2566370 x (10^+001)
or, 12.56637 (sq units)
i assume you entered 2 (units) for the radius

MichaelW

starsiege,

As far as I can tell, judging from the ppt here, your code does everything necessary. I don't have the Irvine ReadFloat and WriteFloat procedures, so to test your code I substituted functions from MSVCRT.DLL by using the MASM32 import library and include file.

INCLUDE Irvine32.inc
INCLUDE macros.inc

;--------------------------------------------------------
; This includes the prototypes for the msvcrt functions.
;--------------------------------------------------------

include c:\masm32\include\msvcrt.inc

.data
prompt1 BYTE " Please enter the radius of the circle",0
prompt2 BYTE " The area of the circle is",0

;-------------------------------------------------------
; This is the format string for scanf. For scanf the 'f'
; type character specifies that the associated argument
; is interpreted as a float (REAL4), so to force the
; function to interpret the argument as a double, the
; type character must be prefixed with 'L'.
;-------------------------------------------------------

format1 BYTE "%Lf",0

;---------------------------------------------------------
; This is the format string for printf. For printf the 'f'
; type character specifies that the associated argument
; is interpreted as a double (REAL8).
;
; The "10" effectively places a newline character in the
; format string, which does essentially what calling the
; crlf procedure does.
;---------------------------------------------------------

format2 BYTE "%f",10,0   ; format string for printf


radius REAL8 ?
area   REAL8 ?

.code
main PROC
finit

mov edx, OFFSET prompt1
call writeString
call crlf
call crlf

;call ReadFloat

invoke crt_scanf, ADDR format1, ADDR radius

;--------------------------------------------------
; I assume that ReadFloat leaves the value read on
; the FPU stack in ST. Because scanf stores the
; value read at the address that it was passed, we
; must specifically load the radius into the FPU.
;--------------------------------------------------

FLD radius    ; st=radius

fld st        ; st=radius,st(1)=radius
fmul          ; st=radius*radius
fldpi         ; st=pi,st(1)=radius*radius
fmul          ; st=pi*radius*radius

FSTP area     ; store result, st-st(7) now empty

;call Writefloat

;-------------------------------------------------
; The printf function expects fp arguments to be
; passed on the (program) stack, and invoke knows
; how to do this.
;-------------------------------------------------

invoke crt_printf, ADDR format2, area

call crlf
ret
main ENDP

END main

The batch file that I used to assemble, link, and execute:

set lib=c:\masm32\lib;c:\irvine32
set path=c:\masm32\bin;%path%
ml /c /coff starsiege_test.asm
pause
Link starsiege_test.obj irvine32.lib kernel32.lib msvcrt.lib /SUBSYSTEM:CONSOLE
pause
starsiege_test.exe
pause


Please enter the radius of the circle

2
12.566371


eschew obfuscation