COMP1040 MIPS

Question 1 [20 Marks]
Write a program in MIPS32 assembly language which takes three input arguments: register A will receive a character, register B will receive another character, and register C will receive the initial address of a string. Within the string, your program will replace any occurrence of the character stored in register B by the character stored in register A. Finally, output the replaced string in register C. For example:
– Input character in register A: c
– Input character in register B: f
– Input string in register C: affording to use a silver fork
– Output string in register C: according to use a silver cork
Detailed marking rubrics can be found in Appendix 2.
CS Help, Email: tutorcs@163.com
Question 2 [20 Marks]
Implement a MIPS version of the following C code, including the C function strncat() and strstr(), which is specified in the IEEE 1003.1-2017 Standard.
Given the following C code snippet:
1 #include
2 #include
4 int count substr n(const char*, const char*, int); 5
6 int main() {
7 const char s1[25] = STRING 1;
8 const char s2[25] = STRING 2;
9 int n = N;
11 int result = count substr n(s1, s2, n);
12 printf(“%d”, result);
14 return 0;
17 int count substr n(const char* p1, const char* p2, int n) {
18 char p2 new[25] = STRING 3;
19 int count = 0;
20 strncat(p2 new, p2, n);
22 char* temp = strstr(p1, p2 new);
23 while (temp != NULL) {
24 count++;
25 temp++;
26 temp = strstr(temp, p2 new);
29 return count;
Code Help
Note: To execute the code snippet above, you may need to replace the RED colored content by syntactically correct the C code. For example:
7 char s1[10] = “Forever Shared Future”;
8 char s2[10] = “revisit”;
9 int n = 2;
18 char p2 new[25] = “a”; Then the program should output
Your are required to implement the MIPS assembly code that returns EXACTLY the same result as executing the code snippet above. You should not change any parts in the above code snippet, except the red colored content. No mark is awarded to this question if this requirement is not followed.
Once you execute your MIPS code, by invoking the appropriate syscall, at the QtSpim console your program should intake a single string in the following format (Px indicates Parameter x):
P1:STRING 1;P2:STRING 2;P3:N;P4:STRING 3;
where the RED colored content should be replaced in the same way as the above C snippet.
For example:
P1:”Forever Shared Future”;P2:”revisit”;P3:2;P4:”a”;
After your code is executed based on the above input, at the QtSpim console, your program should output the corresponding result. For the example above, the output should be:
Exceptions:
What happens if the above C code returns an run-time error, after you supplied the input and run? Note: a run-time error is the type of abnormality that the compiler does not recognize, and can finish compiling; but when executing the code, the error will occur, such as dividing by zero, and index out-of-range.

Programming Help