** Next:** Program Relocation
**Up:** TAL
** Previous:** Machine Code Format

##

This example shows how machine code is generated for a simple Pascal
program. The example program computes

the sum of the squares of the first NUMINTS integers.

The program is written using a `while` loop instead of a `for`
loop to produce assembly code which more closely resembles the Pascal
program. `NUMINTS` is a constant and the program uses two variables,
`Count` and `Squares` to perform the calculation.
The result is left in a variable called Squares.

program SumSquare;
const
NUMINTS = 10 {number of integers to sum}
var
Count, Squares: integer;
begin {SumSquare}
Squares := 0;
Count := 1;
while ( Count <= NUMINTS ) do
begin {while}
Squares := Squares + (Count * Count);
Count := Count + 1;
end; {while}
end. {SumSquare}

The MAL squares program uses registers for the variables `Count` and
`Squares`. The number of integers in the sum is stored in memory
at location `numints`.

#
# MAL program to sum the squares of
# the first 'numints' integers.
#
# Registers:
#
# $2 sum of squares
# $3 branch condition
# $5 numints
# $6 squares
# $7 count
#
.data
numints: .word 10
.text
__start: li $7, 1
li $2, 0
lw $5, numints
while: sub $3, $7, $5
bgtz $3, end
mul $6, $7, $7
add $2, $2, $6
add $7, $7, 1
j while
end: done

When the MAL code is assembled, the following TAL program is produced.
Note that several MAL instructions must be synthesized from one or
more TAL instructions.

*CS 301 Class Account *

Mon Dec 1 23:34:28 AST 1997