created 07/07/2003; revised 08/07/2020

# Chapter 31 Programming Exercises

NOTE: If you have read the chapters on subroutine linkage, write each exercise as a main program calling a subroutine. Use either the stack-based linkage or the frame-based linkage convention. Otherwise write these as an ordinary main.

In the Settings menu of SPIM set Bare Machine OFF, Accept Pseudo Instructions ON, Enable Branch Delays OFF, Enable Load Delays OFF, Enable Mapped IO OFF, Load Exception Handler ON.

## Exercise 1 — Arithmetic Expression

Write a program that computes the value of the following arithmetic expression for values of x and y entered by the user:

```5.4xy - 12.3y + 18.23x - 8.23
```

## *Exercise 2 — Harmonic Series

Write a program that computes the sum of the first `n` terms of the harmonic series by using a loop:

```1/1 + 1/2 + 1/3 + 1/4 + ... + 1/n
```

This sum gets bigger and bigger without limit as more terms are added in. Ask the user for the number of terms to sum, compute the sum and print it out. Of course, you will need to use floating point division.

There are several ways that this program could be written. The sensible way is to use both an integer loop counter that is incremented by integer 1 and a separate floating point divisor that is incremented by 1.0 in each loop iteration.

## *Exercise 3 — Zeno's Series

Write a program that computes the sum of the first `n` terms of this series:

```1/2 + 1/4 + 1/8 + 1/16 + ... + 1/2n
```

Unlike the previous series, this one converges. For large `n` the sum is very close to one. Ask the user for a number of terms to sum, compute the sum and print it out.

As above, there are several ways that this program could be written. Try to find a sensible way to do it.

## **Exercise 4 — Web Page RGB Colors

Colors on a Web page are often coded as a 24 bit integer as follows:

```RRGGBB
```

In this, each R, G, or B is a hex digit 0..F. The R digits give the amount of red, the G digits give the amount of green, and the B digits give the amount of blue. Each amount is in the range 0..255 (the range of one byte). Here are some examples:

 FFFFFF 7F7F7F FF0000 00FF00 0000FF FF00FF 7F007F 70A0F0 FF7F7F

Another way that color is sometimes expressed is as three fractions 0.0 to 1.0 for each of red, green, and blue. For example, pure red is (1.0, 0.0, 0.0), medium gray is (0.5, 0.5, 0.5) and so on.

Write a program that has a color number declared in the data section and that writes out the amount of each color expressed as a decimal fraction. Put each color number in 32 bits, with the high order byte set to zeros:

```          .data
color:    .word  0x00FF0000     # pure red, (1.0, 0.0, 0.0)
```

For extra fun, write a program that prompts the user for a color number and then writes out the fraction of each component.

## ****Exercise 5 — Polynomial Evaluation (Horner's Method)

Write a program that computes the value of a polynomial using Horner's method. The coefficients of the polynomial are stored in an array of single precision floating point values:

```          .data
n:        .word  5
a:        .float 4.3, -12.4, 6.8, -0.45, 3.6
```

Write the program so that the size and the values in the array may be easily changed. Initialize a sum to zero and then loop n times. Each execution of the loop, with loop counter j, does the following:

```sum = sum*x + a[j]
```

To test and debug this program, start with easy values for the coefficients.

```   * == easy program