#P4911. Ktx-65 Assembly Program Interpreter

    ID: 18152 Type: Default 1000ms 256MiB

Ktx-65 Assembly Program Interpreter

Ktx-65 Assembly Program Interpreter

You are given a program written in the Ktx-65 assembly language along with a separate input. Your task is to interpret the assembly program according to the specification below and output the result produced by the program.

The assembly program uses a small set of instructions. Below is the description of the supported instructions:

  • wint X;: Writes an integer to the output. The argument X can be a numeric literal prefixed by '#' (for example, #5) or a register reference with a leading '%' (for example, %r1). The special literal #line outputs the original source line number of that instruction.
  • wch X;: Writes a single character (whose ASCII code is X) to the output.
  • rint %reg;: Reads an integer from the runtime input and stores it in the register reg (for example, %r1). All registers are intially 0.
  • add A B C;: There are two forms:
    • If three operands are provided (e.g. add %r2 1 %r2;), then it computes A + B and stores the result into the register given by the third operand.
    • If only two operands are provided (e.g. add %r1 %r2;) then it adds their values and stores the result in a special register val (which is later used by wint %val;).
  • lle X Y;: Compares the value of X and Y (each can be a literal or register value) and sets an internal flag to true if X ≤ Y, false otherwise.
  • jif offset;: If the previously set flag is true, jumps back offset instructions (i.e. decreases the instruction pointer by offset). Otherwise, execution continues with the next instruction.
  • callfunc $Label;: Calls the function whose label is $Label. The current instruction pointer is saved on the call stack.
  • ret;: Returns from the current function call by restoring the saved instruction pointer. If there is no function to return to, the program terminates.
  • hlt;: Halts the program immediately.

Additional notes:

  • Lines that contain a label in the form [main] or a function declaration (for example, function $Function1;) are used as jump targets. Only labels main and those starting with $ (from function declarations) are considered; other comment lines enclosed in [] should be ignored.
  • The program input consists of two parts. The first part contains the assembly source, and the second part (after the assembly source) is a sequence of integers used by rint instructions.
  • For the purpose of this problem, the assembly source is provided as follows: The first input line contains an integer N, the number of lines in the assembly program. The next N lines contain the assembly code. Finally, a line follows which contains the runtime input tokens (separated by spaces).

Example

[main]
wint #line;    [output current physical line number]
wch 13;
wch 10;
callfunc $Function1;
callfunc $Function2;
hlt;

function $Function1; rint %r1; add %r2 1 %r2; lle %r2 %r1; jif 2; wint %r2; wch 13; wch 10; ret;

function $Function2; rint %r1; rint %r2; add %r1 %r2; wint %val; wch 13; wch 10; ret;

</p>

Suppose the runtime input is: 3 5 7. Then the program execution is as follows:

  1. In [main], wint #line; outputs the source line number of that instruction (for example, 2), then wch outputs carriage return and newline.
  2. callfunc $Function1; calls function $Function1 where:
    • rint %r1; reads 3 into register r1,
    • A loop is executed where register r2 is incremented until it becomes greater than r1. In this case, starting from 0 it becomes 1, 2, 3, then 4. The function then outputs 4 followed by a carriage return and newline.
  3. callfunc $Function2; calls function $Function2 where:
    • Two integers 5 and 7 are read into registers r1 and r2,
    • They are added (5 + 7 = 12), and wint %val; outputs 12 followed by a carriage return and newline.
  4. The program halts upon executing hlt;.

Thus, the final output will be:

2
4
12

inputFormat

The first line of input contains an integer N indicating the number of lines of the assembly program. The next N lines contain the assembly source code. The final line contains a sequence of integers (separated by spaces) which serve as the runtime input for the program (each integer is consumed by an rint instruction in order).

outputFormat

Output the result produced by the interpreted assembly program. Note that the output may contain numeric values and certain control characters (for example, carriage return and newline) as specified by the program.

sample

24
[main]
wint #line;    [output the current physical line number]
wch 13;
wch 10;
callfunc $Function1;
callfunc $Function2;
hlt;
function $Function1;
rint %r1;
add %r2 1 %r2;
lle %r2 %r1;
jif 2;
wint %r2;
wch 13;
wch 10;
ret;
function $Function2;
rint %r1;
rint %r2;
add %r1 %r2;
wint %val;
wch 13;
wch 10;
ret;
3 5 7
2

4 12

</p>