NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: nobody on December 20, 2008, 02:32:44 AM

Title: Returning a 64-bit floating point number?
Post by: nobody on December 20, 2008, 02:32:44 AM
Hi,

If EAX holds the (32-bit) address of a 64-bit floating point number in memory, how do I load the number into the floating point register to return it as the value of a function?

Michael
Title: Re: Returning a 64-bit floating point number?
Post by: Frank Kotler on December 20, 2008, 03:20:18 PM
fld qword [eax]

"dword" for "single precision" (32-bit) floats. "fild" to load an integer into FPU. "fst"/"fstp"/"fist"/"fistp" to get 'em back into memory (p = pop the FPU stack). Nice tutorial here:

http://www.website.masmforum.com/tutorials/fptute/index.html (http://www.website.masmforum.com/tutorials/fptute/index.html)

I've found the FPU kinda "weird" to use (I've used it very little), being "stack based". Raymond's tut looks at it like the cylinder of a revolver (as in firearm), rather than a stack. Might be an easier way to "get" it...

My understanding is that the FPU is considered "obsolete" these days, and the way to do floating point is with MMX (or one of those new-fangled MMX/XXM/SSEn.n things). I know nothing about 'em. I'll bet there's an introductory tut that'll be easier than RTFM... Haven't looked...

Best,
Frank
Title: Re: Returning a 64-bit floating point number?
Post by: nobody on December 28, 2008, 01:46:15 AM
Hi,

I followed the link above and pulled this out of Chap. 2, but I'm uncertain about usage. It looks like the FILD instruction expects the data to be on the stack, but I haven't a clue what function "ptr" serves.

Michael

================

For QWORD values, such as obtained from a signed multiplication with the result in the EDX/EAX register pair, the sequence would need to be:

push edx
    push eax
    fild qword ptr [esp]
    pop eax
    pop edx
Title: Re: Returning a 64-bit floating point number?
Post by: Frank Kotler on December 28, 2008, 04:08:53 AM
Ah, yes. Masm syntax. Well, "Intel Syntax", strictly speaking. Does nothing but provide clarity/bloat (choose one). Just leave it out, for Nasm.

Masm:

inc dword ptr foo

Nasm:

inc dword [foo]

Masm:

mov ebx, offset foo

Nasm:

mov ebx, foo

Watch out for this! While:

mov ebx, foo

is "legal" in both, it means two different things! In Masm, that's "contents of foo" - "[foo]" in Nasmese - *if* "foo" is a variable. The brackets are optional, as is the "dword ptr" - Masm remembers what size you said "foo" was. "mov ebx, [foo]" and "mov ebx dword ptr foo" or "[foo]" would do the same. If it's, say, "foo equ 3", then it's a plain immediate move in both. Confusing? It gets easier with a little practice, I've found...

Sorry I had to send you off to a Masm-syntax tut - should have warned you - but that's the best I'm aware of.

Best,
Frank