I'm trying to use the FCOS instruction in HLA.  I have searched but all of the previous topics have been in MASM or some other assembly language.

I understand FCOS pops the stack, calculates the cosine of the value and pushes the result back on the stack.

So far I have tried declaring a floating point unit:

static

r32 :  real32  := 0;


To test whether it is working, I tried to take the cosine of r32, and output the result with stdout.put.  If correct, the output should be equal to 1.  However I'm unsure how to use the FCOS instruction.  I've tried moving r32 into one of the 32 bit registers, pushing it onto the stack myself, then writing the instruction:

FCOS ();


Then popping the stack, moving the value back to r32 and outputting the value.

I will also be using FSIN but no doubt, once I find out how to use FCOS, I will have no problems with FSIN.
Posted on 2008-02-27 14:26:04 by Slugsnack
I have absolutely no experience working with HLA but from programming in Assembly, I can tell you that FPU registers are 80-bits long and their values don't fit in General Purpose Registers that are 32-bits in IA-32 unless you save the contents of the TOS (Top of the FPU Stack) into a DWORD memory pointer as an integral value.
Posted on 2008-02-27 17:20:42 by XCHG
Although the FPU works internally with 80-bit floats, it can work with 32-bit floats (REAL4), 64-bit floats (REAL8), or 80-bit floats (REAL10) to and from memory. All three formats are standard IEEE compliant.

Slugsnack: You may be able to learn how to use the FPU within HLA from AoA (Art of Assembly) from the same author who developed HLA. Although the following tutorial is geared to MASM, you may also acquire some useful FPU knowledge from it:
http://www.ray.masmcode.com/fpu.html
Posted on 2008-02-27 23:33:17 by Raymond
Thanks a lot, both of you.

I figured it out eventually after doing a little more reading in AoA (I am ploughing through the whole thing and hadn't got to the FPU instruction set part yet).

I hadn't realised that the float uses a different stack to the normal registers.  Here's the code I tested that works:
Program Distance;

#include( "stdlib.hhf" )

static

i32 :   real32   := 1.570796327;

Begin Distance;
fld(i32);
fcos;
fstp(i32);
stdout.put(i32,nl);

End Distance;


//edit :

Now I'm working on converting degrees to radians.  To do this, I need to divide by 360 then multiply by 2 pi.  I'm really uncomfortable with using FPU instructions.  This is what I have so far:
Program Distance;

#include( "stdlib.hhf" )

static

i32 :   real32;

Begin Distance;

    stdout.put("Input value of theta:",nl);
    stdin.get(i32);
    fld(i32);
    fld(2.0);
    fldpi();
    fmul();
    fld(360.0);
    fdiv();
    fstp(i32);
    stdout.put(i32);
End Distance;


If anyone could point me in the right direction, that'd be great.

//edit :

Sorted !  This is the new code:
Program Distance;

#include( "stdlib.hhf" )

static

i32 :   real32;

Begin Distance;

    stdout.put("Input value of theta:",nl);
    stdin.get(i32);
    fld(i32);
    fldpi();
    fmul();
    fld(180.0);
    fdiv();
    fstp(i32);
    stdout.put(i32);
End Distance;


//edit :

All is going well now !  Was just wondering though whether there is a floating point square instruction.  I'm doing two FPU stack pushes then FMUL but if there was some sort of instruction to do that in one go, it'd make my code easier on the eyes :p
Posted on 2008-02-28 15:06:28 by Slugsnack
The MASM syntax to square a value which is already in the FPU's top register is as simple as:
fmul st,st

Those are some of the "tricks" which you may find in the suggested tutorial.
Posted on 2008-02-28 19:52:39 by Raymond
Hmm yeah I asked because I was curious as to how you would do a FPU to the power of a non-integer.  As far as I know, the only way that can be done is with FSQRT instruction but that is only power half.  What would you do if you had to do x^(3/4) for example ?  I can think of a few algorithms that would diverge to the correct value.  I'm purely curious now :p  I just finished my physics app last night.
Posted on 2008-02-29 10:08:52 by Slugsnack
Could I ask something completely off topic here? Whats' with the blue font color? :lol:
Posted on 2008-02-29 10:23:22 by XCHG
What blue font color?
Posted on 2008-02-29 11:00:23 by JimmyClif
Means I can pick out my own posts easier and contrasts other people's post's colour making it easier on my eyes to tell who's post it is.  Not so much of a problem here but I got used to it.  And I like to spice up the screen by making it colourful !
Posted on 2008-02-29 12:43:36 by Slugsnack
how you would do a FPU to the power of a non-integer


You can find a relatively good description of how to compute any power in Chapter 11 of the tutorial:
http://www.ray.masmcode.com/tutorial/fpuchap11.htm

As you will see, it's not a "simple" procedure.
Posted on 2008-02-29 20:55:50 by Raymond