0

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?

8
  • 1
    Are you talking about saving a file with float data or printing out a string representation of the binary values for a float? Commented 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? Commented Jun 13, 2013 at 21:54
  • Exactly, for int it's obvious, but for float not really. Commented Jun 13, 2013 at 21:54
  • what did you try? see this Commented Jun 13, 2013 at 21:54
  • @ManishMishra yeah something like this, but output should be: sign(space)base(space)mantissa. Commented Jun 13, 2013 at 21:58

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.