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?
Asked
Active
Viewed 1,322 times
0

Adil Shaikh
- 44,509
- 17
- 89
- 111

Giuliano
- 21
- 3
-
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 Jun 13 '13 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 Jun 13 '13 at 21:54
-
Exactly, for int it's obvious, but for float not really. – Giuliano Jun 13 '13 at 21:54
-
what did you try? [see this](http://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary) – Manish Mishra Jun 13 '13 at 21:54
-
@ManishMishra yeah something like this, but output should be: sign(space)base(space)mantissa. – Giuliano Jun 13 '13 at 21:58
-
Binary is binary. Whether the bits make up a float or an int doesn't matter. So apart from the fact that you want to insert two spaces in the printed binary number (a trivial change) the code would be identical to what you're using for ints. To move a floating-point register to a general purpose register, use the `MFC1` instruction. – Michael Jun 14 '13 at 05:37
-
@Michael ok Michael, you're right. Same as integer :) Sorry, I'm just a beginner in programming. Now I'm receiving float in binary, but how can I split binary to add space for sign, base and mantissa? – Giuliano Jun 14 '13 at 11:02
-
No need to split anything. Just check the loop counter when you print the bits, and print a space at the appropriate places. – Michael Jun 14 '13 at 11:15
1 Answers
2
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

Konrad Lindenbach
- 4,911
- 1
- 26
- 28