CSCI 210: Lab 4
MIPS
arrays
Due:
11:59
PM
on
Wednesday,
July 7th
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. You will write this program using the MARS MIPS simulator, which you can download here. Check out the lab template from github here.
Partners
You can work on this lab with a partner if you choose. If you decide to work with a partner, you and your partner should check out a single lab4 repository. The first partner will create a team name, and the second partner should choose that team name. Please be careful choosing a team, as this cannot be undone. Please name your team something that makes it clear who you are.
If you choose to work with a partner, you and your partner must complete the entire lab together. Dividing the lab up into pieces and having each partner complete a part of it on their own will be considered a violation of the honor code. Both you and your partner are expected to fully understand all of the code you submit.
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
You will need to:
- Allocate the space for the data.
- 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 a bubble sort. Keep in mind that when you swap two points in your list, you must swap both their x and y coordinates.
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
You may assume that overflow will not occur as a result of computing x2 + y2.
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.
Tips:
- This list of system calls will be helpful.
- You should 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.