Can someone suggest me a method to convert a floating point number into binary using assembly?

for example 5.5 would be 000..101.1

but how would you go on about this in assembly?

Dani AI

Generated

Short answer: treat the float as its 32/64-bit IEEE-754 bit pattern, extract sign/exp/mantissa with masks and shifts, then render bits to a buffer with a small loop. That avoids any 8-register "limit" (use memory and a loop). As noted, the number is already in binary; this fills in the assembly steps.

Steps (single-precision):

  • Load the 32-bit word that holds the float into a general register (for x86 this can be mov eax,[value]).
  • sign = (word >> 31) & 1
  • exp_raw = (word >> 23) & 0xFF
  • frac = word & 0x7FFFFF
  • if exp_raw == 0 then number is subnormal (no implicit 1). Otherwise set mantissa M = frac | 0x800000.
  • E = exp_raw - 127 (bias = 127). If exp_raw == 255 then handle Inf/NaN specially.

Rendering binary digits:

  • Value = M * 2^(E-23). If E >= 23 the value is an integer: integer = M << (E-23). If 0 <= E < 23 then integer = M >> (23-E) and fractional bits come from the low (23-E) bits of M. If E < 0 the integer part is 0 and the fractional part is M / 2^(23-E).
  • Produce the integer string by shifting and testing the high bits, then produce fractional bits by the classic integer method: set numerator = leftover; for each output bit do numerator <<= 1; if numerator >= denom then output '1' and numerator -= denom else output '0'. Use a loop to control precision.

Example pseudocode fragment (x86-style) for extracting masks and starting loops:

mov eax, dword [float_value]
shr eax, 31        ; sign
mov ebx, dword [float_value]
shr ebx, 23
and ebx, 0xFF     ; exp_raw
mov ecx, dword [float_value]
and ecx, 0x7FFFFF ; frac
; handle norm/denorm, compute E and M, then build digits

Cautions: watch for endianness on non-x86 targets, handle subnormals/Inf/NaN and negative-zero, and decide a fixed fractional precision (or stop when remainder becomes zero). For layout and bias details see the IEEE 754 summary (IEEE 754).

Recommended Answers

All 3 Replies


Enter 5.5, look at the bit pattern.

Look familiar?

ye ye.. but how would you go on about doing that in assembly?

i mean.. u can convert a floating point to a binary by continuously diving the remainder by 2, and 'saving' the quotient of the division whether its 1 or 0, and form the binary.

The problem that arises with this solution is that only 8 registers are provided, so that would lead to only allowing you to display a binary of 8 bits long (only).

It's already stored in binary, so you just take the mantissa and output a string of 0 and 1. Assuming you've done this exercise for int, then then this step at least is no different.

You then use the exponent to work out where the radix point . goes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.