7_R3X
7_R3X

Reputation: 4370

Binary Representation of Float in Assembly Language

I'm studying Assembly language and its data types and came across .float. I declared a .float variable named Float and assigned it's value as 10.23. I used GNU assembler v2.28 to assemble it and followed by GNU Linker v2.28 to link the object file to create an executable binary. Here's my program:

.data
    HelloWorld:
        .ascii "Hello World!\n"
    ByteLocation:
        .byte 10
    Int32:
        .int 2
    Int16:
        .short 5
    Float:
        .float 10.23
    IntArr:
        .int 3,6,7,8,10

.bss
    .comm LargeBuffer, 1000

.text
    .globl _start
    _start:
        nop    
        movl $1, %eax
        movl $0, %ebx
        int $0x80

I set a break point using a GDB debugger.

(gdb) break *_start+1 Breakpoint 1 at 0x4000b1: file 03 VariableDemo.s, line 22.

And when I looked at the decimal representation and binary representation of Float. I found this:

(gdb) x/1bw 0x0000000000600139 0x600139: 10.2299995 (gdb) x/1tw 0x0000000000600139 0x600139: 01000001001000111010111000010100

I'm referring Can anyone explain representation of float in memory? to convert the binary representation back to 10.23. Here's my calculation:

first bit = 1 => The floating point is positive.

next 8 bits = 10000010 => Representing a +2 as the multiplier of significand.

next 23 bits = 01000111010111000010100 => Which when converted with negative power of 2 gives 0.278749943

But 0.278749943*2 is not 10.23. So now I 2 questions in mind.

  1. Why is 10.23 in text represented as 10.2299995 in memory.
  2. Where did my calculation go wrong?

Upvotes: 0

Views: 1030

Answers (2)

Malcolm McLean
Malcolm McLean

Reputation: 6404

The mantissa has an implicit leading 1, so it is always binary 1.xxxxxxx. It is then left-shifted or right-shifted according to the exponent (the exponent is biased rather than a two's complement binary number, so 127 equals zero shift, or 1.xxxxx, 128 represents 1x.xxxx and so on).

Finally the binary is converted to denary for output.

Upvotes: 0

Jester
Jester

Reputation: 58772

  1. Conversion error, not everything that is "nice" in base 10 is gonna be nice in base 2.
  2. The exponent bias is 127 not 128, so that's a +3 not a +2
  3. You actually need to use the exponent, so that's *2^3 not just *2
  4. You forgot the implicit leading 1 bit, so you need to add 1 to the mantissa, meaning you have 1.278749943*2^3=10.2299995

Upvotes: 4

Related Questions