News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How can I store the floating point form of 100 into a variable?

Started by ProtoflareX, May 03, 2016, 02:32:52 AM

Previous topic - Next topic

ProtoflareX

Let's assume that I have a variable by the name of tempStoreFP that can store a value of the REAL8 data type. Can anybody provide an example of a line of code that stores the floating point form of 100 into tempStoreFP? Storing an integer into a variable is a trivial matter, but due to my inexperience with assembly, I do not know how to do this with floating point numbers.

qWord

.const
    myConstValue REAL8 100.0

; ...

.data
    myRWValue REAL8 123.0E-45


For instructions to process FP values see raymond's FPU tutorial
MREAL macros - when you need floating point arithmetic while assembling!

raymond

Before you even think about using floating point instructions in assembly, you should at least have a peek at some tutorial such as available at http://www.ray.masmcode.com/fpu.html.

Your requested code could be as follows or some variant of it:

mov eax,100
push eax
fild dword ptr [esp]
fstp tempStoreFP
pop eax


Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

ProtoflareX

Quote from: raymond on May 03, 2016, 03:11:00 AM
Before you even think about using floating point instructions in assembly, you should at least have a peek at some tutorial such as available at http://www.ray.masmcode.com/fpu.html.

Your requested code could be as follows or some variant of it:

mov eax,100
push eax
fild dword ptr [esp]
fstp tempStoreFP
pop eax


Out of curiosity, can you explain the purpose of the "fild dword ptr [esp]" line? I know that fild loads an integer onto the stack, but I don't understand the purposes of the instructions beyond that.

raymond

i) The FPU can only communicate with memory or its own internal registers, not with any of the usual ALU registers.
The first suggested instructions were thus to load the integer value 100 somewhere in memory (i.e. the stack in this case).

ii) The fild instruction then loads the integer to the FPU from memory and automatically converts it to the float format (the only format known to the FPU registers).

iii) The value of 100 is then stored in memory in the floating point format with the fstp instruction. Because the memory variable "tempStoreFP" was declared as a qword, the float format would then be the "double" 64-bit type. The "p" of the fstp instruction releases the FPU register which was used to retrieve the integer from memory.

iv) the last instruction was to restore the stack which was used in this variant to temporarily hold the integer for the transfer to the FPU.

I hope you can appreciate the suggestions to peek at some related tutorial before diving into FPU programming in assembly.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

jj2007

Quote from: ProtoflareX on May 03, 2016, 03:17:20 AM

mov eax,100
push eax
fild dword ptr [esp]
fstp tempStoreFP
pop eax


Out of curiosity, can you explain the purpose of the "fild dword ptr [esp]" line? I know that fild loads an integer onto the stack...

Same without changing eax:
push 100   ; move the dword 100 into a memory location, more precisely: lower the stack by 4 bytes and poke the 100 there
fild dword ptr [esp]  ; get the 100 from memory and throw it onto the FPU, into ST(0)
fstp tempStoreFP  ; poke it into another memory location called tempStoreFP; move previous value into ST(0)
add esp, 4  ; keep the stack balanced


And Ray's advice is a serious one: READ THAT TUT. 13 years old, fascinating read, absolutely up-to-date, and the best you could get for your money even if it would cost something ;)

Btw you may find people who argue that the fpu is out, and SIMD is in. Truth is that SIMD has more choices, but speedwise the fpu is absolutely the same on modern cpus, probably because they use the same circuits. SIMD is only the better option if you do a lot of graphics with parallel processing.

ProtoflareX

As I continued to work on my current program, I found myself in need of assistance again. I would like to pop a value off of the FPU and store it into the first index of an array whose offset has been stored into edx.

If I was working under the following circumstances:
1. Dealing with integers
2. The value I want to move into the array is stored in eax
3. The offset of the array has already been stored in edx

Then I think this would be accomplished with the following line of code:
mov     [edx], eax

But how do I replicate that with a value that is stored on the floating point stack?

raymond

Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

K_F

Nope...
EDI -> points to base/beginning of array
EDX -> Index (Position) into array

For register to memory moves...
Mov  [edi+edx*4], eax

From the FPU to integer
fistp  DWORD PTR [edi+edx*4] - FPU to memory pointed by edi and edx
fist    DWORD PTR [edi+edx*4]

For REAL8's it's just.. but you must make sure edi+edx is 8 byte aligned
fstp  QWORD PTR [edi+edx] - FPU to memory pointed by edi and edx
fst    QWORD PTR [edi+edx]

You cannot move from the FPU to a CPU register, except with the flags
Read Raymonds FPU stuff ;)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'