Inverting bits (Easy)
Time limit1sMemory limit512 MB
Write a program in a tiny 26-register 8-bit assembly language that reads 7 bits and outputs their complements, using the not instruction at most once.
- Level
Easy3 of 10
- Topics
- Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
The wannabe scientist Kleofáš has recently developed a new processor. The processor has 26 registers, labeled A to Z. Each register is an 8-bit unsigned integer variable.
This new processor is incredibly simple. It only supports the instructions specified in the table below. (In all instructions, R is a name of a register, C is a constant, and X is either the name of a register or a constant. All constants are 8-bit unsigned integers, that is, numbers from 0 to 255.)
Notes:
- After any instruction other than not, if the second argument was a register name, its content remains unchanged.
- Whenever
shlorshris called, the shifted value of the first argument is truncated on one end and padded with zeroes on the other end. For example, ifXequals binary 10110110, thenX shr 2equals 00101101.
Example task
Assume that the input contains arbitrary two 8-bit unsigned integers a and b. Write a program that will read these numbers and produce an integer z such that z = 0 if and only if a = b. (That is, if a and b differ, z must be positive.)
Problem specification
For each data set, you have to write a program for Kleofáš's processor that solves the following task: The input for your program is a sequence of integers, each of them a 0 or a 1. The output of your program must be a sequence of their negations, in order. (That is, change each 0 into a 1 and vice versa.)
The sequence contains exactly 7 integers. In your program, you may only use the instruction not at most once. Your program must have at most 100 instructions.
Note that you have to use each of the instructions get and put exactly 7 times.
Input
This problem has no input.
Output
Send us your program as plain ASCII text. Each line of the text may either contain one complete instruction, or just whitespace. If you submit anything that is not a proper program, it will be judged "Wrong answer" and you will get an appropriate error message.
Hint
There are many possible solutions. In our solution, we read the input to registers A and B, compute their bitwise xor in register Z and output it.
However, we do not have an instruction for xor. We have to assemble it from ands, ors, and nots. First, we create (not-A and B) in C and (not-B and A) in D. Finally, we store (C or D) in Z and we are done.
Why does it work? If the two numbers are identical, then C and D will both be zero, and so will Z. If the two numbers are not identical, consider a digit d where the two numbers differ in their binary representations. If this digit is 0 in A, it has to be 1 in B. But then it is 1 in C and therefore it is 1 in Z. And if the digit is 1 in A, it has to be 0 in B, 1 in D, and 1 in Z.