程序代写代做代考 mips C assembler 2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions

2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions
Week 07 Weekly Test Questions Test Conditions
These questions must be completed under self-administered exam-like conditions. You must time the test yourself and ensure you comply with the conditions below.
You may complete this test in CSE labs or elsewhere using your own machine.
You may complete this test at any time before Saturday 25 July 21:00.
Weekly tests are designed to act like a past paper – to give you an idea of how well you are progressing in the course, and what you need to work on. Many of the questions in weekly tests are from past final exams.
Once the first hour has finished, you must submit all questions you’ve worked on.
You should then take note of how far you got, which parts you didn’t understand.
You may choose then to keep working and submit test question anytime up to Saturday 25 July 21:00
However the maximum mark for any question you submit after the first hour will be 50%
You may access this language documentation while attempting this test: C quick reference
You may also access manual entries (the man command).
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.
Set up for the test by creating a new directory called test07, changing to this directory, and fetching the provided code by running these commands:
Or, if you’re not working on CSE, you can download the provided code as a zip file or a tar file.
$ mkdir test07
$ cd test07
$ 1521 fetch test07
Test Complete!
Your time for this test has finished. You must submit your work now. You should reflect on how you went in this hour, and discuss with your tutor if you have concerns. You may choose to keep working, but the maximum mark for any questions you submit later will be 50%.
:
MIPS – Which char?
You have been given line_char.s, a MIPS assembler program that reads a line of input then reads an integer, n, then prints a ‘?’.
Add code to line_char.s to make it equivalent to this C program which reads a line and prints the character in position n:
$ 1521 spim -f line_char.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: Hello
Enter a position: 1
Character is: ?
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/07/questions
1/5

2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions
// read a line from stdin and and then an integer n
// Print the character in the nth-position
#include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
printf(“Enter a position: “);
int n;
scanf(“%d”, &n);
printf(“Character is: “);
printf(“%c”, line[n]);
printf(“%c”, ‘\n’);
return 0; }
In other words change the code you given from printing a ‘?’ to printing the first character of the line that has been read. For example:
$ 1521 spim -f line_char.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: abcdefghijklmnopqrstuvwxyz Enter a position: 0
Character is: a
$ 1521 spim -f line_char.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: abcdefghijklmnopqrstuvwxyz Enter a position: 13
Character is: n
$ 1521 spim -f line_char.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: Hello Andrew!
Enter a position: 12
Character is: !
$ 1521 spim -f line_char.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: i MIPS you
Enter a position: 3
Character is: I
HINT:
Only a few instructions are needed.
NOTE:
You can assume a line can be read.
You can assume the line will contain at most 256 characters. You can assume a positive integer n can be read.
You can assume the line read has at least n + 1 characters.
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
$ 1521 autotest line_char
$ give cs1521 test07_line_char line_char.s
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/07/questions
2/5

2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions
:
MIPS -How Long?
You have been given line_length.s, a MIPS assembler program that reads a line of input and then prints 42.
Add code to line_length.s to make it equivalent to this C program which reads a line and prints its length:
$ 1521 spim -f line_length.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: Hello
Line length: 42
// read a line and print its length #include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
int i = 0;
while (line[i] != 0) {
i++; }
printf(“Line length: “);
printf(“%d”, i);
printf(“%c”, ‘\n’);
return 0; }
In other words change the code you given from printing 42 to printing the line length For example:
$ 1521 spim -f line_length.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: Hello
Line length: 6
$ 1521 spim -f line_length.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: i MIPS you
Line length: 11
$ 1521 spim -f line_length.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: good bye
Line length: 9
NOTE:
As you can see in the above examples the characters read (and counted) include the ‘\n’ at the end of the line. You can assume a line containing at least 1 character is always successfully read.
No error handling is necessary.
When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give:
$ 1521 autotest line_length
$ give cs1521 test07_line_length line_length.s
:
MIPS – Palindromic?
https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/07/questions
3/5

2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions
You have been given palindrome.s, a MIPS assembler program that reads a line of input and then prints 2 lines.
$ 1521 spim -f palindrome.s
Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: Hello
not palindrome
palindrome
Add code to palindrome.s to make it equivalent to this C program which reads a line and prints whether it is a palindrome or not.
// read a line and print whether it is a palindrom #include
// line of input stored here
char line[256];
int main(void) {
printf(“Enter a line of input: “);
fgets(line, 256, stdin);
int i = 0;
while (line[i] != 0) {
i++; }
int j = 0;
int k = i – 2; while (j < k) { if (line[j] != line[k]) { printf("not palindrome\n"); return 0; } j++; k--; } printf("palindrome\n"); return 0; } A palindrome is a sequence which is the same forwards as backwards. For example: $ 1521 spim -f palindrome.s Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: kayak palindrome $ 1521 spim -f palindrome.s Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: canoe not palindrome $ 1521 spim -f palindrome.s Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: madamimadam palindrome $ 1521 spim -f palindrome.s Loaded: /home/cs1521/share/spim/exceptions.s Enter a line of input: abcdecba not palindrome NOTE: You can assume a line can be read. You can assume the line will contain at most 256 characters. When you think your program is working you can autotest to run some simple automated tests: When you are finished working on this exercise you must submit your work by running give: $ 1521 autotest palindrome $ give cs1521 test07_palindrome palindrome.s https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/07/questions 4/5 2020/7/24 COMP1521 20T2 ¡ª Week 07 Weekly Test Questions Submission When you are finished each exercise make sure you submit your work by running give. You can run give multiple times. Only your last submission will be marked. Don't submit any exercises you haven't attempted. If you are working at home, you may find it more convenient to upload your work via give's web interface. Remember you have until Saturday 25 July 21:00 to complete this test. Automarking will be run by the lecturer several days after the submission deadline for the test, using test cases that you haven't seen: different to the test cases autotest runs for you. (Hint: do your own testing as well as running autotest ) Test Marks After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface or by running this command on a CSE machine: The test exercises for each week are worth in total 1 marks. The best 6 of your 8 test marks for weeks 3-10 will be summed to give you a mark out of 9. COMP1521 20T2: Computer Systems Fundamentals is brought to you by the School of Computer Science and Engineering at the University of New South Wales, Sydney. For all enquiries, please email the class account at cs1521@cse.unsw.edu.au CRICOS Provider 00098G $ 1521 classrun -sturec https://cgi.cse.unsw.edu.au/~cs1521/20T2/test/07/questions 5/5