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
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:
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 changeadd
' 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 Monday 01 January 00:00 (midnight) (Saturday 01 January 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 a File for non-ASCII Bytes
We need to check whether files contain non-ASCII bytes.
Write a C program, non_ascii.c
, which takes one argument, a filename.
It should print one line of output.
If the file contains a non-ASCII byte, non_ascii.c
should print the location of the
first non-ASCII byte. Use the same format as the example below.
If the file contains no non-ASCII byte non_ascii.c
should print a message indicating this.
Again use the same format as the example below.
Assume a byte is non-ASCII if it contains a value between 128..255 inclusive.
dcc non_ascii.c -o non_ascii echo hello world >file1 ./non_ascii file1 file1 is all ASCII echo -e 'hello\xBAworld' >file2 ./non_ascii file2 file2: byte 5 is non-ASCII ./non_ascii non_ascii.c non_ascii.c is all ASCII echo -e '\x80\x81' >file3 file2: byte 0 is non-ASCII
When you think your program is working,
you can use autotest
to run some simple automated tests:
1092 autotest non_ascii
When you are finished working on this exercise,
you must
submit your work by running give
:
give dp1092 lab12_non_ascii non_ascii.c
You must run give
before Monday 01 January 00:00 (midnight) (Saturday 01 January 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 Monday 01 January 00:00 (midnight) (Saturday 01 January 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 Monday 01 January 00:00 (midnight) (Saturday 01 January 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 Saturday 01 January 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