2016-03-14 209 views
0

我試着用幾種不同的方式來看它,其中一個是使用二進制向左/右移動,但我無法找到一個組合來使它適用於任何其他任何每次選擇一些數字。那麼如何將浮點數轉換爲整數?MIPS將浮點數轉換爲整數

.data 
five: .float 10.0 

.text 

main: 
    la $a1 five 
    l.s $f12 ($a1) 
    #conversion here 

    li $v0 2# print float, which will print 10.0 (should print integer) 
    syscall 

    li $v0 10 
    syscall 
+1

您是否有MIPS指令集參考?你可能想看看'cvt *。*'。 – EOF

回答

2

免責聲明:我不是MIPS拱的專家。

正如評論中所建議的那樣,快速查找MIPS FP參考給出了所需的指令:cvt.w.s

.data 
    five: .float 5.0 
.text 

#Convert five into an integer 
la $t1, five 
l.s $f12, ($t1)    #f12 = five 
cvt.w.s $f0, $f12   #f0 = (int) five 


#Print five 
li $v0, 2 
syscall 

#Print (int)five 
li $v0, 1 
mfc1 $a0, $f0    #a0 = (int)five 
syscall 

#Exit 
li $v0, 10 
syscall 

這將打印

5.05

由於存在5.0和之間沒有空格。

如果您想手動完成,您可以從answer開始。

+0

謝謝!這正是我需要看到的。我必須仔細看看cvt。 – DanteVFenris

+0

浮點寄存器的內容可以通過'mfc1'(例如'mfc1 $ a0,$ f0')移動到GPR中,從而不需要額外的加載和存儲。 – Michael

+0

@Michael超級duper謝謝!那些裝載/商店是醜陋的。 –