When I try to cast an integer into a float and print it out, the value stored in that register turns to 0.000000.
outI: .string "%d\n"
outF: .string "%f\n"
mov x20, 160
mov x1, x20
ldr x0, =outI
bl printf
scvtf s20, x20 //cast x20 to float
fmov s0, s20
ldr x0, =outF
bl printf
When I try to run above code, The value printed out is:
160
0.000000
Did I give wrong print instruction?
The
%f
format specifier forprintf
expects an argument of typedouble
, but by passing a single-precision floating point value ins0
, you are effectively passingfloat
instead.(In fact, variadic functions such as
printf
can never take an argument of typefloat
. If you try to pass afloat
argument toprintf
from C code in C, it will be implicitly promoted todouble
, but of course assembly won't do that for you.)So you need instead to get a double-precision floating point value in
d0
. If you've already got a single-precision float ins20
, then the simplest approach is to replace yourfmov s0, s20
withfcvt d0, s20
to convert it to double precision.