簡體   English   中英

ARMv8 程序集,如何將 integer 值轉換為浮點值,以及如何打印?

[英]ARMv8 Assembly, How to cast integer value to float value, and how to print it?

當我嘗試將 integer 轉換為浮點數並打印出來時,存儲在該寄存器中的值變為 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

當我嘗試運行上面的代碼時,打印出來的值是:

160
0.000000

我是否給出了錯誤的打印指令?

printf%f格式說明符需要一個double類型的參數,但是通過在s0中傳遞一個單精度浮點值,您實際上是在傳遞float

(In fact, variadic functions such as printf can never take an argument of type float . If you try to pass a float argument to printf from C code in C, it will be implicitly promoted to double , but of course assembly won't do給你的。)

因此,您需要在d0中獲取雙精度浮點值。 如果您已經在s20中獲得了單精度浮點數,那么最簡單的方法是將fmov s0, s20替換為fcvt d0, s20以將其轉換為雙精度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM