flat assembler
Message board for the users of flat assembler.
 flat assembler > Windows > [solved] How to add two floating point numbers
Author
UspenskiyKE

Joined: 28 Sep 2016
Posts: 4

# [solved] How to add two floating point numbers

Hello! I am a newbie in assembler language programming area. Help me please to solve my "great problem"
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
21 Nov 2016, 18:43
system error

Joined: 01 Sep 2013
Posts: 671
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.
21 Nov 2016, 21:58
system error

Joined: 01 Sep 2013
Posts: 671
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.
21 Nov 2016, 22:52
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 15390
Location: Monstropolis
If you include win32ax then you can use the double keyword instead of manually pushing two dwords:
 Code: cinvoke printf, tpo, double C
22 Nov 2016, 03:12
UspenskiyKE

Joined: 28 Sep 2016
Posts: 4
Problem solved! Thanks to all!
22 Nov 2016, 08:35
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------Blog General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum