Halt and Catch Fire

시간 제한1초메모리 제한2048 MB

요약
mov, add, sub, jeq, hcf로 이루어진 작은 어셈블리 프로그램을 해석해 실행하고, 끝난 뒤 $out 또는 정지 시점의 네 값을 출력한다.
난이도

보통10점 중 4점

유형
시뮬레이션, 구현, 해시맵
정답자
아직 제출이 없습니다

문제

Research has led to the creating 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, providing debugging power unmatched by any previously designed CPU.

With the product becoming popular fast, companies can't wait to get their hands on the chip to start development immediately. One problem, however, is the chip is too expensive to just hand over to every developer all willy-nilly.

You have been hired to design a program that runs as if it were a processor, so the programmers can write programs and easily test these programs without needing the hardware.

입력

  • One line with one integer: 1≤n≤10001 \leq n \leq 1000, the number of lines in the program.

  • nn lines with three sections, separated with spaces:

    • The operation, always 3 characters
    • An argument: Either an integer −231≤a≤231−1-2^{31} \leq a \leq 2^{31}-1 or a variable prepended with $
    • An argument: Either an integer −231≤b≤231−1-2^{31} \leq b \leq 2^{31}-1 or a variable prepended with $

There are five possible operations:

  • mov -- Move value from the first argument to the second
  • add -- Add value from the second argument to the first and store in $acc
  • sub -- Subtract value of the second argument from the first and store in $acc
  • jeq -- Jump to the instruction on the value of the second argument if the value of the first argument equals $cmp
  • hcf -- Halt and Catch Fire: Stop the program immediately, takes only integer arguments

An argument can be noted as an integer immediate or a variable reference, in which case it's prepended by $. All variable names consist of at most 20 lowercase characters from the English alphabet (a-z) or underscores (_).

There are four predefined variables:

  • $acc -- Contains the value of an add or sub operation
  • $pc -- Contains the address of the currently processed operation
  • $cmp -- Used to compare with the jeq operation
  • $out -- Should be output when the program finishes

Any other variables are defined as soon as a value is moved to them.

Assume any given input program to be syntactically correct.

Integers will never over- or underflow.

출력

The program exits either when the value of the $pc variable reaches out of the bounds of the program, or when an hcf instruction is reached.

  • In the first case, output one line containing the value of $out at the end of the program.

  • In the second case, output the following:

    • One line containing the hcf instruction that was called.
    • One line containing the content of the $acc variable.
    • One line containing the content of the $cmp variable.
    • One line containing the content of the $out variable.

예제2

  1. 예제 1

    입력
    6
    mov 3 $cmp
    jeq 3 3
    jeq $cmp 5
    mov 42 $out
    jeq $cmp 7
    mov 50 $out
    
    예상 출력
    42
    
  2. 예제 2

    입력
    4
    mov 3 $cmp
    mov 2 $out
    add $cmp $out
    hcf 0 0
    
    예상 출력
    hcf 0 0
    5
    3
    2