flat assembler
Message board for the users of flat assembler.

Index > Main > Float to Long

Author
Thread Post new topic Reply to topic
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Hello,

i've written a little code in asm to convert a C float to C long type for my kernel but it returns, for example, 268370267 instead of 29, when i try to convert 29.07103 to long .

Here's the code :
Code:
ftol:
push ebp
mov ebp, [esp+32]
fld dword [ebp]
fistp dword [ebp+32]
mov eax,[ebp+32]
pop ebp
ret
    


Otherwise, where can i have a "really readable" doc' about the FPU instructions, their operands, simple examples...

Thanks =)
Post 31 Mar 2008, 16:57
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Code:

float Number_Float = 29.07103 ;
long Number_Long ;                        /* 32 bit ?    64bit ?   */

__asm {
            fld      [Number_Float]
            fistp   dword [Number_Long]             /* 32bit */
           }
or 

fistp qword [Number_Long]              /* 64bit */
    
Post 31 Mar 2008, 17:43
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Thanks, and in Fasm ? =)
Post 31 Mar 2008, 17:47
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Code:

Number_Float  DD 29.07103
Number_Long  DD ?


fld          [Number_Float]
fistp        [Number_Long]
    
Post 31 Mar 2008, 17:53
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
White-spirit wrote:
Otherwise, where can i have a "really readable" doc' about the FPU instructions, their operands, simple examples...


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

_________________
(French only) http://dap.developpez.com
Post 31 Mar 2008, 17:57
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
just to anticipate the Question/
Code:
num_float dd ??
num_long dd 29

fild [num_long]
fstp [num_float]
    

it will convert a dword in float
Post 31 Mar 2008, 17:59
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Thanks but all the codes don't work, I use a little 32 bits kernel written by me on a PC emulator called Bochs and i get this error message on Bochs : "math_abort: MS DOS compatibility FPU exception" .
Post 31 Mar 2008, 18:42
View user's profile Send private message Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
1) Try to insert FINIT instruction at start of FPU operation
2) Try in Real Hardware

Do you Have a CPU with FPU ?
I don't use emulator,if in real hardware run then is emulator problem
Post 31 Mar 2008, 19:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
to test this kind of code, only a dos code is enough.

org 100h ==> code.com

it's largelly enough.. Smile
Post 31 Mar 2008, 19:05
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
DJ Mauretto wrote:
1) Try to insert FINIT instruction at start of FPU operation


Thanks all, FINIT is what i needed =)
Post 01 Apr 2008, 08:05
View user's profile Send private message Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Here's my function for those who need it :
Code:
public ftol

long dd ?

ftol:
push ebp
mov ebp, esp
finit
fld dword [ebp+8]
fistp [long]
mov eax,[long]
pop ebp
ret
    
Post 01 Apr 2008, 08:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17272
Location: In your JS exploiting you and your system
revolution
White-spirit wrote:
Here's my function for those who need it :
Code:
public ftol

long dd ?

ftol:
push ebp
mov ebp, esp
finit
fld dword [ebp+8]
fistp [long]
mov eax,[long]
pop ebp
ret    
You can even eliminate the long declaration
Code:
public ftol

ftol:
push ebp
mov ebp, esp
finit
fld dword [ebp+8]
fistp dword [ebp+8]
mov eax,[ebp+8]
pop ebp
ret
    
And if you want you can to make it stdcall compliant use "ret 4".

Also you can eliminate the ebp usage:
Code:
public ftol

ftol:
finit
fld dword [esp+4]
fistp dword [esp+4]
mov eax,[esp+4]
ret
    
Post 01 Apr 2008, 08:14
View user's profile Send private message Visit poster's website Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Yeah thanks =)
Post 01 Apr 2008, 11:44
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
I couldn't resist Very Happy

Of course the first one doesn't compile because FASM doesn't allow movq r64,xmm. But its legal, so here it is:
Code:
        mov     rcx,2364.01
        fastcall ftol,rcx

ftol:
        movq    xmm0,rcx
        cvtpd2dq xmm0,xmm0
        movq    rax,xmm0
        ret
    


Actually if you have your data in a memory location you can use a simple macro.

Code:
macro ftoa a
{
        cvtpd2dq xmm0,dqword[a]
        movq    qword[a],xmm0
}

        ftoa my_float

align 16
my_float        dq      1234.56
    
Post 01 Apr 2008, 11:52
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
How to get , for example , the int value 0147 of the float number 29.0147 please ? :
Code:
dec_val:
finit
fld dword [esp+4]
fist dword [esp+4]
fsub dword [esp+4]
;An instruction to transform 0.0147 to 147
fistp dword [esp+4]
mov eax,[esp+4]
ret 4
    


EDIT : Misled on the use of FABS , sorry , but how to do please ? thanks
Post 01 Apr 2008, 12:00
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Post 01 Apr 2008, 12:12
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17272
Location: In your JS exploiting you and your system
revolution
White-spirit wrote:
How to get , for example , the int value 0147 of the float number 29.0147 please ?
For the fraction you can just multiply the difference by, say, 10000.0 and then store it as an integer. But make sure you set your rounding mode to round down.
Post 01 Apr 2008, 12:34
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Ah, sorry, Extract only takes out the exponent and this is not what you need.
Revolution is right => (29.0147-29)*10000=147 Smile
Post 01 Apr 2008, 12:44
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.