IUP Computer Science
COSC 300     Fall 2005

Project #3
(Due 10 November 2005)

You are to write several MIPS assembly language procedures that deal with strings.  A main program and one procedure, findnext,  is provided to test your procedures.  The program is in the file p3-template.s and can be found on the I: drive at  I:\jlwolfe\300\fall05\p3-template.s and on the P: drive at P:\courses\fall2005\COSC\COSC300\001\information\p3-template.s.  The procedures you need to write are described below.

length - a procedure to determine the number of characters in a null-terminated string, not counting the null.  This procedure takes one argument:  $a0 must contain the address of the start of the string.  The procedure returns one result in $v0, the number of characters.

concatenate - a procedure that joins two null-terminated strings to form one.  This procedure takes three arguments:  $a0 must contain the address of the first string, $a1 must contain the address of the second string, and $a3 must contain the address where the concatenated string is to be placed.  The second string should be attached after the first string, not vice versa.  The strings referred to by $a0 and $a1 must not be changed by doing this process.

substr - a procedure that extracts a specified portion of a null-terminated string and creates a new null-terminated string at a specified location.  This procedure takes four arguments:  $a0 must contain the address of the string from which the extraction is being made, $a1 must contain the starting position in the string where the extraction is to begin made (positions are numbered starting with 0), $a2 must contain the number of characters to extract, and $a3 must contain the address of the place where the substring is to be placed.  The original string must not be changed by the procedure.

insert - a procedure that inserts one string (src) into another string (dest) at a specified position.  This procedure takes four arguments:  $a0 must contain the address of the string to be inserted (src), $a1 must contain the address of the string into which the insertion will be made (dest), and $a2 must contain the character position in dest at which src is to be placed.  Positions are numbered starting with 0.  src must not be changed by the procedure.

Generally, these procedures do not require error checking.  There are two exceptions.  substr should check to make sure the value specified $a1 is a valid position (one that exists in the source string).  If it is invalid, a message such as "Invalid position" should be displayed.  insert should check the value specified in $a2 to make sure it is a valid position.  If it is invalid, a message such as "Invalid position" should be displayed.  I recommend making a separate procedure to do the validation of these values because you are really checking for the same problem and producing the same error message.

The procedure that you are given, findnext, will be useful to doing a variety of tasks.  You will see it called many times in the main program.  You can also use it in the procedures you write.  findnext takes two arguments:  $a0 must contain the address of where the search should start and $a1 must contain the character to search for.  findnext returns the address where the character is found in $v0.  When I wrote these procedures, I used findnext in doing length.  Then I used length in the procedure to validate position.  I also used substr and concatenate in doing insert.

Hand in a printout of your well documented program.  Also, copy the .s file to the handin folder on the P: drive for this course.  You should name the .s file after yourself (I might name mine p3-wolfe.s).
 

The test program that you are being given does the following:

  1. It prompts for a string containing two n's and then prompts for a second string containing two r's.  Each string must be at least 12 characters in length and have the required characters.
  2. It uses findnext to replace the newline characters at the ends of the strings with nulls
  3. It uses length to determine the number of characters in each string so that it can display the last 10 characters of each string.
  4. It uses findnext to locate the two n's in the first string and the two r's in the second string.  It then uses this information with substr to extract the substrings beginning and ending with n and r.
  5. It uses concatenate to join the two substrings and then displays the concatenated string.
  6. It tries to use insert in a manner which should cause an error message to be displayed.  It then uses insert to inject a given string (at label plugin) at character position 12 of the first string entered.  The resulting string is then displayed.