Week 12 Laboratory Exercises

Objectives

  • revision on MIPS, bit operations, environment variables and files

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 lab12 and changing to this directory.
mkdir lab12
cd lab12

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

1092 fetch lab12

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

Exercise — individual:
MIPS factorial

In the files for this lab, you have been given factorial.s. Add code to the factorial function to make it equivalent to this C program:

// Recursive factorial function
// n < 1 yields n! = 1

#include <stdio.h>

int factorial(int);

int main(void) {
    int n = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    int f = factorial(n);
    printf("%d! = %d\n", n, f);
    return 0;
}

int factorial(int n) {
    int result;
    if (n > 1) {
        result = n * factorial(n - 1);
    } else {
        result = 1;
    }
    return result;
}

For example:

1092 mipsy factorial.s
Enter n: 5
5! = 120
1092 mipsy factorial.s
Enter n: 7
7! = 5040
1092 mipsy factorial.s
Enter n: 1
1! = 1

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

1092 autotest factorial 

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

give dp1092 lab12_factorial factorial.s

You must run give before Tuesday 19 November 09:00 (2024-11-19 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:
Create An add Instruction

Your task is to add code to this function in add.c:

// return the MIPS opcode for add $d, $s, $t
uint32_t make_add(uint32_t d, uint32_t s, uint32_t t) {

    return 42; // REPLACE WITH YOUR CODE

}

The function make_add is given the operands for a MIPS add instruction . Add code so that it returns the opcode for that instruction. Reminder the bit pattern for MIPS add instruction is:

Assembler Description C Bit Pattern
add $d, $s, $t add d = s + t 000000ssssstttttddddd00000100000

This is how your code should behave

./add 17 19 3
make_add(17, 19, 3) returned  0x02638820
./add 9 27 12
make_add(9, 27, 12) returned 0x036c4820

Use make(1) to build your code:

make    # or 'make add'
Assumptions/Limitations/Clarifications
  • You may define and call your own functions if you wish.

  • You are not permitted to change the main function you have been given, or to change add' prototype (its return type and argument types).

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

1092 autotest add 

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

give dp1092 lab12_add add.c

You must run give before Tuesday 19 November 09:00 (2024-11-19 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:
Check if two specified bytes are the same

Write a C program, are_bytes_equal.c, which takes four (4) command-line argument: a pair of filenamess and positions:

  1. filename 1
  2. position 1
  3. filename 2
  4. position 2

Your program should compare the byte at first position in the first file with the byte at second position in the second file.

Your program should then print a message saying if the two bytes are or are not equal.

If either position is past the end of the corresponding file then the bytes are not equal

dcc are_bytes_equal.c -o are_bytes_equal
./are_bytes_equal data1.txt 0 data2.txt 0
byte 0 in data1.txt and byte 0 in data2.txt are the same
./are_bytes_equal data1.txt 0 data2.txt 33
byte 0 in data1.txt and byte 33 in data2.txt are not the same
./are_bytes_equal data1.txt 454 data2.txt 454
byte 454 in data1.txt and byte 454 in data2.txt are not the same
./are_bytes_equal data1.txt 454 data3.txt 454
byte 454 in data1.txt and byte 454 in data3.txt are the same

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

1092 autotest are_bytes_equal 

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

give dp1092 lab12_are_bytes_equal are_bytes_equal.c

You must run give before Tuesday 19 November 09:00 (2024-11-19 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:
Test if A Pathname is a Directory

Write a C program, is_directory.c, which takes one argument a pathname.

If the pathname exists and is a directory, it should print 1, otherwise it should print 0.

dcc is_directory.c -o is_directory
mkdir test_directory
echo >test_file
./is_directory test_directory
1
./is_directory test_file
0
./is_directory non_existant_path
0
./is_directory /home/cs1521/public_html
1
./is_directory /etc/resolv.conf
0

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

1092 autotest is_directory 

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

give dp1092 lab12_is_directory is_directory.c

You must run give before Tuesday 19 November 09:00 (2024-11-19 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:
Find peaks in an Array in MIPS

This is a past exam question

You have been given final_q4.s, which contains a MIPS function final_q4, that takes two arguments: an array and its length, and returns the value 42.

Add code to final_q4 so that it is equivalent to this C function:

int final_q4(int array[], int length) {
    int total = 0;
    int i = 1;

    while (i < length - 1) {
        if (array[i] > array[i - 1] && array[i] > array[i + 1]) {
            total++;
        }
        i++;
    }

    return total;
}

The function final_q4 returns the number of peaks in an array of integers

A peak is (in this case) a value that is both preceded and succeeded by a value smaller than itself

In other words, the value before and the value after the current value are smaller than the current value

For example:

1521 mipsy test_final_q4.s final_q4.s
1
3
2
5
4
4
9
0
1
-9
4
1521 mipsy test_final_q4.s final_q4.s
1
2
3
4
5
6
7
8
9
0
1
1521 mipsy test_final_q4.s final_q4.s
9
8
7
6
5
4
3
2
1
0
0

You can use make(1) to build the provided C code:

make final_q4

You can use 1521 mipsy to run your MIPS code:

1521 mipsy test_final_q4.s final_q4.s

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

1092 autotest 22t1final_q4 

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

give dp1092 lab12_22t1final_q4 final_q4.s

You must run give before Tuesday 19 November 09:00 (2024-11-19 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 13 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