5.67 · exercise

Build a float from parts

Run IEEE 754 backwards: standard input gives a sign (0 or 1), an exponent without its bias, and a fraction in hex. Put them together into a single-precision float, then print its bits and its value. With the stdin box as given (0 2 580000) the program prints:

0x40d80000 = 6.750000

The fraction goes in bits 22 to 0, the exponent plus the bias of 127 in bits 30 to 23, and the sign in bit 31. bfi wD, wN, lsb, width (bitfield insert) copies the low width bits of wN into wD starting at bit lsb and leaves every other bit of wD alone, which is exactly how a field drops into place.

When the word is built, fmov moves it into s0 unchanged. printf cannot print a float as it is: %f reads a double from d0, so widen it first with fcvt d0, s0.

This exercise builds normal numbers only, so each part has a range:

  • the sign is 0 or 1
  • the exponent is -126 to 127 (the stored values 0 and 255 are kept for zero, the tiny subnormal numbers, infinity and NaN, not a number)
  • the fraction is 0 to 0x7fffff (23 bits)

Anything outside those ranges prints cannot build that and returns 1.

What is checked

  • the line for the parts in the stdin box, and for more parts you cannot see
  • the exit status (0, or 1 when a part is out of range)
  • the fields go in with bfi and the word moves with fmov

specification

stdin0 2 580000
stdoutprints the right output
exitexits with the right code
sourceuses bfi
sourceuses fmov
hiddenright output and exit code on 10 more inputs you do not see

We run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.

loading editor...

regfile

N clearZ clearC clearV clear

x0–x30 are the integer registers.

X0arg00x0000000000000000
X1arg10x0000000000000000
X2arg20x0000000000000000
X3arg30x0000000000000000
X4arg40x0000000000000000
X5arg50x0000000000000000
X6arg60x0000000000000000
X7arg70x0000000000000000
X8ind0x0000000000000000
X90x0000000000000000
X100x0000000000000000
X110x0000000000000000
X120x0000000000000000
X130x0000000000000000
X140x0000000000000000
X150x0000000000000000
X16ip00x0000000000000000
X17ip10x0000000000000000
X18pr0x0000000000000000
X190x0000000000000000
X200x0000000000000000
X210x0000000000000000
X220x0000000000000000
X230x0000000000000000
X240x0000000000000000
X250x0000000000000000
X260x0000000000000000
X270x0000000000000000
X280x0000000000000000
X29fp0x0000000000000000
X30lr0x0000000000000000
SP0x0000000080000000
PC0x0000000000400000
console

Output prints here as your program runs.

Press step or run under the editor, or feed stdin from the box below.

not assembled

Open in playground