flat assembler
Message board for the users of flat assembler.

Index > Main > how do I use FPU

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 15:25
I have this code in masm to deal with the FPU and it works great

in this code I get a number from 2 different textboxes and then divide them and then output the results to another textbox

this is the data that is local
Code:

LOCAL variable1 :QWORD
LOCAL variable2 :QWORD
LOCAL variable3 :QWORD

LOCAL string1[20]:BYTE
LOCAL string2[20]:BYTE
LOCAL string3[20]:BYTE
    


this is the code
Code:

invoke GetDlgItemText,hWin,textbox1,addr string1,9
invoke StrToFloat,addr string1,addr variable1

invoke GetDlgItemText,hWin,textbox2,addr string2,9
invoke StrToFloat,addr string2,addr variable2

finit
fld variable1
fld variable2
fdiv
fstp variable3

invoke FloatToStr,variable3,addr string3
invoke SetDlgItemText,hWin,textbox3,addr string3
    


I am trying to convert the code to fasm

this is what I have so far but it is not working the textbox3 just says 0

this is the data (this is not local data because I have not learned how to do that in fasm yet)
Code:

v1 dq ?
v2 dq ?
v3 dq ?
v4 rb 20
    


this is the code
Code:

invoke GetDlgItemTextA,[hWin],textbox1,addr v1,100 
invoke GetDlgItemTextA,[hWin],textbox2,addr v2,100 

finit
fld [v1]
fld  [v2]
fdivp
fstp  [v3]

cinvoke wsprintfA,addr v4,"%u",[v3]
invoke SetDlgItemTextA,[hWin],textbox3,addr v4
    


basically my question is can someone show me how to first convert the text to a number and second how to use the FPU to divide. I have the third part of the code that is to convert the number to text.
thanks
Post 05 Sep 2013, 15:25
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 05 Sep 2013, 16:07
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:58; edited 1 time in total
Post 05 Sep 2013, 16:07
View user's profile Send private message Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 16:26
thanks it does work but I need you to please help me modify it a little. this is what I did
Code:

 mov [v1],10.1
   mov [v2],5.2
   
  finit
    fild dword [v1]
    fild dword [v2]
    fdivp
    fistp dword [esp]
; result is now in eax
pop eax 
mov [v3],eax

cinvoke wsprintfA,addr v4,"%u",[v3]
invoke SetDlgItemTextA,[hWin],maximumoutputpowertext,addr v4
    


when i calculate it the result textbox gives me 1 as the answer how can I get it to give me 1.94230769231 as the answer

also is the 1.94230769231 in eax and it is just rounding it when i use wsprintf or is it rounding it before wsprintf

and I also changed the v1 v2 v3 to dd because when I did dq it kept giving me 1 as the answer

thanks
Post 05 Sep 2013, 16:26
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 05 Sep 2013, 16:40
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:58; edited 1 time in total
Post 05 Sep 2013, 16:40
View user's profile Send private message Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 16:58
I am sorry but I am kind of new at fasm and dont understand how to edit the code to get the precision

I tried this but it gave me 1
Code:
mov [v1],10.1
   mov [v2],5.2
   
  finit
    fild dword [v1]
    fild dword [v2]
    fdivp
    fstp  dword [v3]

cinvoke wsprintfA,addr v4,"%u",[v3]
invoke SetDlgItemTextA,[hWin],maximumoutputpowertext,addr v4
    


thanks
Post 05 Sep 2013, 16:58
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 05 Sep 2013, 17:15
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:58; edited 2 times in total
Post 05 Sep 2013, 17:15
View user's profile Send private message Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 17:20
it just give me 0001 instead of 1

I also noticed if i do
Code:
mov [v5],10
   mov [v6],5
    


it will give me 2 but if I do anything with decimals in it it will always give me 1

like
Code:
mov [v5],22.3
   mov [v6],5.1
    


gives me 1 instead of 3 or 4
Post 05 Sep 2013, 17:20
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 05 Sep 2013, 17:26
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:58; edited 1 time in total
Post 05 Sep 2013, 17:26
View user's profile Send private message Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 17:38
i tried this and it gives me a fixed number of like 1386456 every time no matter what numbers I use
Code:
mov [v1],10.1
   mov [v2],5.2
   
  finit
    fild dword [v1]
    fild dword [v2]
    fdivp
    fistp dword [esp]
; result is now in eax
pop eax 
mov [v3],eax 
          
cinvoke sprintf,addr v4,"%0.5f",[v3]
invoke SetDlgItemTextA,[hWin],maximumoutputpowertext,addr v4
    
Post 05 Sep 2013, 17:38
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 17:46
I have changed your example a bit to make it working in console (this way it's easier to test its behavior) and it works to me as expected:
Code:
format  PE console 4.0

entry   start

include 'win32ax.inc'

section '.data' data readable writeable

        v1 dd 0
        v2 dd 0
        v3 dq 0


section '.code' code readable executable

        start:
                mov     [v1], 10.1
                mov     [v2], 5.2

                finit
                fld     dword [v1]
                fld     dword [v2]
                fdivp
                fstp    qword [v3]

                cinvoke printf, <13, 10, "%.16lf", 13, 10>, dword [v3], dword [v3 + 4]
                invoke  ExitProcess, 0


section '.idata' import data readable

        library kernel32, 'kernel32.dll',\
                msvcrt, 'msvcrt.dll'

        import  kernel32,\
                ExitProcess, 'ExitProcess'

        import  msvcrt,\
                printf, 'printf'
    


Image
Post 05 Sep 2013, 17:46
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 17:59
hey thanks I compiled it and it works but now can you help me convert it to gui based to(thanks because I was planning to build this project as a console and gui based)

I tried

Code:
mov     [v1], 10.1
                mov     [v2], 5.2

                finit
                fld     dword [v1]
                fld     dword [v2]
                fdivp
                fstp    qword [v3] 
                
                cinvoke sprintf,addr v4,"%0.5f",dword [v3]

            invoke SetDlgItemTextA,[hWin],maximumoutputpowertext,addr v4
ret
    


and i get illegal instruction on the sprintf line
maybe its the v4 rb I dont know
I changed my data to look like yours now
v1 dd
v2 dd
v3 dq
v4 rb 20
Post 05 Sep 2013, 17:59
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 18:04
Try to write it like that (remember that v3 is a qword):
Code:
cinvoke sprintf, addr v4, "%0.5f", dword [v3], dword [v3 + 4]    
... and of course use a proper header file:

include 'win32ax.inc'

that allows to use inline declarations of strings.
Post 05 Sep 2013, 18:04
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 18:21
thank you so much it works great. If you dont mind can you help me on one more quick problem because it seems like you know what your doing in fasm.

lets say v1 and v2 are both dq
if I have a number in both of them how would I compare them
for example

.if [v3] > [v6]
invoke MessageBoxExA, NULL, "hey", "hey", NULL
.endif

I get cmp invalid error

(I dont care if you show me how to do it in the high level .if or if you use cmp)

also how would you do multiple compares like in c++ I would do

if (v1 > v2 && v1 > v3) {
// do whatever
}

thanks


Last edited by patchariadog on 05 Sep 2013, 18:25; edited 1 time in total
Post 05 Sep 2013, 18:21
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 18:25
OK, here you have a simple GUI version:
Code:
format  PE GUI 4.0

entry   start

include 'win32ax.inc'

section '.data' data readable writeable

        v1 dd 0
        v2 dd 0
        v3 dq 0

        v4 rb 20

section '.code' code readable executable

        start:
                mov     [v1], 10.1
                mov     [v2], 5.2

                finit
                fld     dword [v1]
                fld     dword [v2]
                fdivp
                fstp    qword [v3]

                cinvoke sprintf, v4, "%.16f", dword [v3], dword [v3 + 4]
                
                invoke  MessageBox, 0, v4, "Result", 0
                
                invoke  ExitProcess, 0


section '.idata' import data readable

        library kernel32, 'kernel32.dll',\
                msvcrt, 'msvcrt.dll',\
                user32, 'user32.dll'

        import  kernel32,\
                ExitProcess, 'ExitProcess'

        import  user32,\
                MessageBox, 'MessageBoxA'

        import  msvcrt,\
                sprintf, 'sprintf'
    


Image


Last edited by MHajduk on 05 Sep 2013, 19:52; edited 2 times in total
Post 05 Sep 2013, 18:25
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 18:27
okay thanks. can you please show me how to compare the 2 qwords

thanks
Post 05 Sep 2013, 18:27
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 18:33
patchariadog wrote:
lets say v1 and v2 are both dq
if I have a number in both of them how would I compare them
for example

.if [v3] > [v6]
invoke MessageBoxExA, NULL, "hey", "hey", NULL
.endif

I get cmp invalid error
Well, I've never used such pseudo-HLL features of FASM, so I may be wrong here but I think you can't compare values just like that. I'd use rather registers instead but if you aren't programming in 64 bit you can only compare 32 bit registers this way, IMO.

You can compare values using FCOM instruction of FPU or MMX comparison instructions.
Post 05 Sep 2013, 18:33
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog 05 Sep 2013, 18:38
I looked an it says
fcom ; compare st0 with st1
so if i have a
v1 and v2 dq
how do I load these in to st0 and st1 and then compare
Post 05 Sep 2013, 18:38
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 18:44
Intel manual says the following:

Image

After the comparison you can check EFLAGS bits and decide what to do (e.g. jump).
Post 05 Sep 2013, 18:44
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 05 Sep 2013, 18:51
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:58; edited 1 time in total
Post 05 Sep 2013, 18:51
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 05 Sep 2013, 18:58
HaHaAnonymous wrote:
Untested.
Untested but should work for sure. Smile
Post 05 Sep 2013, 18:58
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.