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

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

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
Use the Mips divide instruction

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

The sample files are also available on Odin at:
   /home/fac/gordon/p/2240/code/lab5/

   cd 2240/5
   cp /home/fac/gordon/p/2240/code/lab5/* .
Two files to be submitted in your Odin folder: 2240/5/
  arrays.s
  nested.s

Loops

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

1. Declares an array to hold 32 integers.
2. Uses a loop to initialize the array with the first 32 even numbers.
3. Displays the array 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 11.

Sample output...

$ spim -f arrays.s
Loaded: /usr/share/spim/exceptions.s

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64

6 12 18 22... 

Note: do not display the ellipsis! It indicates more output.

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/~gordon/2240/code/lab5/loops.s
for an example of an if-else control structure inside a loop.

Then look at
http://www.cs.csub.edu/~gordon/2240/code/lab5/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.)

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

Overview of your program is this:
  · Data segment: Allocate space for an array
  · Text segment: Fill an array with even integers
  · Iterate over the array and display the contents
  · Iterate over the array and display the elements divisible by 3 or 11


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

 . @ @ @ @ @ @ @
 @ . @ @ @ @ @ @
 @ @ . @ @ @ @ @
 @ @ @ . @ @ @ @
 @ @ @ @ . @ @ @
 @ @ @ @ @ . @ @
 @ @ @ @ @ @ . @
 @ @ @ @ @ @ @ .

Diver down flag

You might...
Draft your algorithm in C first, then implement the algorithm in MIPS.
You may enlarge the square some if you want.

Review https://www.cs.csub.edu/~gordon/2240/code/lab5/rectangle.s
for an example of a nested loop structure.

For some extra-credit, render this pattern instead of the one above.

  @ @ @ @ @ @ @ .
  @ @ @ @ @ @ . @
  @ @ @ @ @ . @ @
  @ @ @ @ . @ @ @
  @ @ @ . @ @ @ @
  @ @ . @ @ @ @ @
  @ . @ @ @ @ @ @
  . @ @ @ @ @ @ @

You may display both patterns if you like.
Diver up flag?

What to turn in

Place your lab programs in the correct directory on Odin.
  2240/5/arrays.s
  2240/5/nested.s