Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Byang's Assembly

Limits: 2s, 512 MB

Byang doesn’t need your help with his projects… Except with this project that his teacher wants him to work on. He needs to implement an assembler that works with the original 8086 instruction set.

The original 8086 instruction set has a little over 160 instructions. Chances are, the very computer you are reading this problem statement on, supports the 8086 instruction set.

Byang, however, doesn’t want you to implement the project for him. Instead, he wants you to implement an assembly emulator that runs on simpler rules so that he can see how to get started.

Byang’s assembly language will support the following instructions: LOD, INC, ADD, MUL, JMP, CMP, JCZ, JCP, JCN, and HLT. And, any program written in Byang’s assembly language will have access to 7 registers: A, B, C, D, E, F, and P. The first six of which are general purpose registers and P is the program counter (indicating the “line number” of the program that it is currently executing).

You can think of a register as a storage, capable of holding a single signed integer.

The values stored in all of the registers are initially 0. The program counter register P indicates the operation that is about to be executed. At the end of executing every instruction, it is incremented by 1, with the exception of the jump instructions (JMP, JCZ, JCP and JCN) and HLT.

InstructionDescription
LOD X VLOD loads the signed integer V to register X.
INC XINC increments the value of register X by 1.
ADD X YADD calculates the sum of register X and register Y and assigns it back to register X.
MUL X YMUL calculates the product of register X and register Y and assigns it back to register X.
JMP VJMP instruction jumps to a new position V (0-based) in the program and resumes running from there. This instruction is equivalent to LOD P (V-1).
CMP X YCMP compares the values of X and Y, and stores a 0 in register C if they are equal, a 1 if X is greater than Y, and a -1 if X is less than Y.
JCZ VJCZ is similar to JMP V, except that the instruction will perform the jump only if the value of register C is zero. Otherwise the instruction has no effect.
JCP VJCP is similar to JMP V, except that the instruction will perform the jump only if the value of register C is positive. Otherwise the instruction has no effect.
JCN VJCN is similar to JMP V, except that the instruction will perform the jump only if the value of register C is negative. Otherwise the instruction has no effect.
HLTHLT ends the program immediately.

For all of the instructions above, X and Y are placeholders for any of the 7 registers. X and Y can be the same registers. All instructions are atomic and within each instruction all reads happen before any writes.

See this program written in Byang’s assembly for example:

LOD E 5
LOD A 25
ADD B E
INC D
CMP A B
JCP 2
HLT

This program determines the quotient of dividing 25 by 5. It starts with the following steps:

  • Load 5 to register E
  • Load 25 to register A

And, then it repeats the following steps:

  • Add the value of register E to register B
  • Increment the value of D by 1
  • Compare the values of registers A and B
  • If A is greater than B, go back to position 2 in the program repeating these steps

Once A is no longer greater than B, HLT causes the program to stop.

Now you will have to write a program that takes one of these Byang’s code (program written in Byang’s assembly language) as input. It will then emulate running of the code until the code ends with HLT and print the values of all 7 registers at the end of running the code.

Input

The input will contain a series of lines with assembly instructions. You will have to read the file till the end (read until EOF). Each line will contain a single valid instruction.

All of Byang’s code are guaranteed to be shorter than 1000 characters, the emulator will have to execute at most 1000 instructions in total to reach a HLT, and in none of the operations/steps will any of the registers be assigned values larger than 32-bit integers.

Output

Print the values of the 7 registers as follows:

A B C D E F P

(Replacing each alphabet above by the integer value of the corresponding registers.)

Samples

InputOutput
LOD E 5
LOD A 25
ADD B E
INC D
CMP A B
JCP 2
HLT
25 25 0 5 5 0 6

Author
  • hjr265's Avatar

    hjr265

    Mahmud loves to solve problems and loves even more to create them. When he is not sleeping, he is either making software or playing video games.
Discussion