created 07/03/2003

# Chapter 24 Programming Exercises

In the Settings menu of SPIM set Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed Branches OFF, Delayed Loads OFF, Mapped IO ON, Quiet OFF.

Run the programs by loading the trap handler (as explained in the chapter) and clicking SimulatorGo and then OK in the pop-up panel.

## *Exercise 1 -- String Length

Write a program that repeatedly asks the user for a string and then calculates and prints out the string length. Stop the program when the string is empty (when the user hits "enter" without anything else on the line.)

Be sure to reserve enough memory in the `.data` for the string. Use indexed addressing to scan through the string. Strings read in with the trap handler service include a '\n' character at the end, followed by the null termination. Don't count the '\n' or the null as part of the string length.

Click here to go back to the main menu.

## ***Exercise 2 -- String Reversal

Write a program that asks the user for a string. After reading the string into a buffer, copy it in reversed order to a second buffer. Write out the reversed string. End the program when the first string entered is empty (when it consists only of the end of line character.)

The input strings will be terminated with "\n\0". Don't include these characters in the middle of the concatenated string.

Click here to go back to the main menu.

## ***Exercise 3 -- String Concatenation

Write a program that repeatedly asks the user for two strings. The strings are placed in separate buffers in memory. Now, in a third buffer, create a string that is the concatenation of the two strings. Print out the new string.

Click here to go back to the main menu.

## ***Exercise 4 -- Triangle of Stars

Write a program that writes the following pattern to the simulated terminal:

        *
       ***
      *****
     *******
    *********

The last row starts in column one.

Click here to go back to the main menu.

## **Exercise 5 -- Dot Product

Compute the dot product of two vectors. A vector is an array of integers. Both vectors are the same length. Ask the user for the length of the vectors. Then prompt for and read in the value of each element of each vector. Reserve space in memory for vectors of up to 10 elements, but allow vectors of any size one through 10.

              .data
    length:   .word 0
    vectorA:  .space 40    # space for 10 integers
    vectorB:  .space 40    # space for 10 integers

The dot product of two vectors is the sum of the product of the corresponding elements. For example, (1, 2, 3) dot (4, 5, 6) is 1*4 + 2*5 + 3*6.

After computing it, write out the dot product to the monitor.

Click here to go back to the main menu.

## ***Exercise 6 -- Linear Search

Declare an array of integers:

              .data
    size:     .word 12
    array:    .word 50,12,52,-78,03,12,99,32,53,77,47,00

Write a program that repeatedly asks the user for an integer to search for. After the user enters the integer, the program scans through the array element by element looking for the integer. When it finds a match it writes a message and reports the index where the integer was found. If the integer is not in the array it writes a failure message.

Click here to go back to the main menu.

## ***Exercise 7 -- Averaging Filter

Write a program that processes an array by applying an averaging filter to it. An averaging filter works like this: create a new array where each element at index J is the average of the three elements from the old array at indexes J-1, J, and J+1

              .data
    size:     .word 12
    array:    .word 50,53,52,49,48,51,99,45,53,47,47,50
    result:   .word 0,0,0,0,0,0,0,0,0,0,0,0

In the above, the second element of the result is the average of 50, 53, and 52. The first and last elements of the new array are copies of the corresponding first and last elements of the old array.

After processing, write the old and new arrays to the monitor.

Click here to go back to the main menu.

## ****Exercise 8 -- Selection Sort

Perform selection sort on an array in memory:

              .data
    size:     .word 20
    array:    .word 99,23,45,82,09,34,71,64,88,42,12,87,33,36,83,18,17,04,52,46

The selection sort algorithm looks like this:

    int out, in, min, temp;

    // each time the outer loop is
    // executed, replace the element
    // at out with the minimum of the
    // elements to its right.
    for ( out=0; out<size-1; out++ )
    {
      min = out;

      // find the location of the minimum
      // in the remaining elements
      for ( in=out+1; in<size; in++ )
        if ( array[in] < array[min] )
          min = in;

      // swap the element at out with
      // the minimun of the remaining elements
      temp       = array[out];
      array[out] = array[min];
      array[min] = temp;
    }

Write out the array after it has been sorted.

Click here to go back to the main menu.

End of Exercises