Assignment
Details
-
sortArray1
Write
a MIPS assembly language program that can read a file called
“ints.dat” which contains an unspecified but no more than
200 binary integers and subequently write a filed called
“ints_62.dat” which contains the exact same data. Hardcode
the file names into your program by declaring them as null-terminated
ASCII strings. Note: You need to specify the full path for both
files. The TAs will change these when grading since they can’t run
them as you.
Use
MARS syscalls 13-15 to open, read/write, and close the files. Declare
a 200-word array called BUF that you use to hold the data. Write your
program as one long sequence of instructions. Consider logically
dividing it into segments, e.g., one for opening the input file, one
for reading the input file, etc. You will need to define labels for
branch instructions. Consider calling them CXY where X is the segment
and Y is the label index, e.g., C00, C01, etc.
Check
for syscall error conditions. That is, if file open fails and a file
descriptor of -1 is returned, e.g., if the file doesn’t exist, print
the message “Error: input file open error” for the input
file and “Error: output file open error” for the output
file; if trying to read an empty file, e.g., ints_0.dat, print the
message “Error: input file read error”; and if not writing
as many bytes as expected, print the message “Error: output file
write error” (this is difficult to test as it hopefully won’t
occur).
Add
meaningful comments that describe the overall computation as well as
comments that document each (most of them anyway) assembly language
instruction. Include a listing of which $s and $t-registers are used
for what subcomputation.
-
sortArray2
Add
code for printing out a message that says “Processed X ints”
when the input file contains a single-digit number of integers,
“Processed XY ints” when the input file contains a
double-digit number of integers, and “Processed XYZ ints”
when the input file contains a triple-digit number of integers, e.g.,
“Processed 7 ints” for ints_7.dat, “Processed 33 ints”
for ints_33.dat, and “Processed 195 ints” for ints_195.dat
Specifically, declare the null-terminated ASCII string “Processed
ints” and have code update the space field in accordance with
the number of integers read from the input file. Use integer division
to determine quotient and remainder numbers that you add to ‘0’ which
is then injected into the mentioned ASCII string. The is an exercise
in binary-to-decimal number conversion as well as in-place ASCII
string manipulation, and register plus offset based addressing. Do
NOT implement this by printing the first part of the text string
following by an integer followed by the end of the text string.
Implement it by modifying the text string in place and then printing
it in its entirety.
-
sortArray3
Add
sorting in between the file I/O read and write operations.
Specifically, implement the following so-called selection sort
algorithm (which is different from the bubble sort algorithm you read
about in the textbook). Assume N integers are stored in array called
data, then selection sort works as follows:
int
j = N-1; while (0 As you will learn in CS302, this is neither a good
nor a popular sorting algorithm, but it’s easy to understand and
implement, so we use it for now. Test the algorithm by making sure
the data in the output file appears in ascending order (from smallest
to largest value). How do you inspect a binary file? Fear not,
UNIX>
cp ints_7.dat ints.data UNIX> od -D ints.dat 0000000 949 6898 9925
918 0000020 7861 6442 6637 UNIX> execute sortarray3 (using MARS)
UNIX> od -D ints_62.dat 0000000 918 949 6442 6637 0000020 6898
7861 9925
Also,
the swap by copying the compared data elements back to the other
index slot in the array. That is, you don’t need a swap function per
se. Instead, extract data[j] into say register $rj and data[k] into
say register $rk, then write back $rk to data[j] and $rj to data[k].
This will make more sense once you start writing the program.
-
sortArray4
Saving
best for last, refactor your code to use function calls. In
particular, you need to implement a main function that calls four
functions, namely, fread(), sort(), fwrite(), and prstxt(). You must
also implement support function prmsg() that prints a given text
string to stdout. Consider the following C-style declarations of
these functions:
int
N = fread(char *fname, int *buf, int Nmax); void sort(int *but, int
N); int M = fwrite(char *fname, int *buf, int N); void prstxt(char
*status_text, int N); void prmsg(char *text);
The
fread() function opens the named file, prints the above mentioned
error message if the operation fails and returns -1. Conversely,
succesful opening leads to the file being read and the Nmax or fewer
ints being stored in the given array with the number of ints read
returned. The main function checks the returned integer, exits if it
equals 0 or less and continues otherwise.
The
sort function carries out the selection sort.
The
fwrite() function opens the named file, prints the above mentioned
error message if the operation fails and returns -1. Conversely,
succesful opening leads to the buf-data being written to file. If
fewer than the expected number of ints are written, the above
mentioned error message is printed and the function returns a -1;
otherwise the number of ints written is returned and checked by the
main function. If it equals 0 or less, the program exits. Otherwise,
it continues.
The
prstxt() function prints the status message which says how many ints
were processed. The prmsg() function must be used to print ALL text
strings.
Each
called function must save the return address on the call stack as
well as any $s-registers it uses. When done, these registers must all
be restored after the return value has been copied to register $v0 as
appropriate (not needed for void functions).
The
fread() function must store the file descriptor returned from the
open syscall (fid) in $s0, the pointer to the data buffer (BUF) in
$s1, and the max number of ints to be read (Nmax) in $s2. The
fwrite() function must store the file descriptor returned from the
open syscall (fid) in $s0, the pointer to the data buffer (BUF) in
$s1, and the number of ints to be written (N) in $s2. The sort(),
prstxt() and prmsg() functions may but need not use $s-registers.
-
General
requirements: Use $s-registers for variables that should be
preserved during the computation. Use $t-registers for storing
temporary variables. Add meaningful comments. These could be plain
text or psedo-C like. Document which registers are used and for
what.
The post MIPS assembly language program appeared first on My Assignment Online.