简体   繁体   English

MIPS将浮点数转换为整数

[英]MIPS convert float into an integer

I've tried looking at it a couple different ways, one of them was using binary to shift left/right but I just wasn't able to find a combination to make it work for anything besides a few selected numbers each time. 我尝试用几种不同的方式查看它,其中一种是使用二进制左右移动,但是我无法找到一种组合来使它每次只能用于几个选定的数字。 So how do you convert a float into an integer? 那么如何将浮点数转换为整数?

.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

Disclaimer : I'm not an expert on MIPS arch. 免责声明 :我不是MIPS架构专家。

As suggested in the comments, a quick lookup for MIPS FP reference gives the desired instruction: cvt.ws . 如注释中所建议,对MIPS FP参考的快速查找给出了所需的指令: cvt.ws

.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

This will print 这将打印

5.05 5.05

As there is no space between the 5.0 and the 5 . 因为5.05之间没有空格。

If you want to do it by hand, you can start from this answer . 如果您想手动操作,可以从此答案开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM