Week 04 Laboratory Exercises
Objectives
- Using if, while and for loop translation from C to mips
- Branching and control in mips
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
lab04 and changing to this directory.
mkdir lab04 cd lab04
There are some provided files for this lab which you can fetch with this command:
1092 fetch lab04
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:
1092 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:
1092 mipsy grade.s Enter a mark: 42 FL 1092 mipsy grade.s Enter a mark: 72 CR 1092 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:
1092 autotest grade
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_grade grade.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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:
1092 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:
1092 mipsy count.s Enter a number: 4 1 2 3 4 1092 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:
1092 autotest count
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_count count.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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:
1092 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:
1092 mipsy seven_eleven.s Enter a number: 15 7 11 14 1092 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:
1092 autotest seven_eleven
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_seven_eleven seven_eleven.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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:
1092 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 bwtween `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:
1092 mipsy sequence.s Enter the starting number: 1 Enter the stopping number: 10 Enter the step size: 2 1 3 5 7 9 1092 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:
1092 autotest sequence
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_sequence sequence.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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 Calculator (if)
You have been given mips_calculator_if.s,
a MIPS assembler program that reads two integers and an operation number.
Add code to mips_calculator_if.s to make it equivalent to this C program:
#include <stdio.h>
int main(void) {
int first;
int second;
int operation;
int result;
scanf("%d", &first);
scanf("%d", &second);
scanf("%d", &operation);
if (operation == 1) {
result = first + second;
printf("Result: %d\n", result);
} else if (operation == 2) {
result = first - second;
printf("Result: %d\n", result);
} else if (operation == 3) {
result = first * second;
printf("Result: %d\n", result);
} else if (operation == 4) {
if (second == 0) {
printf("Cannot divide by zero\n");
} else {
result = first / second;
printf("Result: %d\n", result);
}
}
return 0;
}
The operation is represented by a number:
1 = addition 2 = subtraction 3 = multiplication 4 = integer division
For example:
1092 mipsy -f mips_calculator_if.s 8 3 1 Result: 11 1092 mipsy -f mips_calculator_if.s 8 3 4 Result: 2
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest mips_calculator_if
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_mips_calculator_if mips_calculator_if.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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:
1092 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:
1092 mipsy tetrahedral.s Enter how many: 5 1 4 10 20 35 1092 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:
1092 autotest tetrahedral
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1092 lab04_tetrahedral tetrahedral.s
You must run give
before Monday 28 September 09:00 (2026-09-28 09: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.
In-Class Exercise — group:
Presentation
This week during lab class, you will be divided into small groups and assigned an activity from this week to give a short presentation on (2–5 minutes per group).
There is no give or autotest for the group presentation.
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 5 Monday 09:00:00 to submit your work.
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:
1092 classrun -sturec