1

After lots of edits, researches i decided to do it properly (i hope).

So, i want to get a float number from command line.

Here is the code:

format PE Console
entry start 
include 'INCLUDE/win32ax.inc'
include 'INCLUDE/win_macros.inc'

section '.text' code readable executable

 start:

  finit
  fld [a]
  fld [b]
  fmulp
  fst [a]
  cinvoke printf, '%.2f',dword [a],dword [a+4]
  cinvoke printf,'%s','Begin number: '
  cinvoke printf,'%s','a= '
  cinvoke scanf, '%f',a
  cinvoke printf, '%.2f',dword [a],dword [a+4]
  cinvoke system, 'pause'




section '.data' code readable writeable executable
 a dq 12.11
 b dq 3.0    

The multiplication on the is for tests, and shows the number properly.

36.33

Then the i want to scanf a number to a and printf it. The result is:

36.33

How to move this number to a?

  • What should the result be? That's an important part of a [mcve]. – Peter Cordes May 07 '20 at 19:17
  • something like 23.5 or -35.5. – Fire Elemental May 08 '20 at 05:01
  • Did you try single-stepping through your code in a debugger to see when a register value became Infinity or NaN? I'm not sure what `-1.#IND00l` means, but I'm guessing it might be `-Infinity`. I think your printf call is correct, even though you're passing a `double` as two dword halves instead of just a qword. Use a debugger to find check on the float value that you stored to a register, and whether you overflow the x87 stack. – Peter Cordes May 08 '20 at 09:57
  • All the x86 calling conventions specify the x87 register stack should be empty when you make a function call, but your final FP instruction is `fst` not `fstp`. If you unbalance the x87 stack inside your loop, that would also explain getting NAN-indefinite. Again, use a debugger to check. – Peter Cordes May 08 '20 at 10:03
  • You have more code than I want to read, and the parts I've looked at are wasting instructions like doing `fld [pom]` *twice* before `fmulp` instead of just doing `fmul st0`. And that comes right after an `fst [pom]` so you could have just done `fld st0` / `fmul st0` to get pom^2 and pom on the x87 stack. (Unless you need to force rounding to less than 80-bit, in which case you should just use SSE2 double instead of messing around with obsolete x87.) – Peter Cordes May 08 '20 at 10:07
  • Your question doesn't show anything you found out during debugging. So it's the worst of both worlds. I also don't see the benefit of `fld [two]` / `fmulp` instead of just `fmul [two]`. Or better, `fadd st0`. Neither one puts the temporary value anywhere except `st0`. So use a debugger. – Peter Cordes May 08 '20 at 11:30
  • If you have more info to add to your question, [edit] it to create a better [mcve]. It should be obvious that what you put in comments is an unreadable mess. – Peter Cordes May 08 '20 at 11:33
  • cleaned it up... should be better – Fire Elemental May 08 '20 at 11:43
  • Your numbers are obviously getting gigantic, and could easily overflow to +Inf when it gets a lot bigger. `double` (`dq`) does have a pretty large exponent range, though. Anyway, if that `314808528796353100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000` isn't what you expected, single-step your code in a debugger and watch the value in `st0` (and the other x87 stack registers) change. – Peter Cordes May 08 '20 at 11:56
  • `%f` is the correct conversion for a qword `double`. Your format string has a literal `l` after the conversion for some reason; I assume the ell is an intended part of your output, like volume in litres? If you meant `%lf` for "long float", that's non-standard but equivalent to `%f` for printing a double. – Peter Cordes May 08 '20 at 11:58
  • I think that something wrong is with my float declaration.. I changed input to int, and the printf of a and b works nice... – Fire Elemental May 08 '20 at 12:40
  • @PeterCordes I found some solutions. But it still don't work like i want. – Fire Elemental May 08 '20 at 15:35
  • Good, you did some debugging and reduced your problem to the one part of your program that had the bug, now it's possible to look at the question and see an answer. `scanf('%f"` needs a `float*` not a `double*`. For `scanf` you *do* want `%lf` if you want to read a double. The promotion rules for passing a float by value to printf which mean that `printf "%f"` prints a double ([How to print a single-precision float with printf](https://stackoverflow.com/q/37082784)) only apply to printing, not to input where a variadic function like scanf can take `float*` or a `double*`. – Peter Cordes May 08 '20 at 15:38
  • Also note that `scanf` reads from stdin, not from command line args. – Peter Cordes May 08 '20 at 15:38
  • That's it! Thank you :) – Fire Elemental May 08 '20 at 15:44
  • Duplicate of [Why is scanf returning 0.000000 when it is supplied with a double?](https://stackoverflow.com/q/3696627). Unfortunately I wasted my close vote earlier when I misread the original question. – Peter Cordes May 08 '20 at 16:17

1 Answers1

0

With help of @Peter Cordes founded solution:

 start:

  finit
  cinvoke printf,'%s','Begin number: '
  cinvoke printf,'%s','a= '
  cinvoke scanf, '%lf', a ;here is the important thing
  fld [a]
  fld [b]
  fmulp
  fst [a]
  cinvoke printf, '%.2f',double [a],double [a+4]
  cinvoke system, 'pause'




section '.data' code readable writeable executable
 a dq 0
 b dq 3.0