I'd like to ask about converting int and float to binary. The first one is quite simple, but for second, i have no clue. It should be formatted sign, base and mantissa. Any ideas?
-
1Are you talking about saving a file with float data or printing out a string representation of the binary values for a float?Seth Hays– Seth Hays2013-06-13 21:52:17 +00:00Commented Jun 13, 2013 at 21:52
-
Binary is kinda how things are stored already? Not sure what your question is here. Can you add more info or restate?Michael Dorgan– Michael Dorgan2013-06-13 21:54:40 +00:00Commented Jun 13, 2013 at 21:54
-
Exactly, for int it's obvious, but for float not really.Giuliano– Giuliano2013-06-13 21:54:44 +00:00Commented Jun 13, 2013 at 21:54
-
what did you try? see thisManish Mishra– Manish Mishra2013-06-13 21:54:56 +00:00Commented Jun 13, 2013 at 21:54
-
@ManishMishra yeah something like this, but output should be: sign(space)base(space)mantissa.Giuliano– Giuliano2013-06-13 21:58:43 +00:00Commented Jun 13, 2013 at 21:58
|
Show 3 more comments
1 Answer
Heres an example of printing out the sign, exponent and mantissa of a single precision float in MIPS. You said you already understand how to convert decimal to binary, so I have omitted that step.
For details on the layout of single precision floats see: this.
.text
main:
li.s $f0 0.15625 #load floating point immediate
mfc1 $t8 $f0 #store in $t8
#extract bit 31
ori $t0 $zero 0x1
sll $t0 $t0 31
and $a0 $t0 $t8
srl $a0 $a0 31
#print sign
addi $v0 $zero 1
syscall
#print space
addi $a0 $zero 32
addi $v0 $zero 11
syscall
#extract bits 23-30
ori $t0 $zero 0xFF
sll $t0 $t0 23
and $a0 $t0 $t8
srl $a0 $a0 23
#print exponent
addi $v0 $zero 1
syscall
#print space
addi $a0 $zero 32
addi $v0 $zero 11
syscall
#extract bits 0-22
ori $t0 $zero 0xFFFF
sll $t0 $t0 7
ori $t0 $t0 0x7F
and $a0 $t0 $t8
#print mantissa
addi $v0 $zero 1
syscall
#print new line
addi $a0 $zero 10
addi $v0 $zero 11
syscall
jr $ra
This will output:
0 124 2097152