A tree pattern of the form:
generates a Move instruction:
where aPlace represents the register, stack position or
memory location assigned to a.
MOVE aPlace,T
Suppose Op represents an arithmetic operation and consider an abstract syntax tree for the statement t = a Op b:
One possible code sequence is:
MOVE aPlace,Reg
OP bPlace,Reg
MOVE Reg,T
This is the method used in the example of the previous section. Of course, some machines require that special registers be used for operations such as multiplication and division.
IF statements can be represented by an abstract syntax tree such as:
A typical code sequence is:
(Code for condition)
BRANCHIFFALSE Label1
(Code for Statements1)
BRANCH Label2
Label1: (Code for Statements2)
Label2:
Example 3 illustrates this for the statement:
IF a < b THEN Max = b ELSE Max = a;
In Example 3, aPlace and bPlace mey refer to the variables a and b themselves, if the machine allows two memory operands. For machines such as the 86-family (PC-clones) which require that one operand be in a register, the instruction:
COMPARE aPlace,bPlace
MOVE Reg,aPlace
CMP Reg,bPlace
if neither a nor b have been previously assigned to a register.
Loops are just conditionals whose code is repeated. Consider the loop:
LOOP While condition DO
Statements
ENDLOOP
An abstract syntax tree is:
A reasonable code sequence is:
Label1: (Code for NOT condition)
BRANCHIFTRUE Label2
(Code for Statements)
BRANCH Label1
Label2:
Example 4 shows such a loop:
Send questions and comments to: Karen Lemone