CMPS-2240 Lab 4
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

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:
lab4a.s
lab4b.s

Part 1: Loops

Write a program named lab4a.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 lab4b.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
loops.s
for an example of an if-else control structure inside a loop.

Then look at
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 2240/4 directory like this:
from Odin
cp /home/fac/gordon/public_html/2240/code/lab4/* .
from local machine
scp odin.cs.csub.edu:/home/fac/gordon/public_html/2240/code/lab4/* .

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

Psuedocode
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

    declare integer arr[30]
    declare integer i
fill_top:
    i <-- 0
    arr[i] <-- (i * 2 + 1)
    i <-- i + 1
    if (i < 30)
        goto fill_top
display_top:
    i <-- 0
    print arr[i]
    i <-- i + 1
    if (i < 30)
        goto display_top
    //	
    //display numbers divisible by 3 or 7	
    i <-- 0
div37_top:
    a <-- arr[i]
    //divide by 3 and look at the remainder
    b <-- a / 3
    if (remainder == 0)
        print arr[i]
        goto next_num
    //divide by 7 and look at the remainder
    b <-- a / 7
    if (remainder == 0)
        print arr[i]
next_num:
    i <-- i + 1
    goto div37_top

Nested Loops

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


$ spim -f lab4b.s

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

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

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

What to turn in

Files on Odin:

   2240/4/lab4a.s
   2240/4/lab4b.s