The Target Language

The target program produced by the compiler is written in "machine code", and has the following syntax:
Program = { Directive }.
Directive =
     Instruction | "BLOCK" "," Value ";".
Instruction =
     InstructionCode "," Value ";" |
InstructionCode =
     ADD  | SUB  |  MUL  | DIV  | LOAD  | STORE |
     ADDC | SUBC |  MULC | DIVC | LOADC |
     JUMPEQ | JUMPNE | JUMPLT | JUMPGT | JUMPLE | JUMPGE
     JUMP | READ | WRITE | HALT.
Value = Integer.

A program is a directive sequence, each directive being either an "instruction", i.e. machine command, or a memory allocation directive.

We assume the main store of the machine to consist of cells, each cell associated with its address, a unique non-negative integer (thus, the cells are numbered from 1). A cell may hold either an instruction or an integer.

The execution of the program always starts from the first sell.

In addition to the main store, the machine has an accumulator, which is capable of containing an integer.

A directive
    BLOCK,Int;
specifies that at this place in the program there must be allocated Int store cells containing no instructions. This directive usually is put at the end of the program, and used for allocating cells that are to hold the values of the program's variables.
A machine instruction has the form
    Op,Value;
where Op is the instruction's name, and Value the instruction's operand. The meaning of the operand Value depends on the instruction's name. Some instructions assume Value to be the address of the cell. Others assume Value to be an integer. There are instructions, however, which needn't any operand, in which cases Value must be equal to zero.

An instruction LOAD,Addr; loads the contents of the cell having the address Addr into the accumulator.

An instruction STORE,Addr; puts the contents of the accumulator into the cell having the address Addr.

An instruction LOADC,Int; loads the integer Int into the accumulator.

Instructions ADD, SUB, MUL and DIV have the form Op,Addr; and compute respectively the sum, difference, product, and the the truncated quotient of two integers. The first integer is the one contained by the accumulator, and the second the one contained in the cell having the address Addr. The result of the operation is put into the accumulator.

Instructions ADDC, SUBC, MULC, and DIVC have the form Op,Int; and compute respectively the sum, difference, product, and the truncated quotient of two integers. The first integer is the one contained in the accumulator, and the second integer is Int, i.e.the one contained in the operand of the instruction. The result of the operation is put into the accumulator.

An instruction READ,Addr; reads an integer from the input device and puts it into the cell having the address Addr.

An instruction WRITE,0; writes the integer contained by the accumulator to the output device.

An instruction HALT,0; halts the execution of the program.

An instruction JUMP,Addr; causes the control to jump to the instruction contained in the cell having the address Addr.

And, finally, the last group of instructions comprises the conditional jumps JUMPEQ, JUMPNE, JUMPLT, JUMPGT, JUMPLE, and JUMPGE, all having the form Op,Addr;. They are executed in the following way. First, the contents of the accumulator is compared with zero. If the condition implied by the instruction's name is satisfied, the control jumps to the instructions contained in the cell having the address Addr, otherwise, to the next instruction.

Which condition is tested, is determined by the last two letters in the instruction's name. EQ means testing the accumulator's contents for being equal to 0, NE for not being equal to 0, LT for being less than 0, GT for being greater than 0, LE for being less than or equal to 0, GE for being greater than or equal to 0.

The above program computing the factorial will be translated by the compiler into the following target program in machine code.

     001  READ,21;       008  JUMPGE,16;     015  JUMP,6;
     002  LOADC,1;       009  LOAD,19;       016  LOAD,20;
     003  STORE,19;      010  ADDC,1;        017  WRITE,0;
     004  LOADC,1;       011  STORE,19;      018  HALT,0;
     005  STORE,20;      012  LOAD,20;       019  BLOCK,3;
     006  LOAD,19;       013  MUL,19;
     007  SUB,21;        014  STORE,20;

The address of each directive is shown on the left of the directive.