flat assembler
Message board for the users of flat assembler.

Index > Windows > Wrong printf of float number.

Author
Thread Post new topic Reply to topic
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 15:14
Hi Smile
(another edit)
I have problem with my very simple code.
I want to print a float value which is given by user...

The result is:
Quote:

12.11Begin number: a= 1.2
12.11Press any key to continue . . .

How to properly printf that float? Question



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


section '.text' code readable executable

start:

            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
                   
                 
Post 08 May 2020, 15:14
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1775
Roman 08 May 2020, 15:26
double [a]
Post 08 May 2020, 15:26
View user's profile Send private message Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 15:40
Quote:

double [a]


Where I have to put it?
Sorry, but i'm newbie...
Post 08 May 2020, 15:40
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1775
Roman 08 May 2020, 15:53
cinvoke printf, '%.2f',double [a]
Post 08 May 2020, 15:53
View user's profile Send private message Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 16:35
thanks a lot, it works for me Smile
Post 08 May 2020, 16:35
View user's profile Send private message Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 16:42
anyway, i have second problem... :/

here is the code, integral calculator
for given -1.25 and 2 n =100 the return should be ca = 3.5293

the result is: -1.#IND

Why this is shown?


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

section '.text' code readable executable 

 start:

  finit
  cinvoke printf, 'Begin number: '
  cinvoke printf, 'a= '
  cinvoke scanf, '%lf',a
  cinvoke printf, 'End number: '
  cinvoke printf, 'b= '
  cinvoke scanf, '%lf',b
  cinvoke printf, 'Grids(more higher accuracy): '
  cinvoke printf, 'n= '
  cinvoke scanf, '%d',n



 fld [b];
 fld [a]
 fsubp      ;(b-a)
 fild [n]
 fdivp      ;(b-a)/n
 fst [h]    ;store h = (b - a) / n;

;y(a)=2x^3
 fld [a]
 fld [a]
 fmulp       ;a^2
 fld [a]
 fmulp       ;a^3
 fild [two]
 fmulp       ;2(a^3)
 fild [one]
 fsubp        ;2(a^3)-1
 fst [ya]    ;=ya

; y(b) = 2x^3
 fld [b]
 fld [b]
 fmulp
 fld [b]
 fmulp
 fild [two]
 fmulp
 fild [one]
 fsubp
 fst [yb]

; s= y(a) + y(b)
 fld[ya]
 fld[yb]
 faddp
 fstp [s]

 jumper: ;int i = 1; i < n; i++
  fild [i]
  fld [h]
  fmulp
  fld [a]
  faddp    ;(a+i*h)
  fst [pom]

  fld [pom]
  fld [pom]
  fmulp  ;(res)^2
  fld [pom]
  fmulp  ;(res)^3
  fild [two]
  fmulp    ;2(res)^3
  fild [one]
  fsubp    ;2(res)^3-1
           ;y(a+i*h)

  fild [two]
  fmulp ;2*y(a+i*h)
  fst [teset]
  fld [s]
  faddp   ;s += 2 * y( a + i * h);
  fst [s] ;store s;

  inc [i]
  mov edx,[i]
  cmp edx,[n]
 jb jumper;robimy taka petle do{} while(i<b);

 fld [h]
 fild [two]
 fdivp
 fld [s]
 fmulp
 fst [wynik] ; wynik = (h / 2) * s

cinvoke printf, '%.4f',double [wynik],double [wynik+4]
cinvoke system, 'pause'


section '.data' code readable writeable executable
 a dq 0.0
 b dq 0.0
 ya dq 0.0
 yb dq 0.0
 h dq 0.0
 s dq 0.0
 pom dq 0.0
 wynik dq 0.0

 one dd 1
 two dd 2
 n dd 0
 i dd 1
 teset dq 0.0

    
[/b]
Post 08 May 2020, 16:42
View user's profile Send private message Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 17:28
i have second problem... :/

here is the code, integral calculator
for given -1.25 and 2 n =100 the return should be ca = 3.5293

the result is: -1.#IND

Why this is shown?


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

section '.text' code readable executable 

 start:

  finit
  cinvoke printf, 'Begin number: '
  cinvoke printf, 'a= '
  cinvoke scanf, '%lf',a
  cinvoke printf, 'End number: '
  cinvoke printf, 'b= '
  cinvoke scanf, '%lf',b
  cinvoke printf, 'Grids(more higher accuracy): '
  cinvoke printf, 'n= '
  cinvoke scanf, '%d',n



 fld [b];
 fld [a]
 fsubp      ;(b-a)
 fild [n]
 fdivp      ;(b-a)/n
 fst [h]    ;store h = (b - a) / n;

;y(a)=2x^3
 fld [a]
 fld [a]
 fmulp       ;a^2
 fld [a]
 fmulp       ;a^3
 fild [two]
 fmulp       ;2(a^3)
 fild [one]
 fsubp        ;2(a^3)-1
 fst [ya]    ;=ya

; y(b) = 2x^3
 fld [b]

 fmulp

 fmulp
 fild [one]
 fsubp
 fst [yb]

; s= y(a) + y(b)
 fld[ya]
 fld[yb]
 faddp
 fstp [s]


  fild [i]
  fld [h]
  fmulp
  fld [a]
  faddp    ;(a+i*h)
  fst [pom]

  fmulp  ;(res)^3
  fild [two]
  fmulp    ;2(res)^3
  fild [one]
  fsubp    ;2(res)^3-1
           ;y(a+i*h)

  fild [two]
  fmulp ;2*y(a+i*h)
  fst [teset]
  fld [s]
  faddp   ;s += 2 * y( a + i * h);
  fst [s] ;store s;


 fld [h]
 fild [two]
 fdivp
 fld [s]
 fmulp
 fst [wynik] ; wynik = (h / 2) * s

cinvoke printf, '%.4f',double [wynik],double [wynik+4]
cinvoke system, 'pause'


section '.data' code readable writeable executable
 a dq 0.0
 yb dq 0.0
 h dq 0.0
 s dq 0.0
 pom dq 0.0
 wynik dq 0.0

 one dd 1
 two dd 2
 n dd 0
 i dd 1
 teset dq 0.0

    


Last edited by felement on 08 May 2020, 19:23; edited 2 times in total
Post 08 May 2020, 17:28
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1775
Roman 08 May 2020, 19:01
sometime use IDA pro for debug you code.

cinvoke printf, '%.4f',double [wynik],double [wynik+4]

may be cinvoke printf, '%.4f',double [wynik]
Post 08 May 2020, 19:01
View user's profile Send private message Reply with quote
felement



Joined: 08 May 2020
Posts: 8
felement 08 May 2020, 19:23
fstp [s] (not fst [s] )- the number exceeded the size.
Post 08 May 2020, 19:23
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1775
Roman 09 May 2020, 07:56
fld [b];
fld [a]
fsubp ;(b-a)

Use sse. 8 xmm registers.
movsd xmm1,[b] ;for double float
subsd xmm1,[a]

for float:
movss xmm1,[b]
subss xmm1,[a]

convert int to float:
cvtsi2ss xmm1,eax

convert float to int:
cvtss2si eax,xmm1

convert double(xmm1) to float(xmm2):
cvtsd2ss xmm2,xmm1
Post 09 May 2020, 07:56
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.