Week 03 Laboratory Exercises

Objectives

  • learning to run MIPS programs with mipsy
  • 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

Set up for the lab by creating a new directory called lab03 and changing to this directory.
mkdir lab03
cd lab03

There are some provided files for this lab which you can fetch with this command:

1092 fetch lab03

If you're not working at CSE, you can download the provided files as a zip file or a tar file.

Exercise — individual:
Do You MIPS me?

Write a MIPS assembler program bad_pun.s, which is equivalent to this C program:

// A simple C program that attempts to be punny

#include <stdio.h>

int main(void)
{
  printf("Well, this was a MIPStake!\n");

  return 0;
}

For example:

1092 mipsy bad_pun.s
Well, this was a MIPStake!

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest bad_pun 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab03_bad_pun bad_pun.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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:
Cheating in maths class

Write a MIPS assembler program gaussian_sum.s, which is equivalent to this C program:

// A simple C program that calculates the Gaussian sum between two numbers
// Written 12/2/2022
// by Dylan Brotherston (d.brotherston@unsw.edu.au)

#include <stdio.h>

int main(void)
{
  int number1, number2;

  printf("Enter first number: ");
  scanf("%d", &number1);

  printf("Enter second number: ");
  scanf("%d", &number2);

  int gaussian_sum = ((number2 - number1 + 1) * (number1 + number2)) / 2;

  printf("The sum of all numbers between %d and %d (inclusive) is: %d\n", number1, number2, gaussian_sum);

  return 0;
}

For example:

1092 mipsy gaussian_sum.s
Enter first number: 1
Enter second number: 100
The sum of all numbers between 1 and 100 (inclusive) is: 5050
1092 mipsy gaussian_sum.s
Enter first number: 1
Enter second number: 1000
The sum of all numbers between 1 and 1000 (inclusive) is: 500500
1092 mipsy gaussian_sum.s
Enter first number: 10
Enter second number: 13
The sum of all numbers between 10 and 13 (inclusive) is: 46

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest gaussian_sum 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab03_gaussian_sum gaussian_sum.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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 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 lab03_grade grade.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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 lab03_count count.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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 lab03_seven_eleven seven_eleven.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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 lab03_sequence sequence.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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 lab03_tetrahedral tetrahedral.s

You must run give before Tuesday 17 September 09:00 (2024-09-17 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.

Submission

When you are finished each exercises 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 Tuesday 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