Week 03 Laboratory Exercises

Objectives

  • Learning to run MIPS programs
  • Understanding MIPS I/O (syscalls)
  • Use data to define string

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 Monday 21 September 09:00 (2026-09-21 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

You have been given mips_calculator.s, a MIPS program with a simple template for you to build on.

Add code to mips_calculator.s to make it equivalent to this C program:

#include <stdio.h>

int main(void) {
    int first, second;

    scanf("%d", &first);
    scanf("%d", &second);

    printf("Sum: %d\n", first + second);
    printf("Difference: %d\n", first - second);
    printf("Product: %d\n", first * second);

    return 0;
}

In other words, it should read two integers and print their sum, difference and product.

For example:

1092 mipsy -f mips_calculator.s
8 3
Sum: 11
Difference: 5
Product: 24

1092 mipsy -f mips_calculator.s
12 4
Sum: 16
Difference: 8
Product: 48

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

1092 autotest mips_calculator 

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

give dp1092 lab03_mips_calculator mips_calculator.s

You must run give before Monday 21 September 09:00 (2026-09-21 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 Rectangle

You have been given mips_rectangle.s, a simple mips template.

Add code to mips_rectangle.s to make it equivalent to this C program:

#include <stdio.h>

int main(void) {
    int length;
    int width;
    int area;
    int perimeter;

    scanf("%d", &length);
    scanf("%d", &width);

    area = length * width;
    perimeter = 2 * (length + width);

    printf("Area: %d\n", area);
    printf("Perimeter: %d\n", perimeter);

    return 0;
}

In other words, it should read the length and width of a rectangle and print its area and perimeter.

For example:

1092 mipsy  -f mips_rectangle.s
8
3
Area: 24
Perimeter: 22

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

1092 autotest mips_rectangle 

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

give dp1092 lab03_mips_rectangle mips_rectangle.s

You must run give before Monday 21 September 09:00 (2026-09-21 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 Monday 21 September 09:00 (2026-09-21 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 Time

You have been given mips_time.s, a MIPS assembler program that reads a number of seconds.

Add code to mips_time.s to make it equivalent to this C program:

#include <stdio.h>

int main(void) {
    int total_seconds;
    int hours;
    int minutes;
    int seconds;
    int remaining_seconds;

    scanf("%d", &total_seconds);

    hours = total_seconds / 3600;
    remaining_seconds = total_seconds % 3600;

    minutes = remaining_seconds / 60;
    seconds = remaining_seconds % 60;

    printf("Hours: %d\n", hours);
    printf("Minutes: %d\n", minutes);
    printf("Seconds: %d\n", seconds);

    return 0;
}

In other words, it should convert a total number of seconds into hours, minutes and seconds.

For example:

1092 mipsy  -f mips_time.s
3672
Hours: 1
Minutes: 1
Seconds: 12

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

1092 autotest mips_time 

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

give dp1092 lab03_mips_time mips_time.s

You must run give before Monday 21 September 09:00 (2026-09-21 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 Receipt

You have been given mips_receipt.s, a MIPS assembler program that reads the prices and quantities of two items.

Add code to mips_receipt.s to make it equivalent to this C program:

#include <stdio.h>

int main(void) {
    int price_1;
    int quantity_1;
    int price_2;
    int quantity_2;

    int item_1_cost;
    int item_2_cost;
    int subtotal;
    int tax;
    int total;

    scanf("%d", &price_1);
    scanf("%d", &quantity_1);
    scanf("%d", &price_2);
    scanf("%d", &quantity_2);

    item_1_cost = price_1 * quantity_1;
    item_2_cost = price_2 * quantity_2;

    subtotal = item_1_cost + item_2_cost;
    tax = subtotal / 10;
    total = subtotal + tax;

    printf("Item 1: %d\n", item_1_cost);
    printf("Item 2: %d\n", item_2_cost);
    printf("Subtotal: %d\n", subtotal);
    printf("Tax: %d\n", tax);
    printf("Total: %d\n", total);

    return 0;
}

In other words, it should calculate the cost of each item, the subtotal, the tax and the final total.

For example:

1092 mipsy  -f mips_receipt.s
12
3
5
4
Item 1: 36
Item 2: 20
Subtotal: 56
Tax: 5
Total: 61

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

1092 autotest mips_receipt 

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

give dp1092 lab03_mips_receipt mips_receipt.s

You must run give before Monday 21 September 09:00 (2026-09-21 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 Distance

You have been given mips_distance.s, a MIPS assembler program that reads the coordinates of two points.

Add code to mips_distance.s to make it equivalent to this C program:

#include <stdio.h>
//find the distance between 2 points
int main(void) {
    int x1;
    int y1;
    int x2;
    int y2;

    int dx;
    int dy;
    int squared_distance;

    scanf("%d", &x1);
    scanf("%d", &y1);
    scanf("%d", &x2);
    scanf("%d", &y2);

    dx = x2 - x1;
    dy = y2 - y1;

    squared_distance = dx * dx + dy * dy;

    printf("Squared distance: %d\n", squared_distance);

    return 0;
}

In other words, it should calculate the squared distance between two points.

For example:

1092 mipsy -f mips_distance.s
2
3
7
6
Squared distance: 34

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

1092 autotest mips_distance 

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

give dp1092 lab03_mips_distance mips_distance.s

You must run give before Monday 21 September 09:00 (2026-09-21 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

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 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