The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: tomson123 on February 02, 2012, 01:21:17 PM

Title: FMUL returning 0 - why?
Post by: tomson123 on February 02, 2012, 01:21:17 PM
I'm pretty new to asm... Basically, I just need to multiply two floating point numbers.
For some reason my fmul in my code is always returning zero... I can't seem to do it right and I'm trying my best for half a day now...
Fadd and Fsub are working all right, but when I change either of them to fmul, i get 0  :(
Here's some sample code to show what I mean:

.486
.model flat, stdcall
.data
number1   QWORD 21
number2   QWORD 2
result qword ?

.code

   finit

   fld number1
   fld   number2            
   fmul                           
   fst result   

end

I'm working on Windows 7, Intel's P7350 if that would be any help...
If you need any additional info, I will provide it.

Thank you in advance for your patience...
Title: Re: FMUL returning 0 - why?
Post by: FORTRANS on February 02, 2012, 02:04:13 PM
Hi,

   How are you determining that the result is zero?  Could it just
be very small?  You have "QWORD 21" which will be interpreted
as an integer by the assembler,  not as a 21.0 float.  (Unless you
use FILD.)  That would be a very small real value when you use
FLD.

HTH,

Steve
Title: Re: FMUL returning 0 - why?
Post by: jj2007 on February 02, 2012, 02:12:36 PM
Steve is right. Use this:
   fild number1
   fild number2           
   fmulp                           
   fistp result   
Title: Re: FMUL returning 0 - why?
Post by: tomson123 on February 02, 2012, 02:25:49 PM
Thank you for you intrest :)
To be honest, i haven't really tried FILD... In every FPU tutorial, the syntax was FLD/FILD, without any explanation why, so i just thought of using the shorter version, for an obvious reason.
Unfortunately, this does not work either... I don't get 0 anymore, but almost the "exact" opposite now... It's some horrific >10digit number...
As to determining the value of "result", I'm using Visual Studio 2010 (asm is a part of a bigger, C++/winapi project)  and simply adding watch to the variables/registers i need.

If I have to, I could use some different variable types, but I need to return them as double/float later on, and I'm not sure if it would work...
Title: Re: FMUL returning 0 - why?
Post by: jj2007 on February 02, 2012, 02:51:11 PM
1. Select the Visual Studio folder
2. Press "delete" :lol

include \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
.data
number1   QWORD 21
number2   QWORD 2
result   QWORD ?
resultR8   REAL8 ?
   Init

   fild number1
   fild number2
   fmulp
   fst resultR8
   fistp result
   Print Str$("The result as double is \t%f\n", resultR8)
   Inkey Str$("The result as integer is \t%i\n", result)

   Exit
end start

The result as double is         42.00000
The result as integer is        42

Seriously: It works. What is odd though is that the assembler does not throw at least a warning when you try to fld a qword ::)

P.S.: The lazy variant is Print Str$("The result as integer is \t%i\n", number1*number2)
Title: Re: FMUL returning 0 - why?
Post by: tomson123 on February 02, 2012, 03:00:15 PM
Quote from: jj2007 on February 02, 2012, 02:51:11 PM
1. Select the Visual Studio folder
2. Press "delete" :lol

True that :bg

Frankly, I really thought I would get away with it :) My project isn't really that hard, so i reckoned VS would be sufficient. I suppose i got what was coming to me :)
And no, it didn't throw any warnings  :eek
Thanks a lot, looks like I have to get a decent asm environment. Could you suggest somenting? Easy to handle is my priority :)

Again, thank you for all your help :)
Title: Re: FMUL returning 0 - why?
Post by: jj2007 on February 02, 2012, 03:11:39 PM
Quote from: tomson123 on February 02, 2012, 03:00:15 PM
Thanks a lot, looks like I have to get a decent asm environment. Could you suggest somenting? Easy to handle is my priority :)

Now that could open up a nice religious-ideological debate. Some use Notepad (seriously, and they are very good coders actually), the default editor here is \Masm32\qEditor.exe, I use RichMasm (part of MasmBasic (http://www.masm32.com/board/index.php?topic=12460)), then there is RadAsm, GoAsm, VS, ... and many more. It depends strongly on personal experience and what you want to achieve. Professional programmers have different needs...
Title: Re: FMUL returning 0 - why?
Post by: dedndave on February 02, 2012, 03:30:06 PM
RadAsm seems to be pretty nice, if you like integrated environments
Title: Re: FMUL returning 0 - why?
Post by: tomson123 on February 02, 2012, 03:34:39 PM
Thank you all :)
I am currently creating a project in RadAsm (since i have heard of it before, it was my pick). I hope it will make my life easier :)

Title: Re: FMUL returning 0 - why?
Post by: dedndave on February 02, 2012, 05:32:50 PM
one nice thing about RadAsm - Ketil is continually improving it   :U
Title: Re: FMUL returning 0 - why?
Post by: raymond on February 03, 2012, 01:51:41 AM
If you intend to continue using Fpu instructions, some of the very basic knowledge about the FPU would definitely "make your life a lot easier". How about reading at least the first two chapters of:

http://www.ray.masmcode.com/tutorial/index.html

If you think it may be of some use, the entire tutorial can be downloaded from the previous page so that you can have access to it offline.