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 access this language documentation while attempting this test:

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:

mkdir test07
cd test07
1521 fetch test07

Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.

weekly test question:
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 '?'.
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: ?
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:
//  read a line from stdin and and then an integer n
//  Print the character in the nth-position

#include <stdio.h>

// 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

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest line_char
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test07_line_char line_char.s

weekly test question:
MIPS -How Long?

You have been given line_length.s, a MIPS assembler program that reads a line of input and then prints 42.
1521 spim -f line_length.s
Loaded: /home/cs1521/share/spim/exceptions.s
Enter a line of input: Hello
Line length: 42
Add code to line_length.s to make it equivalent to this C program which reads a line and prints its length:
// read a line and print its length
#include <stdio.h>

// 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

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest line_length
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test07_line_length line_length.s

weekly test question:
MIPS - Palindromic?

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 <stdio.h>

// 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

When you think your program is working you can autotest to run some simple automated tests:

1521 autotest palindrome
When you are finished working on this exercise you must submit your work by running give:
give cs1521 test07_palindrome palindrome.s

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 Wednesday 01 January 00: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:

1521 classrun -sturec

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.