flat assembler
Message board for the users of flat assembler.

Index > Linux > How to print a float in 64-bit program?

Author
Thread Post new topic Reply to topic
gAr



Joined: 18 Apr 2010
Posts: 5
gAr 22 Jan 2014, 15:29
I'm trying a simple program to print a floating point value in 64-bit linux distro.
What I have done is that I wrote a C program and got the disassembly and translated to fasm syntax, using an example in fasm 1.70.03 for linux.

This is the C program I used
Code:
#include <stdio.h>
/* compiled in gcc with options -g -Wall */
int main(){
    float a=1.1;
    printf("%lf\n",a);
    return 0;
}
    


and this is the fasm translated code:
Code:
format ELF64 executable 3
entry start

include 'import64.inc'

interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libc.so.6'
import printf,exit

segment readable executable

start:
push   rbp
mov    rbp,rsp
sub    rsp,0x10
mov    eax,0x3f8ccccd  ; = 1.1
mov    DWORD  [rbp-0x4],eax
movss  xmm0,DWORD  [rbp-0x4]
cvtps2pd xmm0,xmm0
mov    eax,pf
mov    rdi,rax
mov    eax,0x0  ; This was 0x1 in the disassembly, but that caused SIGSEGV
call   [printf]
mov    eax,0x0  
call [exit]

segment readable writeable
pf db '%lf',0xa,0
    


Instead of printing 1.1, it prints 0.00000
Can anyone help with this, please?
Post 22 Jan 2014, 15:29
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 22 Jan 2014, 16:09
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:27; edited 2 times in total
Post 22 Jan 2014, 16:09
View user's profile Send private message Reply with quote
Melissa



Joined: 12 Apr 2012
Posts: 125
Melissa 22 Jan 2014, 16:10
Stack has to be 16 byte aligned for printf (when float arguments
are used)...

Code:
format elf64 executable 3
include 'import64.inc'

interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libc.so.6'
import printf,exit

segment readable executable
entry start

start:
push   rbp
mov    rbp,rsp
sub    rsp,0x8 ; stack has to be 16 byte aligned for printf
mov    eax,0x3f8ccccd  ; = 1.1 
mov    DWORD  [rbp-0x4],eax
movss  xmm0,DWORD  [rbp-0x4]
cvtps2pd xmm0,xmm0
mov    eax,pf
mov    rdi,rax
mov    eax,1  ; eax -> number of float arguments
call   [printf]
xor    edi,edi
call [exit]

segment readable writeable
pf db '%lf',0xa,0

    
Post 22 Jan 2014, 16:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 22 Jan 2014, 16:21
You can also use the 1.1 value directly:
Code:
mov eax,1.1    
Post 22 Jan 2014, 16:21
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 22 Jan 2014, 16:24
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:27; edited 1 time in total
Post 22 Jan 2014, 16:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 22 Jan 2014, 16:31
HaHaAnonymous wrote:
Maybe he is just paranoid like me and want to be extremely sure the value is correct.
How do you know that 0x3f8ccccd is "correct"? Are you just trusting the C compiler to always give you "correct" values? Why trust it more than fasm, or Windows calculator?
Post 22 Jan 2014, 16:31
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 22 Jan 2014, 16:52
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:26; edited 1 time in total
Post 22 Jan 2014, 16:52
View user's profile Send private message Reply with quote
gAr



Joined: 18 Apr 2010
Posts: 5
gAr 22 Jan 2014, 16:52
Thanks everybody for your replies, both of the solutions worked great.

So, alignment was the problem. I was going mad about why the very same code by the compiler wasn't working with fasm!

Quote:

How do you know that 0x3f8ccccd is "correct"? Are you just trusting the C compiler to always give you "correct" values? Why trust it more than fasm, or Windows calculator?

I just translated the code to fasm as I said, wasn't sure whether 1.1 would work. Thanks for letting me know!
Post 22 Jan 2014, 16:52
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.