CSCI 210: Lab 4
MIPS
arrays
Due:
9:59
PM
on
Monday,
March 13
Write a program to read a list of (x,y) pairs representing points in the x-y plane and print them in ascending order, according to their distance from the origin.
Preliminaries
You will write this program using the MARS MIPS simulator, which you can download here.
Check out the lab template from github here.
Program Specification
No starter code is provided. Create a file in MARS called lab4.asm and write your program there. You may want to use your code from Lab 3 for reference.
Input:
The first line of input is an integer n (1 <= n <= 1000) representing the number of points in the list. n is followed by 2n additional lines of input containing the coordinates of the points to be sorted. For example, the list { (3,4), (4,2), (0,-5), (1,3) } would be input as:
4
3
4
4
2
0
-5
1
3
You can read the values using syscall 5 (read integer). (syscall 5 requires that only one integer appear on each line.)
The input should be stored in an array. You can view the storage conceptually as a series of pairs, as shown below:
3 4
4 2
0 -5
1 3
Output
The output of the program will consist of n lines, each containing the x- and y-coordinates of one point in the sorted list. For example, the output from the list of 4 points shown above would be:
1 3
4 2
0 -5
3 4
Implementation
You will need to:
- Dynamically allocate the space for the data using sbrk.
- Decide how to map the data to the space you allocate.
- Determine how to address the x- and y-coordinates of each point.
Once you have read in all the data, you need to sort it in increasing order according to the value of (x2 + y2); that is, the square of the distance from the point to the origin. If two points in your list are the same distance from the origin, they should be sorted in lexicographic order; that is, first in increasing order according to the x-coordinate, and if the x-coordinates are the same, then according to the y-coordinate. If the same point appears more than once in the input, it should appear with the same multiplicity in the output. For example, the correct ordering of the points (0,-5), (3,4), (4,-3), (-3,4), (-5,0) would be (-5,0), (-3,4), (0,-5), (3,4), (4,-3).
You may use any sort method that you choose. I would recommend using something simple like insertion sort. Keep in mind that when you swap two points in your list, you must swap both their x and y coordinates.
You may assume that overflow will not occur as a result of computing x2 + y2.
For full points, you must structure your solution to use functions. See the next section for a suggested list of functions. Functions must be proper functions with arguments passed in the argument registers, results returned in the return registers, caller-saved registers that are used must be saved before use and restored before returning.
Suggested plan for developing the program:
First, write and debug a program that will read the list of points, store it in an array, and print it in the desired format. Then go back and add the sort operation. Make sure to test!
My solution consists of:
- A
main
function that reads the number of points from the user, allocates enough space to hold the points (each point is two integers) and then calls three functions,read_points
,sort_points
, andprint_points
. It ends by calling the exit system call. - A
read_points
function that takes a pointer to the array of points and the number of points. In a loop, it reads the x- and y-coordinates for each point and stores them in the array. - A
sort_points
function that takes a pointer to the array of points and the number of points. I used one of the O(n2) sorting algorithms you learned about in 150 rather than something faster like Quicksort or Mergesort. Thesort_points
function needs to determine if one point is “smaller” than another. To that end,sort_points
calls apoint_less_than
function whenever two points need to be compared. Many sorting algorithms are built on the idea of swapping two elements. I implemented aswap
procedure which does that. point_less_than
takes two pointers as arguments. The first pointer points to the first point and the second to the second point. Since each point is just two consecutive integers, ifp
were a pointer to a point, then (in C),p[0]
is the x-coordinate andp[1]
is the y-coordinate. In MIPS, since$a0
will be the argument register holding the first pointer, we can access the x- and y-coordinates as follows.lw $t0, 0($a0) # load the x-coordinate of the first point into $t0 lw $t1, 4($a0) # load the y-coordinate of the first point into $t1
point_less_than
returns 1 if its first argument is less than its second; otherwise, it returns 0.- Like
point_less_than
,swap
takes two pointers to points as arguments. It loads the four integers (two for each point), and then stores them in the appropriate location for the other point. - A
print_points
function which prints the points in a loop.
Each of these 6 functions is a proper function meaning arguments
are passed in $a0
through $a3
and the return value (if any) is in
$v0
. Proper stack manipulation was performed (reserving space on the
sack for registers, including $ra
in the prologue and cleaning it up
in the epilogue).
You may initially find writing proper functions to be a hassle, but doing so will make your code vastly easier to read and reason about. And it’s required to get full points.
Hints:
- Begin by writing either very detailed psuedo-code, or a working version of this program in a high level language. You want to be sure your overall logic is correct before you translate it into assembly.
- This list of system calls will be helpful.
- You must allocate memory for your array at runtime. The sbrk system call can do this for you. You will note that sbrk takes an amount of memory in bytes while your array will be of integers.
Testing:
You can run your program using the Mars IDE by entering a small sample data set in the I/O window. You should also test your program on a larger data set, since it is supposed to work for as many as 1000 data points. You can use I/O redirection from a command line prompt to run the program with input from a file with the command:
java -jar Mars.jar lab4.asm < input
This zip file contains a sample input file and the corresponding output file. I recommend creating your own, larger, test file as well.
Submission
Submit the lab by committing your code and pushing it to your GitHub repository