CMPS-2240 Lab 4
Arrays, Loops, 2-Way Branching

Eddie Rangel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Notice:
Due: Monday Mar. 16th by 4pm

Introduction

Goals:
Implement a loop control structure
Implement an if-else control structure
Use a loop to manipulate an array
Write a program that uses a nested loop

Resources:
MIPS Instruction Reference
MIPS Tutorial Part 5 (Ch 17 - Ch 20):
Week 4 examples
Appendix A.10 in the text

Two files to be submitted:
arrays.s
nested.s

Loops

Write a program named arrays.s that does the following:

1. Declares an array to hold 30 integers.
2. Uses a loop to initialize the array with the first 30 odd integers.
3. Display the same values to the screen within the loop.
4. Uses a second loop to step through the array and display only those integers that are evenly divisible by 3 or 7.

Sample output...


$ spim -f arrays.s
Loaded: /usr/share/spim/exceptions.s
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 

3 7 9 15 21 27 33 35 39 45 49 51 57 
$ 

Do not display anything for the other values in the array. You will need to implement if-else control structures within your loops.

First look at
http://www.cs.csub.edu/~eddie/cmps2240/code/lab4/loops.s
for an example of an if-else control structure inside a loop.

Then look at
http://www.cs.csub.edu/~eddie/cmps2240/code/lab4/loops2.s
for an example of filling an array and then displaying the values in the array. (The loops2.s code is closer to what you want your solution to do.)

copy the files to your own lab4 directory like this:
from odin
cp /home/fac/eddie/public_html/cmps2240/code/lab4/* .
from local machine
scp odin.cs.csubak.edu:/home/fac/eddie/public_html/cmps2240/code/lab4/* .

Space
Reread lab 2 if you need a refresher on how to allocate arrays. Note that an integer requires 4 bytes.

Psuedocode
The psuedocode for your program is this:

Data segment: Allocate space for an array
Text segment: Fill an array with odd integers
and also iterate over the array and display the contents
Iterate over the array and display the elements divisible by 3 or 7

Check Off

Demonstrate to the instructor that your program displays the numbers from item 2 above, then displays the numbers from item 4 above.
This is worth 5 points.

Nested Loops

In this part of the lab you will write a program named nested.s that uses nested loops to display this to the screen:


$ spim -f nested.s

 . O O O O O O O O
 O . O O O O O O O
 O O . O O O O O O
 O O O . O O O O O
 O O O O . O O O O
 O O O O O . O O O
 O O O O O O . O O
 O O O O O O O . O
 O O O O O O O O .

Draft your algorithm in C first, then implement the algorithm in MIPS.

Review http://www.cs.csub.edu/~eddie/cmps2240/code/lab4/doors.s
for an example of a nested loop structure.

Check Off
Demonstrate to the instructor that your program displays the pattern above. This is worth 5 points.

What to turn in

Place your lab programs arrays.s and nested.s in the correct directory on odin.

directory name: /home/fac/yourname/2240/4/
Replace yourname with your own odin username!