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 Tuesday 27 February 12:00 (midday) (2024-02-27 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 Tuesday 27 February 12:00 (midday) (2024-02-27 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 Tuesday 27 February 12:00 (midday) (2024-02-27 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 Tuesday 27 February 12:00 (midday) (2024-02-27 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 Tuesday 27 February 12:00 (midday) (2024-02-27 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:
Read & Execute MIPS Instructions
Write a MIPS assembler program dynamic_load.s
which reads MIPS instructions as signed decimal integers
until it reads the value -1,
then executes the instructions.
dynamic_load.s should read instructions until it reads the value -1.
dynamic_load.s should then print a message, load the instructions, print another message, exactly as in the examples below.
For example, below is a tiny MIPS assembler program which prints 42. The comment on each line shows how the instruction is encoded, as a hexadecimal and as a signed decimal integer; it is this signed integer value that your program will read.
li $a0, 42 # 3404002a 872677418 li $v0, 1 # 34020001 872546305 syscall # 0000000c 12 jr $ra # 03e00008 65011720
This is what dynamic_load.s
must do.
1521 mipsy dynamic_load.s Enter mips instructions as integers, -1 to finish: 872677418 872546305 12 65011720 -1 Starting executing instructions 42Finished executing instructions
The supplied files for the lab include files containing the instructions for some MIPS assembler programs from lectures. You can use these to test your program; for example:
cat add.instructions 872939537 873005081 19419168 663585 872546305 12 872677386 872546315 12 65011720 -1 1521 mipsy dynamic_load.s <add.instructions Enter mips instructions as integers, -1 to finish: Starting executing instructions 42 Finished executing instructions 1521 mipsy dynamic_load.s <print10.instructions Enter mips instructions as integers, -1 to finish: Starting executing instructions 1 2 3 4 5 6 7 8 9 10 Finished executing instructions 1521 mipsy dynamic_load.s <sum_100_squares.instructions Enter mips instructions as integers, -1 to finish: Starting executing instructions 338350 Finished executing instructions
If you want to experiment with your own tests, this command will give you any MIPS program as integers.
1521 mips_instructions 42.s 537133098 537001985 12 537133066 537001995 12 65011720 -1
If you want to try creating your own test cases, here is some MIPS assembler that prints a message without using initialized data:
# print a string without using pre-initialized data
# for the dynamic load challenge exercise
main:
li $a0, 'H' # printf("%c", 'Hi');
li $v0, 11
syscall
li $a0, 'i' # printf("%c", 'i');
li $v0, 11
syscall
li $a0, '\n' # printf("%c", '\n');
li $v0, 11
syscall
jr $ra
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest dynamic_load
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1521 lab02_dynamic_load dynamic_load.s
You must run give
before Tuesday 27 February 12:00 (midday) (2024-02-27 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 Tuesday 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