COMP1521 – 21T3
Instructions
Question 0 (10 MARKS) Question 1 (10 MARKS) Question 2 (10 MARKS) Question 3 (10 MARKS) Question 4 (10 MARKS) Question 5 (10 MARKS) Question 6 (10 MARKS) Question 7 (10 MARKS) Question 8 (10 MARKS) Question 9 (10 MARKS) Submission
The University of Wales
Copyright By https://powcoder.com 加微信 powcoder
Term 2, 2021
COMP1521: Computer Systems Fundamentals
Final Exam
— Friday, 13 August 2021 —
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 at Monday 29 November 13:50, Sydney time.
You can start working on examination questions at Monday 29 November 14:00, Sydney time.
You must stop working on examination questions at Monday 29 November 17:00, Sydney time. Only submissions 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.
You must not communicate with anyone via any medium during the examination — this includes, but is not limited to, communications via email, telephone, instant-message, talking, hot-air balloon, … — except for COMP1521 staff, via
You must not get help from anyone during this exam, except for COMP1521 staff.
You must not use code-synthesis tools, such as GitHub Copilot, during this exam.
You must not communicate your exam answers to any other person, even after the end of the exam.
Some students have extended time to complete the exam.
You must ensure that, during and after the examination, no other person can access your work.
You must not place your examination work in a location accessible to any other person, whether they be a student in the course or otherwise. This includes file sharing services such as Dropbox or GitHub. Your zPass should not be disclosed to any other person. If you have disclosed your zPass, you should change it immediately.
This is an open-book examination: you may use your papers or books. You may not create or modify materials on the Internet, except as would be reasonably required to complete the exam.
Deliberate violation of exam conditions is academic misconduct,
and will be referred to the UNSW Student Conduct and Integrity Unit.
Troubleshooting
If you are having issues working on the exam at CSE, please try the following:
if you are using VLAB: try logging out and logging back in — you will very likely be connected to a different server, which may make your connection better. If the problem persists, try using SSH instead: instructions at
if you are using SSH: try logging out and logging back in. If the problem persists, try using VLAB instead: instructions at
if you are using VSCode: try disconnecting and reconnecting; or try changing servers from vscode.cse.unsw.edu.au to vscode2.cse.unsw.edu.au.
If you still have problems, contact COMP1521 staff via — we will try to help you fix the problem; and, if we can fix the problem, we may also be able to give you extra time.
If you have a problem that cannot be fixed and you cannot complete the exam, you should apply for special consideration. This will require evidence of a problem, and you should take screenshots documenting the problem. For example:
error messages,
screen not loading
timestamped speed tests,
power outage maps, or
messages or information from your internet provider regarding the issues experienced.
You must also contact course staff via as soon as it is clear the issue cannot be fixed.
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. If you have questions, or you feel unable to complete the exam, contact
Examination Structure
This examination has 10 questions, worth a total of 100 marks.
Questions are worth equal marks.
All 10 questions are practical questions.
You must answer each question in a separate file. Each question specifies the name of the file to use. These are named after the corresponding question number. 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 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 21t2final_q
Available Resources: Documentation
You may find this language documentation useful during this exam:
C quick reference
MIPS Quick Reference Card MIPS Instruction Reference SPIM Documentation
MIPS Quick Tutorial
You may also access:
manual entries via the man command, and Texinfo pages via the info command.
Getting Started
Set up for the exam by creating a new directory called exam_21t2final, changing to this directory, and fetching the provided code by running these commands:
mkdir -m 700 exam_21t2final
cd exam_21t2final
1521 fetch exam_21t2final
Or you can download the provided code as a zip file or a tar file.
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_21t2final
Only files that don’t exist will be recreated. All other files will remain untouched.
Question 0 (10 MARKS)
Count leading zeroes is an operation on a list of bits that counts how many zero-bits are present before the first one-bit, starting from the most-significant bit. For example, in the case of an 8-bit number,
count_leading_zeroes(0b10000000) == 0 count_leading_zeroes(0b01111111) == 1 count_leading_zeroes(0b00101010) == 2 count_leading_zeroes(0b00001110) == 4 count_leading_zeroes(0b00000001) == 7 count_leading_zeroes(0b00000000) == 8
You must implement this operation for 32-bit values. You have been given final_q0.c:
// COMP1521 21T2 … final exam, question 0
#include
#include
#include
count_leading_zeroes (uint32_t x)
return 42;
Add code to the function count_leading_zeroes so that it counts and returns the amount of leading zeroes of a 32-bit unsigned integer x.
For example:
33554432 == 0x02000000
./final_q0 33554432
When you think your program is working, you can run some simple automated tests:
1521 autotest 21t2final_q0
When you are finished working on this activity, you must submit your work by running give:
give cs1521 21t2final_q0 21t2final_q0.c
To verify your submissions for this activity:
1521 classrun -check 21t2final_q0
Question 1 (10 MARKS)
You have been given final_q1.c:
// COMP1521 21T2 … final exam, question 1
#include
#include
#include
#include
#define BITS 8
and (bool x[BITS], bool y[BITS], bool result[BITS])
or (bool x[BITS], bool y[BITS], bool result[BITS])
xor (bool x[BITS], bool y[BITS], bool result[BITS])
not (bool x[BITS], bool result[BITS])
Add code to the following functions:
and (bitwise AND)
or (bitwise OR)
xor (bitwise XOR)
not (bitwise NOT / bitwise negation)
such that they perform their respective bitwise operations on given arrays of 8 (i.e., BITS) booleans. A boolean with the value true represents a 1 bit, and a boolean with the value false represents a 0 bit. You must store the result of your bitwise operation in the provided result array in each function.
You must not modify the provided x and y boolean arrays. You may only modify the result array.
final_q1_main.c provides some code to let you test each of these functions.
For example:
./final_q1 -and 10110101 01101001
10110101 &
./final_q1 -or 10110101 01101001
10110101 |
./final_q1 -xor 10110101 01101001
10110101 ^
./final_q1 -not 10110101
When you think your program is working, you can run some simple automated tests:
1521 autotest 21t2final_q1
When you are finished working on this activity, you must submit your work by running give:
give cs1521 21t2final_q1 21t2final_q1.c
To verify your submissions for this activity:
1521 classrun -check 21t2final_q1
Question 2 (10 MARKS)
A parity check is a simple way to provide an integrity check for a value. Often, when we transmit data, we will also transmit its parity, which can allow a receiver to verify they have read the correct data. A number is defined to have even parity, if the amount of 1 bits in the number is even.
Conversely, a number is defined to have odd parity, if the amount of 1 bits in the number is odd.
For example, checking the parity of some 8-bit numbers:
0b00000000: zero 1 bits are set ⇒ even parity 0b00000001: one 1 bit is set ⇒ odd parity 0b00000010: one 1 bit is set ⇒ odd parity 0b00000011: two 1 bits are set ⇒ even parity
You have been given final_q2.c, which contains code that implements a parity checker: // COMP1521 21T2 … final exam, question 2
#include
#include
main (void)
You have also been given final_q2.s, a stub MIPS program that reads an integer and prints “the parity is even\n”: # COMP1521 21T2 … final exam, question 2
uint32_t n;
scanf (“%d”, &n);
int bit_idx = 0;
int n_bits_set = 0;
while (bit_idx != 32) {
int bit = (n >> bit_idx) & 1;
n_bits_set = n_bits_set + bit;
bit_idx++;
if (n_bits_set % 2 != 0) {
printf (“the parity is odd\n”);
printf (“the parity is even\n”);
even_parity_str:
odd_parity_str:
.asciiz “the parity is even\n”
.asciiz “the parity is odd\n”
# input is in $t0
li $v0, 4
la $a0, even_parity_str
li $v0, 0
jr $ra
Add code to final_q2.s such that it prints out whether a 32-bit number, read from standard input, has odd or even parity. If the parity is even, you should print “the parity is even\n”.
If the parity is odd, you should print “the parity is odd\n”.
For example:
1521 spim -file final_q2.s
the parity is even
1521 spim -file final_q2.s
the parity is odd
1521 spim -file final_q2.s
the parity is even
1521 spim -file final_q2.s
the parity is even
The correct strings to print are already provided for you in the .data segment
You do not have to perform any error checking.
When you think your program is working, you can run some simple automated tests:
1521 autotest 21t2final_q2
When you are finished working on this activity, you must submit your work by running give:
give cs1521 21t2final_q2 21t2final_q2.s
To verify your submissions for this activity:
1521 classrun -check 21t2final_q2
Question 3 (10 MARKS)
You have been given final_q3.c:
// COMP1521 21T2 … final exam, question 3
#include
#include
#include
cp (char *path_from, char *path_to)
Add code to the function cp so that it copies the content from the file located at path_from into a file located at path_to.
If there is not an existing file located at path_to, a new file should be created. If there is an existing file located at path_to, it should be overwritten. It should copy all bytes from the file. i.e. once cp has been executed, the files located at path_from and at path_to should be identical.
For example:
cat: c: No such file or directory
./final_q3 a b
./final_q3 a c
When you think your program is working, you can run some simple automated tests:
1521 autotest 21t2final_q3
When you are finished working on this activity, you must submit your work by running give:
give cs1521 21t2final_q3 21t2final_q3.c
To verify your submissions for this activity:
1521 classrun -check 21t2final_q3
Question 4 (10 MARKS)
Multiplication is often an “expensive” operation — it can take a substantial amount of time and energy to do, and not just in hardware — so we often prefer to reduce multiplication into bitwise operations, or into simple additions or subtractions, which are often far cheaper.
For example, let’s say we wish to multiply some value b by 7 without using *. Because 7 is 8 – 1, we can restate multiplying by seven as multiplying by eight, then subtracting the original multiplicand once: b × 7 = b × 8 − b = b × 23 − b. We now have multiplication by a power of two, which we can convert to a bitwise-left-shift; and subtraction is a permitted operation. In C, we would express this as (b << 3) - b.
You have been given final_q4.c:
// COMP1521 21T2 ... final exam, question 4
#include
mul (long a, long b_)
You do not have to perform any error checking.
You can assume there is a file located at path_from.
You can assume that you have permissions to read from the file located at path_from.
You can assume that you have permissions to write to a file located at path_to.
You may not call any external programs
You are not permitted to run any external programs.
You are not permitted to use system, popen, posix_spawn, fork, exec, or any of their equivalents.
abort(); }
Add code to the function mul so it performs a × b when a is one of 17, -3, 60, or -112.
As is worked above, the case where a is 7 has already been provided for you.
You may not use multiplication, *.
You may only use the operations + (two-operand addition), – (one-operand negation), – (two-operand subtraction), and << (two-operand bitwise-left-shift). Additionally, you may only use five of these operations to do each multiplication. Autotest will not warn you about this, but it will be checked in marking. You may not use floating-point operations to approximate an answer.
When you think your program is working, you can run some simple automated tests:
1521 autotest 21t2final_q4
When you are finished working on this activity, you must submit your work by running give:
give cs1521 21t2final_q4 21t2final_q4.c
To verify your submissions for this activity:
1521 classrun -check 21t2final_q4
Question 5 (10 MARKS)
You have been given final_q5.c:
// COMP1521 21T2 ... final exam, question 5
#include
#include
#include
print_utf8_count (FILE *file)
unsigned long b = b_;
// NOTE: Maximum 5 operations allowed // for each multiplication
// NOTE: Permitted operations:
// [x+y,x-y,-x,x<
#include
#include
convert_hexdump_to_bytes (FILE *hexdump_input, FILE *byte_output)
// Hint: `man 3 fscanf`
// Hint: `man 3 fseek`
// Hint: See question text for
// third hint.
Add code to the function convert_hexdump_to_bytes such that, given a hexdump of a file on hexdump_input, it writes the data the hexdump represents into the given byte_output file. You should make careful use of the address column to ensure correct ordering of the data!
fscanf: fscanf(hexdump_input, “%x”, …)
fseek … but you should not try to fseek on hexdump_input: as it is piped input, you will