2

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

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

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)

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

this is the code

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

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

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

I know this code is not right because I am not converting the text to float at the begining but i do not know how to

I also tried a simpler version and it did not work either

mov [v1],5.3
mov [v2],7.1

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

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

so my question is can someone please show me how to read a number from 2 different textboxes and then divide them and the return the result to another textbox using fasm code

thank you

Darrin Woolit
  • 69
  • 1
  • 7
  • I'm not familiar with either Windows or Fasm, but switching to dword for variable 3 doesn't look right, nor does `%u` in your conversion. Start with that and see if it helps... – Frank Kotler Sep 04 '13 at 18:59
  • I dont think it is that because I have used those 2 lines of code to convert raw numbers to readable text in other projects – Darrin Woolit Sep 04 '13 at 20:01
  • Suit yourself. How does it "not work"? I'm pretty sure Fasm wants `rq 1` for an uninitialized double precision float (one of the differences from Nasm syntax), and `%f` for scanf, printf and friends. Printf definitely always wants double precision, although we say `%f`. I'm less sure of scanf. – Frank Kotler Sep 04 '13 at 20:24
  • can you please show me how to fix it. I am kind of new at assembly programming should it be like this cinvoke wsprintfA,addr v4,"%f",addr v3 – Darrin Woolit Sep 04 '13 at 20:58
  • That looks right, but I can't confirm. Does it work? What happens? – Frank Kotler Sep 04 '13 at 21:34
  • I ran this code mov [v1],5 mov [v2],9 finit fld [v1] fld [v2] fmulp fstp [v3] cinvoke wsprintfA,addr v4,"%f",addr v3 invoke SetDlgItemTextA,[hWin],maximumoutputpowertext,addr v4 and it gave me f also using the qword before fpu instructions also gave me f – Darrin Woolit Sep 04 '13 at 21:39
  • I also added this code .if [v3] =0 invoke MessageBoxExA, NULL, "hey", "hey", NULL .endif and it gave me the messagebox. to me this tells me that the 5 lines of fpu code are not working – Darrin Woolit Sep 04 '13 at 22:01
  • I just looked up wsprintf again at http://msdn.microsoft.com/en-us/library/windows/desktop/ms647550%28v=vs.85%29.aspx their is no f so that may explain the f output, but that still does not explain the v3 = 0 – Darrin Woolit Sep 04 '13 at 22:04
  • In Nasm syntax, `mov [v1]. 5.0` - the decimal point indicates floating point. Suspect Fasm is the same(?). Where's John Found when we need him? :) – Frank Kotler Sep 05 '13 at 00:17
  • @FrankKotler - I am here. :) IMO, the question is not very good asked. What actually is the question? `mov [v1], 5.0` will not work, because you can't store immediate constants bigger than 32bit. `mov dword [v1], 5.0` works OK. In 64bit context `mov rax, 5.0 | mov [v1], rax` works as well. – johnfound Sep 05 '13 at 07:18

1 Answers1

0

There are several problems in the demonstrated code.

At first, it is not clear what StrToFloat procedure is? Is it imported from some DLL or it is part of the code, or some other library?

If this procedure is imported, it has to be imported in the FASM program as well. Else it can be written from scratch or ported in source form from the MASM program.

The immediate show stopper here is mov [v1], FLOAT_CONSTANT instruction. The reason is that v1 is qword variable, but mov can moves only dword immediate values (even in 64bit environment).

mov dword [v1], 5.0 works fine, but of course it is not what the OP needs. Floating qword constants can be defined immediately in compile time as well: v1 dq 3.2

If we really want to set some qword floating constant in run time, we have to make it in two instructions following way:

    a = 5.3

    mov  dword [var], (a and $ffffffff)
    mov  dword [var+4], (a shr 32)

    var dq ?

The original FPU code in FASM syntax will be:

    finit
    fld  [variable1]
    fdiv [variable2]
    fstp [variable3]
johnfound
  • 6,857
  • 4
  • 31
  • 60
  • the question is how can I read one number from one text box, another number from another textbox,multiply them using FPU and then show the results in a third text box – Darrin Woolit Sep 05 '13 at 14:56
  • strtofloat is a masm special libary thing. basically i need help with 2 parts. the first part is reading the text from the textbox and converting it to a float to send to the FPU. the second part is using the FPU to multiply. I already have the third part of the code which is to convert the float to text. thanks – Darrin Woolit Sep 05 '13 at 15:01
  • basically I just need to learn how to convert those 5 lines of fpu instructions from masm to fasm – Darrin Woolit Sep 05 '13 at 15:17
  • @DarrinWoolit - about the FPU code - it is simple - the answer is edited. – johnfound Sep 05 '13 at 15:30
  • but that is what I already tired an it did not work. I tried finit fld [v1] fld [v2] fdivp fstp [v3] if I change it to fdiv it says invalid operand – Darrin Woolit Sep 05 '13 at 16:10
  • @DarrinWoolit Sorry, it is probably masm syntax that is weird. The answer is again edited and IMHO correct now. :) – johnfound Sep 05 '13 at 17:01
  • I got it to display me intergers but not floats mov [v5],10.1 mov [v6],5.2 finit fild dword [v5] fild dword [v6] 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 this gives me 1 how do i get 1.94230769231 – Darrin Woolit Sep 05 '13 at 17:02