Question 3 [25 Marks]
Implement a MIPS-version of the strncmp() C function, as standardized by the ISO C stan- dard.
Background
The format of strncmp() is defined below:
int strncmp(const char *s1, const char *s2, size_t n); Quoted from the ISO description:
¡°The strncmp() function shall compare not more than n bytes (bytes that follow a NUL character are not compared) from the array pointed to by s1 to the array pointed to by s2. The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared. Upon successful completion, strncmp() shall return an integer greater than, equal to, or less than 0, if the possibly null-terminated array pointed to by s1 is greater than, equal to, or less than the possibly null-terminated array pointed to by s2 respectively.¡±
Your Task
Given the following C code snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include
#include
int main() {
char s1[5] = STRING 1;
char s2[6] = STRING 2;
int n = N;
int result = strncmp(s1, s2, n);
printf(“The result is %d.”, result);
return 0;
}
4
Note: To execute the code snippet above, you may need to replace the RED colored content by syntactically correct C code. For example:
6 char s1[5] = “abcd”;
7 char s2[6] = “abcde”;
8 int n = 2;
Then the program should output
The result is 0.
Your are required to implement the MIPS assembly code that returns EXACTLY the same result as effectively calling the strncmp() function in line 10 of the code snippet above. You should not change any parts in the above code snippet, except the red colored content. No marks is awarded to this question if this requirement is not followed.
Input:
Once you execute your code, at the simulator console, by invoking the appropriate syscall, your program should intake a string in the following format (Px indicates Parameter x):
P1:STRING 1;P2:STRING 2;P3:N;
where the RED colored content should be replaced in the same way as the above C snippet.
For example:
P1:”abcd”;P2:”abcde”;P3:2;
Output:
After your code is executed based on the above input, by invoking the appropriate syscall, at the simulator console, your program should output the corresponding strncmp result. For the example above, the output should be:
0
What happens if strncmp() returns an error?
At the simulator console, your program should output the following message below. Note that: you do not have to specify the error, but make sure your program generates exactly this error message whenever strncmp() generates any error message.
An error has occurred.
5
Resources
1. You may need to refer to the ASCII Table. Note: No ¡°Extended ASCII Codes¡± need to be considered in this question.
2. Use https://www.programiz.com/c-programming/online-compiler/ as the standard strncmp() behavior reference. You can copy and past the C code snippet in Page 4 into this online C gadget and run, to obtain the referential strncmp() results. Namely, your program should output exactly the same ¡®result¡¯ value (except errors) as the C code outputs in this online gadget.
3. Click here to refer to the formal definition of strncmp() function.
4. Feel free to post questions in Moodle Discussion Forum, or email us regarding your doubts
for clarification. Especially, with doubts in this question, contact Dr. Heng directly.
Evaluation
1. Out of the 25 marks for this question, TEN (10) test cases will be employed to evaluate the functionality of your program. Example test case:
Each correct answer is rewarded 2.5 marks. Each incorrect answer is rewarded 0 mark. Your output answer should follow exactly the output requirement given in this question, otherwise it would be deemed incorrect.
2. Unless otherwise specified, we will use https://www.programiz.com/c-programming/ online-compiler/ as the standard strncmp() behavior reference, to evaluate your pro- gram output.
3. Good luck! Enjoy MIPS Programming. Remember to submit your coursework on time.
6