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
%fformat specifier forprintfexpects an argument of typedouble, but by passing a single-precision floating point value ins0, you are effectively passingfloatinstead.(In fact, variadic functions such as
printfcan never take an argument of typefloat. If you try to pass afloatargument toprintffrom 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, s20withfcvt d0, s20to convert it to double precision.