what am I doing wrong?
First: make sure you are using the right calling convention (stack, registers, left to right, right to left, etc.). If your program indeed prints a floating point number, although it is not the one you required, then at least the format string is being passed correctly (or you are having a lot of luck and printf
found the address of the format string at the right place even if you didn't put its address there).
Second: the number you are trying to print... is it a float or a double? rs
is defined to hold a quadword value (64 bits), but floats are 32 bits. So, if the first point has been checked and it's ok, I suggest you to use "%lf"
as format, instead of "%f"
.
BTW: why do you put RAX = 0
? What does it mean regarding the call to printf
?
UPDATE: This may help you. A disassembly of a silly program (f.c
):
#include <stdio.h>
main()
{
float x;
x = 1.6;
printf ("%f\n", x);
}
$ gcc -c -S f.c
$ less f.s
.file "f.c"
.section .rodata
.LC1:
.string "%f\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0x3fcccccd, %eax
movl %eax, -4(%rbp)
movss -4(%rbp), %xmm0
cvtps2pd %xmm0, %xmm0
movl $.LC1, %eax
movq %rax, %rdi
movl $1, %eax
call printf
leave