0% found this document useful (0 votes)
2K views7 pages

COMP1521 22T1 - Week 03 Weekly Test Sample Answers

The document provides sample answers for a COMP1521 weekly test on MIPS assembly programming. It includes two questions - one asking to modify a MIPS program to print the minimum of two numbers, and another asking to modify a program to print numbers between two numbers except 13. Sample solutions in MIPS assembly are provided for both questions. Students are instructed to write their solutions, test them, and submit their work.

Uploaded by

Rudra Arjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views7 pages

COMP1521 22T1 - Week 03 Weekly Test Sample Answers

The document provides sample answers for a COMP1521 weekly test on MIPS assembly programming. It includes two questions - one asking to modify a MIPS program to print the minimum of two numbers, and another asking to modify a program to print numbers between two numbers except 13. Sample solutions in MIPS assembly are provided for both questions. Students are instructed to write their solutions, test them, and submit their work.

Uploaded by

Rudra Arjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

Week 03
Weekly Test
Sample Answers
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 Week 4 Thursday 20:59:59.
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 Week 4 Thursday 20:59:59
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
MIPS 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.
Getting Started
Set up for the test by
creating a new directory called test03 and changing to this directory.
$ mkdir test03

$ cd test03

There are some provided files for this test which you can fetch with this command:
$ 1521 fetch test03

If you're not working at CSE,


you can download the provided files
as a zip file
or a tar file.

weekly test question:

MIPS Minimum
In the files for this lab,
you have been given min.s,
a MIPS assembler program that
reads 2 numbers and then prints 42.
Add code to min.s
to make it equivalent to this C program:
// print the minimum of two integers

#include <stdio.h>

int main(void) {

int x, y;

scanf("%d", &x);

scanf("%d", &y);

if (x < y) {

printf("%d\n", x);

} else {

printf("%d\n", y);

return 0;

In other words it should read 2 numbers and print the smaller number.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 1/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

For example:
$ 1521 spim -f min.s

$ 1521 spim -f min.s

118

26

26

$ 1521 spim -f min.s

42

42

42

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

When you are finished working on this exercise you must submit
your work by running give:
$ give cs1521 test03_min min.s

SOLUTION:
Sample solution for min.s
# print the minimum of two integers

# x in $t0, y in $t1

main:

li $v0, 5 # scanf("%d", &x);

syscall #

move $t0, $v0

li $v0, 5 # scanf("%d", &y);

syscall #

move $t1, $v0

bge $t0, $t1, else # if (x < y) {

move $a0, $t0 # printf("%d\n", x);

li $v0, 1

syscall

li $a0, '\n' # printf("%c", '\n');

li $v0, 11

syscall

j end

else:

move $a0, $t1 # printf("%d\n", y);

li $v0, 1

syscall

li $a0, '\n' # printf("%c", '\n');

li $v0, 11

syscall

end:

li $v0, 0 # return 0

jr $ra

SOLUTION:
Alternative solution for min.s

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 2/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

# print the minimum of two integers - more concise solution

# x in $t0, y in $t1

main:

li $v0, 5 # scanf("%d", &x);

syscall #

move $a0, $v0

li $v0, 5 # scanf("%d", &y);

syscall #

bge $v0, $a0, end

move $a0, $v0

end:

li $v0, 1 # printf("%d\n", y);

syscall

li $a0, '\n' # printf("%c", '\n');

li $v0, 11

syscall

li $v0, 0 # return 0

jr $ra

weekly test question:

MIPS Counting (but not 13)


In the files for this lab,
you have been given not13.s,
a MIPS assembler program that
reads 2 numbers and then prints 42.
Add code to not13.s
to make it equivalent to this C program:
// print the integers between x and y except 13

#include <stdio.h>

int main(void) {

int x, y;

scanf("%d", &x);

scanf("%d", &y);

int i = x + 1;

while (i < y) {

if (i != 13) {

printf("%d\n", i);

i = i + 1;

return 0;

In other words it should read 2 numbers and print the numbers between them, except it doesn't print 13.
For example:

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 3/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

$ 1521 mipsy not13.s

$ 1521 mipsy not13.s

10

15

11

12

14

$ 1521 mipsy not13.s

14

10

11

12

Assumptions/Limitations/Clarifications
You can assume the first number read is not greater than the second number.
When you think your program is working you can autotest to run some simple automated tests:
$ 1521 autotest not13

When you are finished working on this exercise you must submit
your work by running give:
$ give cs1521 test03_not13 not13.s

SOLUTION:
Sample solution for not13.s
# print the integers between x and y except 13

# x in $t0, y in $t1, i in $t2

main:

li $v0, 5 # scanf("%d", &x);

syscall #

move $t0, $v0

li $v0, 5 # scanf("%d", &y);

syscall #

move $t1, $v0

addi $t2, $t0, 1 # i = x + 1;

loop:

bge $t2, $t1, end # while (i < y) {

beq $t2, 13, skip # if (i != 13) {

move $a0, $t2 # printf("%d\n", i);

li $v0, 1

syscall

li $a0, '\n' # printf("%c", '\n');

li $v0, 11

syscall

skip: # }

addi $t2, $t2, 1 # i = i + 1;

j loop # }

end:

li $v0, 0 # return 0

jr $ra

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 4/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

weekly test question:

MIPS Squares
In the files for this lab,
you have been given square.s,
a MIPS assembler program that reads a number and then prints 42.
Add code to square.s
to make it equivalent to this C program:
// print a square of asterisks

#include <stdio.h>

int main(void) {

int x;

scanf("%d", &x);

int i = 0;

while (i < x) {

int j = 0;

while (j < x) {

printf("*");

j = j + 1;

i = i + 1;

printf("\n");

return 0;

In other words it should read a numbers and print a square of asterisks of that size.
For example:
$ 1521 mipsy square.s

***

***

***

$ 1521 mipsy square.s

****

****

****

****

$ 1521 mipsy square.s

*******
*******
*******
*******
*******
*******
*******

Assumptions/Limitations/Clarifications
You can assume the number read is positive (> 0).
When you think your program is working you can autotest to run some simple automated tests:
$ 1521 autotest square

When you are finished working on this exercise you must submit
your work by running give:
$ give cs1521 test03_square square.s

SOLUTION:
Sample solution for square.s

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 5/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

# print a square of asterisks

# x in register $t0

# i in register $t1

# j in register $t2

main:

li $v0, 5 # scanf("%d", &x);

syscall #

move $t0, $v0

li $t1, 0 # i = 0

loop0:

bge $t1, $t0, end0 # while (i < x) {

li $t2, 0 # j = 0

loop1:

bge $t2, $t0, end1 # while (j < x) {

li $a0, '*' # printf("%c", '*');

li $v0, 11

syscall

addi $t2, $t2, 1 # j++

j loop1 # }

end1:

li $a0, '\n' # printf("%c", '\n');

li $v0, 11

syscall

addi $t1, $t1, 1 # i++

j loop0 # }

end0:

li $v0, 0 # return 0

jr $ra

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
Week 4 Thursday 20:59:59
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.

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 6/7
23/05/2022, 16:26 COMP1521 22T1 — Week 03 Weekly Test Sample Answers

COMP1521 22T1: 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
[email protected]
CRICOS Provider 00098G

https://cgi.cse.unsw.edu.au/~cs1521/22T1/test/03/answers 7/7

You might also like