News:

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

Sqr Rts & Decimals

Started by Titan, January 08, 2005, 09:59:12 PM

Previous topic - Next topic

Titan

I have a few math problems, I've worked in VB6 in the past, and math is VERY easy.  Assembly seems to be a challenge when it comes to even the simplist math equations.

1.) How do decimals work if all hex values have only an equivalent integer value?
2.) Does anyone have a square root macro?  Or is there somewhere I can find it?

Sorry I don't know much about this stuff.

Thanks,
Titan

dioxin

Titan,
   I don't know VB but you'll probably find that the "decimal" numbers in VB are just the native floating point numbers used by the Floating Point Unit.

   Not all numbers are integers in ASM.

   To find a square root, load the number, execute the FSQRT instuction and store the result. For some maths operations it's easier in ASM than with VB!

!fld  number               'get the number
!fsqrt                     'calculate its squre root
!fstp answer               'store the answer


         I'm sure there was an FPU tutorial around here somewhere, someone else will point you to it. Until then I suggest you get the Pentium Manuals from Intel.
http://www.intel.com/design/pentium4/manuals/index_new.htm

   They'll explain all the FPU instructions and the formats of all the FP numbers. Though a good tutorial makes them easier to digest.

Paul.


Robert Collins

Quote from: Titan on January 08, 2005, 09:59:12 PM
......if all hex values have only an equivalent integer value?.......

Actually, binary does have an equivalent to decimal fractions.

1.5 = 1.1 in binary but this has nothing to do with your question.

Ratch

Titan,
QuoteDoes anyone have a square root macro?  Or is there somewhere I can find it?

     A MACRO defines inline code when called.  If you mean a procedure or subroutine to find the square root, then the following is one of many ways to do it.  Ratch

http://www.bmath.net/bmath/index.html

raymond

Titan,

If you are only interested in the square root of an integer returned also as an integer, use the following:

fild number
fsqrt
fistp root

number must be a memory variable declared either as a 15-bit WORD, a 31-bit DWORD or a 63-bit QWORD. The reason for lowering the number of bits by 1 is that they are always considered as signed integers by the FPU. If the most significant bit is set, it becomes a negative number and the FPU will return an error when instructed to extract its square root.
root must also be a memory variable declared either as a WORD, DWORD or QWORD but must be of sufficient size to take the answer.

The returned square root would be rounded to the nearest integer with the given code. If you need a truncated square root, several more instructions would be required.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Titan

Hi everyone, thanks for the help, I got it working:

.elseif eax==IDC_BTN1
     fild number
     fsqrt
     fistp root
     invoke wsprintf,  ADDR buf, ADDR fil, root
     invoke MessageBox,hWnd,addr buf,addr AppName,MB_OK

Question is - is there an easier way to play with integers than always having to convert them using wsprintf?  Why does MASM always like to display everything as ASCII characters?  :(

EDIT: When I make number "20"... it says the square root is 4. :-\  How am I suppose to display decimal values if I'm having to convert to integers with wsprintf? :'(

dioxin

Titan, 
  FILD and FISTP  load integers and store integers.
  If you want to load a non-integer, use FLD
  If you want to store a non-integer use FSTP

Your code will load 20 as an integer and convert it to 20.00000 in the FPU
It then works out the SQRT of 20.00000 = 4.472....
It then stores the integer part of that result, = 4

So when you print it you only get the 4.


Paul.

Titan

Yeah, but how would I display that decimal in my messagebox.  I have this:

.elseif eax==IDC_BTN1
fld number
fsqrt
fstp root
invoke wsprintf,  ADDR buf, ADDR fil, root
invoke MessageBox,hWnd,addr buf,addr AppName,MB_OK

But the message box comes up as 457859778. :(

raymond

Titan,

The tutorial at the link given to you by MichaelW has been prepared to explain all those things. Maybe you should have a look at it and ask questions if the explanations are not clear enough.

Alternatively, if you have the MASM32 package, you could use the FpuSqrt and FpuFLtoA functions of the FPULIB library if you want to get the fractional portions of your square roots. Make sure you read the Help file to fully understand the requirements of the parameters for each function.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com