****************************************************************** ***************** Assignment #3 for CS341 ********************* ***** Due Monday 10/15/01 ***** ****************************************************************** The goal of this assignment is to gain experience in translating functions and function calls to assembly. This assignment involves implementing a limited string utility program. The utilities provided are: (i) Initialize a string (ii) Display a string (iii) Return length of a string (iv) Concatenate two strings (v) Find start position of the first occurence of a substring within given string (vi) Erase given substring within another string The main program accepts commands from the keyboard. Upon parsing the command, it prepares the appropriate parameters and invokes the required function. It displays results if necessary on the screen. The function may also need to invoke another function before returning to the main program. Initialize the data area of your program with the three strings below and leave at least another 200 bytes for new strings to be created in due course. Also leave about 100 bytes of storage for temporary auxiliary variables. (For example, you may wish to store starting locations and/or lengths of strings here.) .data s1: .asciz "Hello " s2: .asciz "How far is Santa Fe from here?" s3: .asciz "qwerty7&* pQR" move: .skip 200 - (. - s1), -1 temp: .skip 100, -1 User commands that make use of the above functions are elaborated upon below. ----------------------------------------------------------------------------- *** Example of (i) i 4 "there" Interpretation: Initialize string s1 to there. So now the data area in your program will appear as: s1: .asciz "Hello " s2: .asciz "How far is Santa Fe from here?" s3: .asciz "qwerty7&* pQR" s4: .asciz "there" move: .skip 200 - (. - s1), -1 temp: .skip 100, -1 ----------------------------------------------------------------------------- *** Example of (ii) d 2 Interpretation: Display string s2 on the screen. In this case, the following should be displayed. How far is Santa Fe from here? ----------------------------------------------------------------------------- *** Example of (iii) l 1 Interpretation: Display length of string s1. Do not include the null termination in the length computation. Your program should display 6 ----------------------------------------------------------------------------- *** Example of (iv) c 1 4 1 Interpretation: Concatenate strings s1 and s4 (i.e. append s4 to s1 ) and save it as s1. Display s1 on the screen. If s1 already exists (as in this case), it should be re-initialized. Further, if the space occupied by the earlier s1 is too small, s1 should be relocated in your data area (again, as in this case). Make sure the main program "remembers" the new location of s1. s1: .asciz "Hello " ! This label no longer points ! to s1 since s1 is relocated. s2: .asciz "How far is Santa Fe from here?" s3: .asciz "qwerty7&* pQR" s4: .asciz "there" s1': .asciz "Hello there" ! New location of s1 . . . ----------------------------------------------------------------------------- *** Example of (v) f "far" 2 Interpretation: Find the first occurence of the pattern "far" in string s2. If "far" is contained in s2, the index of f (starting character in "far") should be returned. In this case, the program should display 4. If the given substring does not occur in s2, the subroutine should return -1 and the user should be so informed. ----------------------------------------------------------------------------- *** Example of (vi) e "is Santa Fe" 2 Interpretation: Erase the first occurence of "is Santa Fe" from s2 and display the result. In this case, "How far from here?" should be displayed. The new s2 should begin at the same location as the old s2. If the content of an entire string is deleted, it ought to still exist as a null string. However, for the purpose of this assignment, we assume that a null string is no string at all. In effect, this corresponds to a delete. Finally, the subroutine to erase a substring should invoke the subroutine that finds a substring in a given string. ----------------------------------------------------------------------------- NOTE: Assume that the id of a string ranges between 1 and 15 (hexadecimal 0F) and that strings in the command line will be refered to in hex. Do not write code to handle any command line errors. Assume the user will not enter an invalid command or illegal parameters. Your program should keep track of the location of the end of the last valid string stored since any new string will commence from that point onward. For example, in command c 1 4 1 (see Example of (iv) above), your program should realize that the new s1 cannot fit in the space occupied by the old s1. It should also figure out where the new s1 should reside - just after the end of s3 (in this case). There are two versions of this assignment -- the "regular version" (and the "extra credit" version). For extra credit you need to keep track of holes or wasted spaces in memory caused by erasure of substrings within strings. Holes can also occur because s1 was relocated as in the example on string concatenation above. Then, when a new string needs to be created or a result needs to be stored, a best fit or first fit approach (greedy strategy) should be employed to minimize wasted memory. Again, this is not part of the regular assignment.