flat assembler
Message board for the users of flat assembler.

Index > Windows > [solved] How to add two floating point numbers

Author
Thread Post new topic Reply to topic
UspenskiyKE



Joined: 28 Sep 2016
Posts: 4
UspenskiyKE 21 Nov 2016, 18:43
Hello! I am a newbie in assembler language programming area. Help me please to solve my "great problem" Smile
I attempted to write a program which is calculating the sum of the two floating point numbers. It was compiled successfully, but the result of the run was unexpected for me. For example, if the one number is 1.3 and another is 1.8, the sum should be equal to 3.1 But my program returns extremely big number as the sum. This is the text of the program:

Code:
format PE console 4.0
 
include 'include\win32a.inc'
start:  cinvoke printf, req, 41h   ; Âûâîä íà ýêðàí
    cinvoke scanf, tpt, A       ; ââîä A
    cinvoke printf,  req, 42h   ; Âûâîä íà ýêðàí 
    cinvoke scanf, tpt, B       ; ââîä B
    fld  [A]
    fld   [B]
    fadd st0, st1
    fst dword [C]

    cinvoke printf, tpo, C
    invoke  sleep, 50000     ; 50 sec. delay
;
gtfo:   invoke  exit, 0
req db  'Enter %c:',0
tpo db  'A + B = '
tpt db  '%f',0
A   dd  ?
B   dd  ?
C   dd  ?
; import data in the same section
 data import
 
 library msvcrt,'MSVCRT.DLL',\
    kernel32,'KERNEL32.DLL'
 
 import kernel32,\
    sleep,'Sleep'
 
 import msvcrt,\
    puts,'puts',\
    scanf,'scanf',\
        printf,'printf',\ 
    exit,'exit'
end data    


What was my mistake? My CPUs are AMD E2 and Intel Core i3.

Great thanks!

Edit by revolution: Added code tags
Post 21 Nov 2016, 18:43
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 21 Nov 2016, 21:58
I think the printf prints a double. Something like

Code:
push dword[s+4]
push dword[s]
push f
call [printf]
add esp,12

call [getchar]
invoke exit,0

f db '%f',0ah,0
s dq 3.43    


Using 32-bit floats may complicate things further (promotion, multiple pushes etc). Try using all doubles for variables and see how it goes. Btw, printf's arguments like variables are passed by values, not by reference. If you are new, my suggestion is to test short codes first especially when dealing with Win API and high-level constructs. They are known to hide things and children.
Post 21 Nov 2016, 21:58
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 21 Nov 2016, 22:52
Ok, here's a wild guess of what you are trying to achieve here

Code:
cinvoke scanf,x,A
cinvoke scanf,x,B
cinvoke getchar   ;for that classic "I can't see the output" cry.

fld qword[A]   ;the thing
fld qword[B]
fadd st0,st1
fstp qword[C]

;Split the double into two unit. printf dont like floats.
cinvoke printf,f,dword[C],dword[C+4]  

cinvoke getchar  ;don't fall asleep!
invoke exit,0

x db '%lf',0   ;double needs a %lf specifier for scanf
f db '%f',0ah,0
A dq ?
B dq ?
C dq ?    


I'm not interested in solving it for you but judging by the blasphemy of Win API that you are messing up with, I decided to throw in some clues.
Post 21 Nov 2016, 22:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 19869
Location: In your JS exploiting you and your system
revolution 22 Nov 2016, 03:12
If you include win32ax then you can use the double keyword instead of manually pushing two dwords:
Code:
cinvoke printf, tpo, double C    
Post 22 Nov 2016, 03:12
View user's profile Send private message Visit poster's website Reply with quote
UspenskiyKE



Joined: 28 Sep 2016
Posts: 4
UspenskiyKE 22 Nov 2016, 08:35
Problem solved! Thanks to all!
Post 22 Nov 2016, 08:35
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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.