DanteVFenris
DanteVFenris

Reputation: 3

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

Upvotes: 0

Views: 11504

Answers (1)

Margaret Bloom
Margaret Bloom

Reputation: 44126

Disclaimer: I'm not an expert on MIPS arch.

As suggested in the comments, a quick lookup for MIPS FP reference gives the desired instruction: 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

This will print

5.05

As there is no space between the 5.0 and the 5.

If you want to do it by hand, you can start from this answer.

Upvotes: 3

Related Questions