
CS 2731
Computer Organization II -- Spring 2003
Laboratory 4 [Feb 3, 5]:
Simple program
and simple array
-
Prior to the laboratory, you should do the following:
-
Make a separate directory for laboratory 4 and put all of
the code that you develop for this lab in that directory.
-
Write two MIPS programs:
-
The first is a program that prints the first 20 Fibonacci
numbers on a line, with a space between each one, [Note: the Fibonacci
numbers are the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ..., each
number the sum of the two previous ones.]
-
Modify the first program into a second that allocates space
for an integer array (A) of size 20 and stores the first 20 Fibonacci numbers
into this array. Then it should print the numbers in this array, on one
line with a blank between each one.
- You should realize that pieces of these programs have already
been supplied and discussed in class: for example, printing an array.
-
Implementation Notes:
-
The first program can be based on the following C program
or on the Java program here
which has the same output as the C program:
four06% cat lab4_1.c
#include <stdio.h>
void main(void)
{
int F0, F1, F2;
int i, last;
F0 = 0; F1 = 1;
i = 0; last = 20;
do {
F2 = F0 + F1;
printf("%i ", F0);
F0 = F1;
F1 = F2;
i = i + 1;
} while (i < last);
printf("\n");
}
four06% cc -o lab4_1 lab4_1.c
four06% lab4_1
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
four06%
The second program can be based on the following C program
or on the Java program here
which has the same output as the C program:
four06% cat lab4_2.c
#include <stdio.h>
void main(void)
{
int A[20];
int i, last;
A[0] = 0; A[1] = 1;
i = 2; last = 20;
do {
A[i] = A[i-1] + A[i-2];
i = i + 1;
} while (i < last);
i = 0;
do {
printf("%i ", A[i]);
i = i + 1;
} while (i < last);
printf("\n");
}
four06% cc -o lab4_2 lab4_2.c
four06% lab4_2
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
four06%
For Checkoff:
-
Handle each program separately, finishing the first before
the second. Check out by giving a listing showing the source
and a run with output to
the lab instructor (or if necessary to the course instructor).
Revision date: 2003-01-31.
(Please use ISO
8601, the International Standard.)