next up previous
Next: Program Relocation Up: TAL Previous: Machine Code Format

Example: Sum of Squares

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

displaymath640

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.


#
#	TAL squares program to illustrate branch
#	and jump address calculation.  Computes the 
#	sum of squares of the first 'numints' integers.
#
#	Registers:
#
#	    $2	sum of squares
#	    $3	used for TAL synthesis
#	    $5	numints
#	    $6	squares
#	    $7	count
#
	.data
numints:	.word	10
	.text
__start:	addi	$7, $0, 1
		addi	$2, $0, 0
		lui	$5, 0x1001
		ori	$5, $5, 0
		lw	$5, ($5)
while:		sub 	$3, $7, $5
		bgtz	$3, end
		mult	$7, $7
		mflo	$6
		add	$2, $2, $6
		addi	$7, $7, 1
		j 	while
end:		addi	$2, $0, 10
		syscall

ibm1% spim
SPIM Version 4.4.2, Release:  April 22, 1993
Copyright 1990-92 by James R. Larus (larus@cs.wisc.edu)
Modified to read SAL code by Scott Kempf (scottk@cs.wisc.edu)
(spim) read "squares.s"
(spim) step
[0x00400000]     0x20070001  addi $7, $0, 1     # addi  $7, $0, 1
[0x00400004]     0x20020000  addi $2, $0, 0     # addi  $2, $0, 0
[0x00400008]     0x3c051001  lui $5, 4097       # lui   $5, 0x1001
[0x0040000c]     0x34a50000  ori $5, $5, 0      # ori   $5, $5, 0
[0x00400010]     0x8ca50000  lw $5, 0($5)       # lw    $5, ($5)
[0x00400014]     0x00e51822  sub $3, $7, $5     # sub   $3, $7, $5
[0x00400018]     0x1c600005  bgtz $3 20 	# bgtz  $3, end
[0x0040001c]     0x00e70018  mult $7, $7        # mult  $7, $7
[0x00400020]     0x00003012  mflo $6    	# mflo  $6
[0x00400024]     0x00461020  add $2, $2, $6     # add   $2, $2, $6
[0x00400028]     0x20e70001  addi $7, $7, 1     # addi  $7, $7, 1
[0x0040002c]     0x08100005  j 0x00400014       # j     while
[0x00400014]     0x00e51822  sub $3, $7, $5     # sub   $3, $7, $5
[0x00400018]     0x1c600005  bgtz $3 20 	# bgtz  $3, end
[0x00400030]     0x2002000a  addi $2, $0, 10    # addi  $2, $0, 10
[0x00400034]     0x0000000c  syscall    	# syscall
(spim) exit
ibm1%




CS 301 Class Account
Mon Dec 1 23:34:28 AST 1997