The University of New South Wales
Term 3, 2024
COMP1521: Computer Systems Fundamentals

20T2 Final Exam (practice)

— Wednesday 27 November 2024 —
10 questions — 100 marks
10 minutes reading; 3 hours working

Examination Information

Examination Instructions and Conditions

  • You can start reading the text of this examination when instructed to do so by your invigilator.

  • You can start working on examination questions when instructed to do so by your invigilator.

  • You must stop working on examination questions immediately when instructed to do so by your invigilator.

  • Only submissions made before this time will be marked.

    For students with approved examination extensions from UNSW Equitable Learning Services,
    You should continue working until your extended working time expires.

    Your invigilator will tell you when this time expires.

  • You must not communicate with any person during the examination
    except for COMP1521 Course Staff and Exam Invigilators.

  • You are not permitted to talk, email, telephone, message , etc. all
    except to COMP1521 Course Staff and Exam Invigilators.

  • You must not get help from anyone during this exam,
    except from COMP1521 Course Staff and Exam Invigilators.

  • You must not communicate (email, message, post, ...) your exam answers to anyone, even after the exam has finished.

    There are two sessions, so other students may still be taking the exam after you have finished.

    Additionally Some students have extended time to complete the exam.

    And some students may be taking the exam on a different day.

  • Communicating your answers to other students, even after the exam, may be academic misconduct.

  • You must ensure that, during and after the examination, no other person can access your work.

  • You must not use code-synthesis tools, such as GitHub Copilot, during this exam.

  • Your zPass should not be disclosed to any other person. If you have disclosed your zPass, you should change it immediately.

  • This is a closed-book examination.

  • You are not permitted to access papers, books, or any other written materials.

  • You are not permitted to access files on your computer or other computers, except the files provided by the exam.

  • You are not permitted to access web pages or other Internet resources, except the web pages provided by the exam, and the online language documentation linked below.

Deliberate violation of exam conditions is academic misconduct,
and will be referred to the UNSW Student Conduct and Integrity Unit.

Examination Structure

  • This examination has 9 questions,
    worth a total of 90 marks.

    This exam originally had 11 questions, but two questions have been removed from this past paper.
    Questions are worth equal marks.

  • All 9 questions are practical questions.

  • Not all questions may provide files. You should create any files needed for submission if they are not provided.

  • You must answer each question in a separate file. Each question specifies the name of the file to use. Make sure you use exactly this file name.

  • When you finish working on a question, you should submit your files using the give command specified in the question. You should not wait until the submission deadline to submit your answers. Running autotests does not automatically submit your code.

  • You do not receive additional time for submitting your answers.

    You must submit all questions before the end of the 3 hour exam period.

    Failing to logout immediately at the end of the exam period is academic misconduct.

  • You may submit as many times as you like; only the last submission will be marked.

  • You can verify what submissions you have made with 1521 classrun -check

Available Resources: Language Documentation

You may access this language documentation while attempting this test:

Troubleshooting

If you are having issues working on the exam:

  • Immediately inform your invigilator.

Fit-to-Sit

This exam is covered by UNSW's Fit-to-Sit policy. That means that, by sitting this exam, you are declaring yourself well enough to do so. You will be unable to apply for special consideration after the exam for circumstances affecting you before it began.

Getting Started

All provided files for this exam are located in your home directory.

simply open a terminal or text editor in your home directory and you will be able to access all the files.

If you make a mistake and need a new copy of a particular file, you can do the following:

rm broken-file
1521 fetch exam_20t2final

Only files that don't exist will be recreated. All other files will remain untouched.

Question 1 (10 marks)

You have been given practice_q1.s, a MIPS assembler program that reads one number and then prints it.

Add code to practice_q1.s to make it equivalent to this C program:

// print the sum of two integers

#include <stdio.h>

int main(void) {
    int x, y;

    scanf("%d", &x);
    scanf("%d", &y);
    printf("%d\n", x + y);

    return 0;
}

In other words, it should read 2 numbers and print their sum.

For example:

1521 mipsy practice_q1.s
5
8
13
1521 mipsy practice_q1.s
118
26
144
1521 mipsy practice_q1.s
42
42
84

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

1521 autotest 20t2final_q1

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q1 practice_q1.s

To verify your submissions for this activity:

1521 classrun -check 20t2final_q1

Question 2 (10 marks)

Your task is to add code to this function in practice_q2.c:

// given a uint32_t value,
// return 1 iff the least significant (bottom) byte
// is equal to the 2nd least significant byte; and
// return 0 otherwise
int practice_q2(uint32_t value) {
    // PUT YOUR CODE HERE

    return 42;
}

Add code to the function practice_q2 so that, given a uint32_t value, it returns 1 iff the least significant (bottom) byte of value is equal to the second least significant byte. practice_q2 should return 0 otherwise.

For example, given the hexadecimal value 0x12345678, practice_q2 should return 0, because the least significant byte, 0x78, is not equal to the second least significant byte, 0x56.

Similarly, given the hexadecimal value 0x12345656, practice_q2 should return 1, because the least significant byte, 0x56, is equal to the second least significant byte, 0x56.

You must use bitwise operators to implement practice_q2.

For example:

dcc practice_q2.c test_practice_q2.c -o practice_q2
./practice_q2 0x00000000
practice_q2(0x00000000) returned 1
./practice_q2 0x00000001
practice_q2(0x00000001) returned 0
./practice_q2 0x00000100
practice_q2(0x00000100) returned 0
./practice_q2 0x00000101
practice_q2(0x00000101) returned 1
./practice_q2 0x00001212
practice_q2(0x00001212) returned 1
./practice_q2 0x00001213
practice_q2(0x00001213) returned 0
./practice_q2 0x12345678
practice_q2(0x12345678) returned 0
./practice_q2 0x12345656
practice_q2(0x12345656) returned 1
./practice_q2 0x12345634
practice_q2(0x12345634) returned 0
./practice_q2 0x12345666
practice_q2(0x12345666) returned 0
./practice_q2 0x12121212
practice_q2(0x12121212) returned 1
./practice_q2 0x37861212
practice_q2(0x37861212) returned 1
./practice_q2 0x12121221
practice_q2(0x12121221) returned 0

You can also use make(1) to build your code:

make practice_q2

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

1521 autotest 20t2final_q2

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q2 practice_q2.c

To verify your submissions for this activity:

1521 classrun -check 20t2final_q2

Question 3 (10 marks)

You have been given practice_q3.s, a MIPS assembler program that reads an integer value and then prints it.

Add code to the file practice_q3.s so that it prints 1 iff the least significant (bottom) byte of value is equal to the second least significant byte, and prints 0 otherwise.

For example, given the decimal value 305419896, which is hexadecimal 0x12345678, practice_q3.s should print 0, because the least significant byte, 0x78, is not equal to the second least significant byte, 0x56.

Similarly, given the decimal value 305419862, which is hexadecimal 0x12345656, practice_q3.s should print 1, because the least significant byte, 0x56, is equal to the second least significant byte, 0x56.

For example:

1521 mipsy practice_q3.s 
0
1
1521 mipsy practice_q3.s 
1
0
1521 mipsy practice_q3.s 
256
0
1521 mipsy practice_q3.s 
257
1
1521 mipsy practice_q3.s 
4626
1
1521 mipsy practice_q3.s 
4627
0
1521 mipsy practice_q3.s 
305419896
0
1521 mipsy practice_q3.s 
305419862
1

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

1521 autotest 20t2final_q3

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q3 practice_q3.s

To verify your submissions for this activity:

1521 classrun -check 20t2final_q3

Question 4 (10 marks)

You have been given practice_q4.s, a MIPS assembler program that reads 1 number and then prints it.

Add code to practice_q4.s to make it equivalent to this C program:

//  read numbers until their sum is >= 42, print their sum

#include <stdio.h>

int main(void) {
    int sum = 0;
    while (sum < 42) {
        int x;
        scanf("%d", &x);
        sum = sum + x;
    }
    printf("%d\n", sum);
    return 0;
}

In other words, it should read numbers until their sum is ≥ 42 and then print their sum.

For example:

1521 mipsy practice_q4.s
10
20
25
55
1521 mipsy practice_q4.s
20
22
42
1521 mipsy practice_q4.s
100
100
1521 mipsy practice_q4.s
10
10
10
10
10
50

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

1521 autotest 20t2final_q4

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q4 practice_q4.s

To verify your submissions for this activity:

1521 classrun -check 20t2final_q4

Question 5 (10 marks)

Write a C program, practice_q5.c, which takes two names of environment variables as arguments. It should print 1 iff both environment variables are set to the same value. Otherwise, if the environment variables differ in value, or either environment variable is not set, it should print 0.

The shell command export sets an environment variable to a value; the shell command unset unsets an environment variable. In the following example, export is used to set the environment variables VAR1, VAR2 and VAR3, and unset is used to ensure environment variables VAR4 and VAR5 are unset.

export VAR1=hello
export VAR2=good-bye
export VAR3=hello
unset VAR4
unset VAR5
./practice_q5 VAR1 VAR2
0
./practice_q5 VAR3 VAR1
1
./practice_q5 VAR2 VAR4
0
./practice_q5 VAR4 VAR5
0

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

1521 autotest 20t2final_q5

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q5 practice_q5.c

To verify your submissions for this activity:

1521 classrun -check 20t2final_q5

Question 6 (10 marks)

We need to count the number of ASCII bytes in files.

Write a C program, practice_q6.c, which takes a single filename as its argument, counts the number of bytes in the named file which are valid ASCII, and prints one line of output containing that count.

Assume a byte is valid ASCII iff it contains a value between 0 and 127 inclusive.

- You must match the output format in the example below exactly.

dcc practice_q6.c -o practice_q6
echo hello world >file1
./practice_q6 file1
file1 contains 12 ASCII bytes
 echo -e 'hello\xBAworld' >file2
./practice_q6 file2
file2 contains 11 ASCII bytes
echo -n -e '\x80\x81' >file3
./practice_q6 file3
file3 contains 0 ASCII bytes

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

1521 autotest 20t2final_q6

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q6 practice_q6.c

To verify your submissions for this activity:

1521 classrun -check 20t2final_q6

Question 7 (10 marks)

You have been given practice_q7.s, a MIPS assembler program that reads 1 number and then prints it.

Add code to practice_q7.s to make it equivalent to this C program:

// Read numbers into an array until their sum is >= 42
// then print the numbers in reverse order

#include <stdio.h>

int numbers[1000];

int main(void) {
    int i = 0;
    int sum = 0;
    while (sum < 42) {
        int x;
        scanf("%d", &x);
        numbers[i] = x;
        i++;
        sum += x;
    }

    while (i > 0) {
        i--;
        printf("%d\n", numbers[i]);
    }
}

In other words, it should read numbers until their sum is ≥ 42, and then print the numbers read in reverse order.

For example:

1521 mipsy practice_q7.s
11
17
3
19
19
3
17
11
1521 mipsy practice_q7.s
10
20
30
30
20
10
1521 mipsy practice_q7.s
42
42
  • No error checking is required.

  • Your program can assume its input contains only integers.

  • Your program can assume these integers sum to ≥ 42.

  • Your program can assume that it will have to read no more than 1000 integers before their sum is ≥ 42.

  • You can assume the value of the expression can be represented as a signed 32 bit value. In other words, you can assume overflow/underflow does not occur.

  • Your solution must be in MIPS assembler only.

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

1521 autotest 20t2final_q7

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q7 practice_q7.s

To verify your submissions for this activity:

1521 classrun -check 20t2final_q7

Question 8 (10 marks)

We need to count the number of UTF-8 characters in a file, and check that the file contains only valid UTF-8.

Write a C program, practice_q8.c, which takes a single filename as its argument, counts the number of UTF-8 characters in the named file, and prints one line of output containing that count.

Use the same format as the example below.

dcc practice_q8.c -o practice_q8
echo hello world >file1
./practice_q8 file1
file1: 12 UTF-8 characters
echo -e -n '\xF0\x90\x8D\x88' >file2
./practice_q8 file2
file2: 1 UTF-8 characters
echo -e -n '\x24\xC2\xA2\xE0\xA4\xB9' >file3
./practice_q8 file3
file3: 3 UTF-8 characters
./practice_q8 utf8.html
utf8.html: 7943 UTF-8 characters

If practice_q8.c reads a byte which is not valid UTF-8, it should stop and print an error message in the same format as the example below. Note the error message includes how many valid UTF-8 characters have been previously read.

echo -e -n '\x24\xC2\xA2\xE0\xA4\x09' >file4
./practice_q8 file4
file4: invalid UTF-8 after 2 valid UTF-8 characters

If the end of file is reached before the completion of a UTF-8 character, practice_q8.c should also print an error message; for example:

echo -e -n '\x24\xC2\xA2\xE0\xA4' >file5
./practice_q8 file5
file5: invalid UTF-8 after 2 valid UTF-8 characters

A reminder of how UTF-8 is encoded:

#bytes #bits Byte 1 Byte 2 Byte 3 Byte 4
1 7 0xxxxxxx - - -
2 11 110xxxxx 10xxxxxx - -
3 16 1110xxxx 10xxxxxx 10xxxxxx -
4 21 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

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

1521 autotest 20t2final_q8

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q8 practice_q8.c

To verify your submissions for this activity:

1521 classrun -check 20t2final_q8

Question 10 (10 marks)

We need a program which will save an entire directory tree as a single file. The directory tree may consist of many directories and files.

Write a C program, practice_q10, which is given either 1 or 2 arguments.

If practice_q10 is given 2 arguments, the first argument will be the pathname of a file it should create and the second argument will be the pathname of a directory. practice_q10 should then save the entire contents of the specified directory tree in the specified file.

If practice_q10 is given 1 argument, that argument will be the pathname of a file in which a directory tree has been saved. practice_q10 should re-create all the directories and files in the directory tree.

For example, these commands create a directory tree named a:

mkdir -p a/b/c
echo hello andrew >a/file1
echo bye andrew >a/b/file2
echo 1 >a/b/c/one
echo 2 >a/b/c/two
ls -lR a
a:
total 8
drwxr-xr-x 3 z1234567 z1234567 4096 Aug 19 20:38 b
-rw-r--r-- 1 z1234567 z1234567   13 Aug 19 20:38 file1

a/b:
total 8
drwxr-xr-x 2 z1234567 z1234567 4096 Aug 19 20:38 c
-rw-r--r-- 1 z1234567 z1234567   11 Aug 19 20:38 file2

a/b/c:
total 8
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 one
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 two
cat a/file1
hello andrew
cat a/b/file2
bye andrew

In this example, practice_q10 saves the contents of the directory tree a into a file named data:

dcc practice_q10.c -o practice_q10
./practice_q10 data a
ls -l data
-rw-r--r-- 1 z1234567 z1234567 4567 Aug 19 20:38 data

This example shows practice_q10 restoring the contents of the directory tree a after it has been removed:

rm -rf a
ls -lR a
ls: cannot access 'a': No such file or directory
cat a/file1
cat: a/file1: No such file or directory
./practice_q10 data
ls -lR a
a:
total 8
drwxr-xr-x 3 z1234567 z1234567 4096 Aug 19 20:38 b
-rw-r--r-- 1 z1234567 z1234567   13 Aug 19 20:38 file1

a/b:
total 8
drwxr-xr-x 2 z1234567 z1234567 4096 Aug 19 20:38 c
-rw-r--r-- 1 z1234567 z1234567   11 Aug 19 20:38 file2

a/b/c:
total 8
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 one
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 two
cat a/file1
hello andrew
cat a/b/file2
bye andrew

This example shows practice_q10 restoring the contents of the directory tree a in a different directory:

mkdir new_directory
cd new_directory
../practice_q10 ../data
cat a/file1
hello andrew
cat a/b/file2
bye andrew

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

1521 autotest 20t2final_q10

When you are finished working on this activity, you must submit your work by running give:

give cs1521 20t2final_q10 practice_q10.c

To verify your submissions for this activity:

1521 classrun -check 20t2final_q10

Submission

When you are finished working on a question, submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any questions you haven't attempted.

Do not leave it to the deadline to submit your answers.

Submit each question when you finish working on it.

Running autotests does not automatically submit your code.

— End of examination. —