Week 02 Laboratory Exercises
Objectives
- learning to run MIPS programs with mipsy and mipsy-web
- understanding MIPS I/O (syscalls)
- understanding MIPS control instructions (branch)
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
lab02
and changing to this directory.
mkdir lab02 cd lab02
There are some provided files for this lab which you can fetch with this command:
1521 fetch lab02
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
Exercise — individual:
MIPS Grading
In the files for this lab,
you have been given grade.s
,
a MIPS assembler program
which reads a number
and always prints FL:
1521 mipsy grade.s Enter a mark: 100 FL
Add code to grade.s
to make it equivalent to this C program:
// read a mark and print the corresponding UNSW grade
#include <stdio.h>
int main(void) {
int mark;
printf("Enter a mark: ");
scanf("%d", &mark);
if (mark < 50) {
printf("FL\n");
} else if (mark < 65) {
printf("PS\n");
} else if (mark < 75) {
printf("CR\n");
} else if (mark < 85) {
printf("DN\n");
} else {
printf("HD\n");
}
return 0;
}
For example:
1521 mipsy grade.s Enter a mark: 42 FL 1521 mipsy grade.s Enter a mark: 72 CR 1521 mipsy grade.s Enter a mark: 89 HD
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest grade
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_grade grade.s
You must run give
before Monday 29 September 12:00 (midday) (2025-09-29 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Exercise — individual:
MIPS Counting
In the files for this lab,
you have been given a MIPS assembler program count.s
,
which reads a number and prints 42:
1521 mipsy count.s Enter a number: 13 42
Add code to count.s
to make it equivalent to this C program:
// read a number n and print the integers 1..n one per line
#include <stdio.h>
int main(void) {
int number, i;
printf("Enter a number: ");
scanf("%d", &number);
i = 1;
while (i <= number) {
printf("%d\n", i);
i = i + 1;
}
return 0;
}
For example:
1521 mipsy count.s Enter a number: 4 1 2 3 4 1521 mipsy count.s Enter a number: 10 1 2 3 4 5 6 7 8 9 10
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest count
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_count count.s
You must run give
before Monday 29 September 12:00 (midday) (2025-09-29 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Exercise — individual:
MIPS 7-Eleven
In the files for this lab,
you have been given seven_eleven.s
,
a MIPS assembler program
which reads a number and prints 42:
1521 mipsy seven_eleven.s Enter a number: 13 42
Add code to seven_eleven.s
to make it equivalent to this C program:
// Read a number and print positive multiples of 7 or 11 < n
#include <stdio.h>
int main(void) {
int number, i;
printf("Enter a number: ");
scanf("%d", &number);
i = 1;
while (i < number) {
if (i % 7 == 0 || i % 11 == 0) {
printf("%d\n", i);
}
i = i + 1;
}
return 0;
}
For example:
1521 mipsy seven_eleven.s Enter a number: 15 7 11 14 1521 mipsy seven_eleven.s Enter a number: 42 7 11 14 21 22 28 33 35
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest seven_eleven
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_seven_eleven seven_eleven.s
You must run give
before Monday 29 September 12:00 (midday) (2025-09-29 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Exercise — individual:
MIPS Sequence
In the files for this lab,
you have been given a MIPS assembler program sequence.s
,
which reads a number and prints 42:
1521 mipsy sequence.s Enter the starting number: 13 42
Add code to sequence.s
to make it equivalent to this C program:
// Read three numbers `start`, `stop`, `step`
// Print the integers between `start` and `stop` moving in increments of size `step`
#include <stdio.h>
int main(void) {
int start, stop, step;
printf("Enter the starting number: ");
scanf("%d", &start);
printf("Enter the stopping number: ");
scanf("%d", &stop);
printf("Enter the step size: ");
scanf("%d", &step);
if (stop < start) {
if (step < 0) {
for (int i = start; i >= stop; i += step) {
printf("%d\n", i);
}
}
}
if (stop > start) {
if (step > 0) {
for (int i = start; i <= stop; i += step) {
printf("%d\n", i);
}
}
}
return 0;
}
For example:
1521 mipsy sequence.s Enter the starting number: 1 Enter the stopping number: 10 Enter the step size: 2 1 3 5 7 9 1521 mipsy sequence.s Enter the starting number: 1 Enter the stopping number: -10 Enter the step size: -2 1 -1 -3 -5 -7 -9
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest sequence
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_sequence sequence.s
You must run give
before Monday 29 September 12:00 (midday) (2025-09-29 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Challenge Exercise — individual:
MIPS Tetrahedra
In the files for this lab,
you have been given tetrahedral.s
,
a MIPS assembler program that
reads a number and prints 42:
1521 mipsy tetrahedral.s Enter a number: 42 42
Add code to tetrahedral.s
to make it equivalent to this C program:
// Read a number n and print the first n tetrahedral numbers
// https://en.wikipedia.org/wiki/Tetrahedral_number
#include <stdio.h>
int main(void) {
int i, j, n, total, how_many;
printf("Enter how many: ");
scanf("%d", &how_many);
n = 1;
while (n <= how_many) {
total = 0;
j = 1;
while (j <= n) {
i = 1;
while (i <= j) {
total = total + i;
i = i + 1;
}
j = j + 1;
}
printf("%d\n", total);
n = n + 1;
}
return 0;
}
For example:
1521 mipsy tetrahedral.s Enter how many: 5 1 4 10 20 35 1521 mipsy tetrahedral.s Enter how many: 12 1 4 10 20 35 56 84 120 165 220 286 364
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest tetrahedral
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_tetrahedral tetrahedral.s
You must run give
before Monday 29 September 12:00 (midday) (2025-09-29 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
Submission
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 3 Monday 12:00:00 (midday) to submit your work without receiving a late penalty.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest
runs for you.
(Hint: do your own testing as well as running autotest
.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1521 classrun -sturec