Halt and Catch Fire
Time limit1sMemory limit2048 MB
Simulate a small assembly-like program with mov, add, sub, jeq, and hcf, then print $out or the machine state at the hcf instruction.
- Level
Easy3 of 10
- Topics
- Simulation, Hash map
- Solved
- No attempts yet
Problem
Research has led to the creation of a new super fast CPU called the Fast Processing Chip (FPC). This product has a never-before-seen debugging function: an instruction called hcf, which gives more debugging power than any previously designed CPU.
The product is becoming popular quickly, and companies want to get their hands on the chip to start development right away. However, the chip is too expensive to hand to every developer.
You have been hired to design a program that runs as if it were a processor. Programmers can write programs with it and test them easily without needing the hardware.
Input
The first line contains one integer , the number of lines in the program.
The next lines each have three parts, separated by spaces:
- The operation, always 3 characters.
- An argument, which is either an integer or a variable name prefixed with
$. - An argument, which is either an integer or a variable name prefixed with
$.
There are five operations:
mov: Moves the value of the first argument to the second argument.add: Adds the value of the second argument to the first argument and stores the result in$acc.sub: Subtracts the value of the second argument from the first argument and stores the result in$acc.jeq: Jumps to the instruction at the address given by the value of the second argument if the value of the first argument equals$cmp.hcf: Halts and catches fire, stopping the program immediately. It takes only integer arguments.
An argument is either an integer immediate or a variable reference, which starts with $. Variable names consist of at most 20 lowercase English letters (a-z) or underscores (_).
There are four predefined variables:
$acc: Contains the result of the lastaddorsuboperation.$pc: Contains the address of the operation currently being processed.$cmp: Used in comparison by thejeqoperation.$out: Is output when the program finishes.
Any other variable is defined as soon as a value is moved into it.
Assume that the input program is syntactically correct. Integers never overflow or underflow.
Output
The program ends when the value of $pc goes outside the bounds of the program, or when an hcf instruction is reached.
If the program ends because $pc goes out of bounds, print one line containing the value of $out at the end of the program.
If the program ends at an hcf instruction, print the following:
- One line containing the
hcfinstruction that was called. - One line containing the value of
$acc. - One line containing the value of
$cmp. - One line containing the value of
$out.