Building EDSAC Instructions
Time limit1sMemory limit128 MB
Convert a decimal fraction into two's-complement binary and print it as an EDSAC assembly instruction, rounding toward zero and detecting out-of-range values.
- Level
Medium5 of 10
- Topics
- Bit manipulation, String, Implementation, Math
- Solved
- No attempts yet
Problem
EDSAC(Electronic Delay Storage Automatic Calculator) was an early digital computer that could store and run programs in main memory. Its instructions used an accumulator, and arithmetic was based on a 17-bit word type and a 35-bit double-word type. Input and output used a 5-bit teletype code.
An EDSAC program can be written in a simple assembly language. Each instruction consists of one character, a nonnegative decimal address, and either F or D. F means full word, and D means double word. For instance, A 128 F adds the full-word value stored at memory address 128 to the accumulator. This instruction is encoded as the binary string 11100000100000000. The first 5 bits, 11100, are the opcode for add; the next 11 bits, 00010000000, are the operand address 128; and the last bit, 0, selects full word. For double word, the last bit is 1.
EDSAC arithmetic uses two's-complement fixed-point values, not floating point. The arithmetic unit treats the binary point as lying between the leftmost bit and the bit immediately to its right. Therefore a 17-bit word value x can represent -1.0 <= x < 1.0.
The largest representable positive real number is 01111111111111111 = 0.9999847412109375, and the smallest positive real number is 00000000000000001 = 2^(-16) = 0.0000152587890625.
Conveniently, the opcode for add and the teletype code for A are both 11100, and the opcode for subtract and S are both 01100. The 32 characters representable by the teletype code are PQWERTYUIOJ#SZK*?F@D!HNM&LXGABCV, and a 5-bit opcode can also represent 32 values. In the teletype code, P is 00000, Q is 00001, and the values increase in that order until V, which is 11111. This made an EDSAC assembler easy to build.
However, the EDSAC assembler had no special syntax for data constants. A programmer therefore used ordinary instruction bit patterns as data values. The constant 3/4, whose bits are 01100000000000000, is written as S 0 F; a value close to 1/3, 00101010101010101, is written as T 682 D. Here T = 00101 and 682 = 01010101010.
Given a decimal number, write a program that prints the correct EDSAC instruction representing that value.
Input
The first line contains the number of test cases P, where 1 <= P <= 1000.
Each test case contains one decimal number D on its own line. D has the form sd.ddd.... Here s is an optional minus sign, and each d is one decimal digit. There is at least 1 digit and at most 16 digits after the decimal point.
Output
For each test case, output one line containing an EDSAC instruction that represents the input value. The format is one opcode character, one space, a nonnegative decimal operand, one space, and either F or D.
If the input value cannot be represented exactly in 17 bits, use the representable value closer to 0. In other words, round positive values down and negative values up.
If D is not in the range -1.0 <= D < 1.0, output INVALID VALUE instead of an EDSAC instruction.