ASSIGH OPERATOR

Back to:
index

This operator can assign a new value to the variable. By sign := it is naturally divided on two parts. Left part contains the destination variable name, and the right one - the expression, that must be calculated and put into variable. Being simple educational compiler, "COMPAS" doesn't allow to use brackets ( ). So right part can contain not more than one operation.

"COMPAS" always control the fact, that types of left and right parts must be identical.

In most common case assign operator consists from 3 steps:
get first operand (after :=)
calculation, if it's necessary: do operation or calculate function
save the result of previous actions.

Condition analysis in "COMPAS" is realized in analogous way.

Examples and operator structure

1 step: get first operand

2 step: do operation

3 step: save result

"E97" code bricks

Intel code bricks


I called bricks the short pieces of code, that "COMPAS" generate as a part of operator. Compiler builds result machine code from the bricks.

Examples and operator structure

Examples of typical assign operators, available in "COMPAS", are exemplified below.

    for INTEGER:
  • a := 1;
  • a := 1234;
  • a := b;
  • a := b + c;
  • a := b - c;
  • a := b * c;
  • a := b DIV c;
  • a := b MOD c;
  • a := ABS(b);
  • a := SQR(b);
    for CHAR:
  • d := 'w';
  • d := e;
  • d := SUCC(e);
  • d := PRED(e);
  • a := ORD(d);
  • d := CHR(a);
    for BOOLEAN:
  • f := FALSE;
  • f := g;
  • f := i > 100;
  • f := g AND h;
  • f := g OR h;
  • f := g XOR h;
  • f := NOT g;
Note, that right part of assignment can contain operation or function. All variables can contain index.

The typical structures of this operator are the following:

{variable} := {variable or constant}

{variable} := {variable or constant} {operation sign} {variable or constant}

{variable} := {function name} ( {variable or constant} )



Back to:
index top

Step 1: get first operand

First operand is always placed in the definite processor register. In "E97" case it is R1, in Intel - AX.

There are several variants of the first operator:

  • (v1) simple variable (a)
  • (v2) variable with concrete index (a[2])
  • (v3) variable with another variable as index (a[i])
  • (v4) local variable in procedure or function
  • (v5) constant (23, 'y', TRUE)
Variants (v1) and (v2) compiler translates into instruction of extracting concrete memory address to fix register.

(v3) is more complex variant, because compiler can't calculate array element address - it "doesn't know" the value of index variable. So it has to generate a little piece of code, that will do this calculation during run-time.

Variant (v4) will be discussed later.

(v5) is a simple case: it transforms to the only processor instruction, which put constant value into fix register. All constants for processor are presented in numeric form, of course.
"E97" uses different type of instruction for "long" (>15) and "short" (<=15) constants.

After this step in any case the first operand is placed in fixed register (R1 in "E97" and AX in Intel processor).



Back to:
index top E97 Intel

Step 2: do operation

This step is optional: when right part of the operator doesn't contain any operation or function it is just skip.

In "E97" and in Intel compilers this step is realized slightly different: in last case the value of the second operand is previously extracted to CX register and only then operation is made. I have to use this additional action because Intel's instruction set is more complex and not "symmetrical" for all operations. For example, it can add memory cell value to AX, but can't multiply them. By the way, multiplication and division have some other individualities in Intel.
"E97" as a virtual educational computer, has more simple and logical instruction set.

When the right part of the operator has operation, "COMPAS" generates code, which realize this operation with fix register (R1 in "E97" and AX in Intel) and the second operand. The result is always put to the same fix register. I want to remind You, that Intel code takes the second operand from CX register.

All the words about variants (v1)-(v5), written above about operand's extraction, remain in force.

If right part of the assignment contains function, "COMPAS" use two ways of translation. For simple functions (SQR, SUCC, PRED) it inserts processor instruction into code program. For more complex (ABS) compiler calls subroutine from the run-time library. Note, that MOD operation is realized the same way.



Back to:
index top E97 Intel

Step 3: save result

The last step saves the result from fixed register (R1 or AX) to memory. This step is "glassy symmetrical" to step 1, so if You have read the discription of variants (v1)-(v4), it's evident for You what instructions are used.



Back to:
index top E97 Intel