flat assembler
Message board for the users of flat assembler.

Index > Linux > floating point numbers / output to console

Author
Thread Post new topic Reply to topic
andyz74



Joined: 26 Nov 2007
Posts: 36
Location: Germany
andyz74 15 May 2011, 15:11
I have this code

Code:
; fp-test.asm
; Beispiel zu Kommazahlen
; (soll eine festgelegte Kommazahl ausgeben
  format ELF executable
       
     
   entry start

     segment readable writable executable


macro schreibe satz,laenge                      ; macro for writing
{
       mov     eax,4
       mov     ebx,1
       mov     ecx, $ + 16
 mov     edx, laenge
 int     0x80
        db 0ebh
     db laenge+2
 nop
 nop
 db satz
}



macro bin_ax
{
     pusha
       xor cx, cx
  mov bx, 10
m1: xor dx, dx
    div bx
      push dx
     inc cx
      cmp ax, 0
   jnz m1
m2: pop dx
    add dl, 30h
 mov [_d], dl

    pusha
       mov eax, 4
  mov ebx, 1
  mov ecx, _d
 mov edx, 1
  int 80h
     popa
        
    loop m2
     popa
}

  puffer db "12345123451234512345"

msg1  db      "testest",0Ah,0 


; ################ Hauptprogramm ######################

start:

 fld [x1]                                ; lädt Ganzzahl (hier word) in den stack
   fadd    [x2]
        fistp [y]                               ; und speichert diese in y
  wait
        
    pusha
       mov eax, [y]
        bin_ax
      popa
        
    schreibe 0ah,1                  ; macro for writing

     

        mov eax, 1
  mov ebx, 0
  int 80h




     segment readable writable
a  dd      3.5
b        dd      12.5
x1      dd      2.2
x2       dd      2.7
y        dd      0
_d db      0
    


...and it can calculate and write the numbers, but only with a rounded integer as result.

if i change "fistp [y]" to "fstp [y]" and "y" stores a real number in it, I can't manage to output this.

Has anybody a macro or sth, that can output floating-point-numbers, or is anybody able to explain, how i can output them?

Thanks in advance and sorry for my bad english. Confused
Post 15 May 2011, 15:11
View user's profile Send private message Visit poster's website Reply with quote
andyz74



Joined: 26 Nov 2007
Posts: 36
Location: Germany
andyz74 17 May 2011, 07:50
Hmmm..
maybe in this thread : http://board.flatassembler.net/topic.php?t=4377&start=53
is information for me, especially the part "FTOA" ?
Post 17 May 2011, 07:50
View user's profile Send private message Visit poster's website Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 17 May 2011, 09:54
Your dilemma: either writing your own libc or using an already existing one. It's up to you.
Post 17 May 2011, 09:54
View user's profile Send private message Reply with quote
indinaman



Joined: 28 Nov 2020
Posts: 2
indinaman 28 Nov 2020, 02:14
;Floating Point Number To String
format PE console 4.0
entry main
include '\fasmw16932\fasmw16932\INCLUDE\win32a.inc'

section '.data' data readable writeable
num1 dq 48.6
num2 dq 17.1
result dq ?
fmt db " %.9f",13,10,0
fmt2S db " %s",13,10,0
;szBuff db 32 dup (0)
numS rb 15
section '.code' code readable executable
main:
fld qword [num1]
fld qword [num2]
fmulp
fstp qword [result]
invoke printf, fmt, dword[result] ,dword[result+4]
fld qword [num1]
fld qword [num2]
fdivp
fstp qword [result]
invoke printf, fmt, dword[result] ,dword[result+4]
fld qword [num1]
fld qword [num2]
faddp
fstp qword [result]
invoke printf, fmt, dword[result] ,dword[result+4]
fld qword [num1]
fld qword [num2]
fsubp
fstp qword [result]
invoke printf, fmt, dword[result] ,dword[result+4]

invoke _gcvt,dword[result],dword[result+4],15,numS
invoke printf, fmt2S,numS ;String Number
cinvoke getchar
invoke ExitProcess, 0


section '.idata' import data readable
library kernel32,'kernel32.dll', msvcrt,'msvcrt.dll'
import kernel32, ExitProcess,'ExitProcess'
import msvcrt,\
getchar,'getchar',\
atof,'atof',\
strtod,'strtod',\
rand,'rand',\
_gcvt,'_gcvt',\
printf,'printf'
Post 28 Nov 2020, 02:14
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 03 Dec 2020, 11:56
Thanks, only one remark: You need to use wine to run this program on Linux. I feel this a bit suboptimal. Could you please provide us a clean Linux version?
Post 03 Dec 2020, 11:56
View user's profile Send private message Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 125
Melissa 05 Dec 2020, 04:03
Here is program to print float under Linux.


Description:
Download
Filename: float.asm
Filesize: 14.97 KB
Downloaded: 661 Time(s)

Post 05 Dec 2020, 04:03
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 14 Dec 2020, 20:19
Nice, thank you. Did you compare which implementation is faster, yours or that of libc?
Post 14 Dec 2020, 20:19
View user's profile Send private message Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 125
Melissa 16 Dec 2020, 02:07
Endre wrote:
Nice, thank you. Did you compare which implementation is faster, yours or that of libc?


Haven't benchmarked as I don't print floats that much ; )

BTW, implementation is not mine, I took it from somewhere, I guess you can find it perhaps here on forum.
Post 16 Dec 2020, 02:07
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.