| #include <iostream> |
| #include <vector> |
| #include <string> |
| #include <algorithm> |
|
|
| using namespace std; |
|
|
| |
| struct Instruction { |
| string type; |
| int a, x, b, y; |
| }; |
|
|
| int main() { |
| ios_base::sync_with_stdio(false); |
| cin.tie(NULL); |
|
|
| unsigned int k; |
| cin >> k; |
|
|
| if (k == 1) { |
| cout << 1 << endl; |
| cout << "HALT PUSH 1 GOTO 1" << endl; |
| return 0; |
| } |
|
|
| unsigned int m = (k - 1) / 2; |
|
|
| vector<Instruction> program; |
| int n = 0; |
|
|
| |
| |
| |
| |
| program.push_back({"POP", 1023, 2, 1020, 2}); |
| n = 1; |
|
|
| |
| for (int i = 30; i >= 0; --i) { |
| bool bit_is_one = (m >> i) & 1; |
| |
| int current_block_start = n + 1; |
| int next_block_start; |
| if (i > 0) { |
| next_block_start = current_block_start + 6; |
| } else { |
| |
| next_block_start = 1 + 31 * 6 + 1; |
| } |
|
|
| |
| |
| |
| |
| int val_in_prev_stage = (i == 30) ? 1020 : (2 * (i + 1) + 3); |
| int val_out_current_stage = (i == 0) ? 3 : (2 * i + 3); |
| int val_temp = 2 * i + 4; |
|
|
| |
| |
| |
| program.push_back({"POP", val_in_prev_stage, current_block_start + 1, 1021, current_block_start + 3}); |
| |
| program.push_back({"POP", 1023, current_block_start + 2, val_temp, current_block_start + 2}); |
| program.push_back({"POP", 1023, current_block_start, val_temp, current_block_start}); |
| |
| |
| int adder_target = current_block_start + 4; |
| if (bit_is_one) { |
| |
| program.push_back({"POP", 1021, adder_target, val_out_current_stage, adder_target}); |
| } else { |
| |
| program.push_back({"POP", 1021, adder_target, 1021, adder_target}); |
| } |
|
|
| |
| |
| program.push_back({"POP", val_temp, current_block_start + 5, 1021, next_block_start}); |
| |
| program.push_back({"POP", 1023, current_block_start + 4, val_out_current_stage, current_block_start + 4}); |
|
|
| n += 6; |
| } |
| |
| |
| int loop_entry = n + 1; |
| int halt_instr = n + 2; |
| |
| program.push_back({"POP", 3, halt_instr, 2, loop_entry}); |
| program.push_back({"HALT", -1, -1, 1, loop_entry}); |
| n += 2; |
|
|
| cout << n << endl; |
| for (const auto& instr : program) { |
| if (instr.type == "POP") { |
| cout << "POP " << instr.a << " GOTO " << instr.x << " PUSH " << instr.b << " GOTO " << instr.y << endl; |
| } else { |
| cout << "HALT PUSH " << instr.b << " GOTO " << instr.y << endl; |
| } |
| } |
|
|
| return 0; |
| } |