created 06/29/2003


Chapter 20 Programming Exercises


For these programming exercises, use only those instructions that have been discussed so far in these notes:


add div mflo slt, slti
addi divu mult sltu, sltiu
addiu j multu sra
addu lb nor srl
and lbu or sub
andi lh ori subu
beq lhu sb sw
bgez lui sh xor
bltz lw sll xori
bne mfhi    

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

Run the programs by setting the value of the PC to 0x400000 and then single stepping (pushing F10) or by multiple stepping (push F11 and enter a number of steps). Observing the results in the SPIM window.

A carefully thought-out and debugged flow chart will cut in half the time it takes to do one of these exercises. Plus you will learn good coding techniques and gain insight into programming languages and language translation.


*Exercise 1 — To Lower Case

Declare a string in the data section:

          .data
string:   .asciiz    "ABCDEFG"

Write a program that converts the string to all lower case characters. Do this by adding 0x20 to each character in the string. (See Appendix F to figure out why this works.)

Assume that the data consists only of upper-case alphabetical characters, with no spaces or punctuation.

Click here to go back to the main menu.


***Exercise 2 — Capitalization

Declare a string in the data section:

          .data
string:   .asciiz    "in a  hole in the   ground there lived a hobbit"

Write a program that capitalizes the first letter of each word, so that after running your program the data will look like this:

          .data
string:   .asciiz    "In A  Hole In The   Ground There Lived A Hobbit"

Easy version: assume that the data consists only of lower case characters and spaces. There may, however, be several spaces in a row. Be sure to capitalize the first letter of the string.

Medium-hard version: assume that the data consists only of upper and lower case characters and spaces. Alter a character only if it is lower case and follows a space.

Click here to go back to the main menu.


***Exercise 3 — Space Removal

Declare a string in the data section:

          .data
string:   .asciiz    "Is  this a dagger    which I see before me?"

Write a program that removes all the spaces from the string so that the resulting string looks like:

          .data
string:  .asciiz    "IsthisadaggerwhichIseebeforeme?"

Be sure to end the result string with a null after its final character.

Easy version: declare a second buffer to hold the result string. Transfer non-space characters from the input string to the result string.

Medium-hard version: Use only the buffer that holds the original string. Use two character pointers, one for the current character and another for its destination.

The logic for this can be tricky. Figure out how you would do it with characters arrays in C or Java before you try it with assembly. For testing, use a data string such as the following:

          .data
string:  .asciiz    "aaaa bbbb  cccc   dddd    eeee"

Click here to go back to the main menu.


**Exercise 4 — Array Maximum and Minimum

Declare an array of integers, something like:

        .data
size:   .word 8
array:  .word 23, -12, 45, -32, 52, -72, 8, 13

Write a program that determines the minimum and the maximum element in the array. Assume that the array has at least one element (in which case, that element will be both the minimum and maximum.) Leave the results in registers.

Click here to go back to the main menu.


**Exercise 5 — Ascending Numbers

Declare an array of integers, something like:

        .data
size:   .word 10
array:  .word 2, 4, 7, 12, 34, 36, 42, 8, 57, 78

Write a program that determines if the numbers form an increasing sequence where each integer is greater than the one to its left. If so, it sets a register to 1, otherwise it sets the register to 0.

Of course, write the program to work with an array of any size, including 0. Arrays of size 0 and size 1 are considered to be ascending sequences. The array can contain elements that are positive, negative, or zero. Test the program on several sets of data.

Click here to go back to the main menu.


*Exercise 6 — Paired Data

In this program data comes in pairs, say height and weight:

        .data
pairs:  .word 5                  # number of pairs
        .word 60, 90             # first pair: height, weight
        .word 65, 105
        .word 72, 256
        .word 68, 270
        .word 62, 115

Write a program that computes the average height and average weight. Leave the results in two registers.

Click here to go back to the main menu.


**Exercise 7 — Array Reversal

Declare an array of integers in the usual way:

        .data
size :  .word 7                     # number of elements
        .word 1, 2, 3, 4, 5, 6, 7

Write a program that reverses the order of elements in the array. The result will be as if the array were declared:

        .data
size :  .word 7                     # number of elements
        .word 7, 6, 5, 4, 3, 2, 1

Test that the program works on arrays of several lengths, both even and odd lengths.

Click here to go back to the main menu.


End of Exercises