Homework Assignment 2
Computer Organization
Spring, 2013



Written Assignment: Due in class Monday, March 18th

Write Java expressions to do the following:  (Assume x and y are declared as ints.  Bits are numbered 31 to 0 from left to right.)

1.  Set y to the value in bits 17-10 of x.

2.  Set bits 17-10 of x to 0.

3.  Set bits 17-10 of x to 1.

4.  Invert bits 17-10 of x.

5.  Store the value '0110' in bits 19-16 of x.


Programming Assignment, due via handin by 8pm, Tuesday, March 19th. Turn in as assignment 2, class 210

For this assignment you will write three MIPS assembly language programs.  (Use MARS to test and run your programs.)

Problem 1.  Write a program which computes a sequence of fibonacci numbers and displays it.

Input:

Read three input values from the keyboard:

a.  the first number in the sequence,
b.  the second number in the sequence, and
c.  the number of elements of the sequence.

Each element of the sequence (beyond the first two) is equal to the sum of the previous two.  For example, if the user inputs 3, 1, and 10, then your program should generate the sequence 3, 1, 4, 5, 9, 14, 23, 37, 60, 97.

Output:

For each element of the sequence that you generate, display the following:

a.  the number in decimal notation (using syscall 1).
b.  the number in hexadecimal.
c.  the number of 1-bits in the binary representation of the number.

For the example above, you would display

3     0x00000003   2
1     0x00000001   1
4     0x00000004   1
5     0x00000005   2
9     0x00000009   2
14     0x0000000E   3
23     0x00000017   4
37     0x00000025   3
60     0x0000003C   4
97     0x00000061   3

The hex version can be displayed using system call 34.

Don't use arrays for this problem.  They are not necessary.


Problem 2.  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.

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 large enough to hold as many as 1000 points.  You can view the storage conceptually as a series of pairs, as in the table below:

3
4
4
2
0
-5
1
3

You are responsible for:

1.  Allocating the space for the data.
2.  Deciding how to map the data to the space you allocate.
3.  Determining 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.

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 problem2.asm < input

Here is a zip file containing a sample input file and the corresponding output file:  mipsio.zip.  I recommend creating your own, larger, test file as well.

Problem 3.

Implement a doubly-linked list in MIPS assembly language.  The program will read a sequence of integers from the user and insert each one into the list.  Maintain the list in increasing order from head to tail on each insertion.  The input will be terminated by a value of -9999.  When the input is complete, display the data from the list in forward order, beginning at the head of the list.  Then, display the data in reverse order, beginning at the tail of the list.

When you display the contents of the list, print for each node the address of the node, the prior and next pointers from the node, and the data item in the node.  Print the data items in decimal and all of the addresses in hex.

sample input:

300
200
400
100
500
-9999

sample output:

list in forward order:
address         prior           next           data

0x10040024      0x00000000      0x1004000c      100
0x1004000c      0x10040024      0x10040000      200
0x10040000      0x1004000c      0x10040018      300
0x10040018      0x10040000      0x10040030      400
0x10040030      0x10040018      0x00000000      500

list in reverse order:
address         prior           next           data
0x10040030      0x10040018      0x00000000      500
0x10040018      0x10040000      0x10040030      400
0x10040000      0x1004000c      0x10040018      300
0x1004000c      0x10040024      0x10040000      200
0x10040024      0x00000000      0x1004000c      100

problem 3 remarks:

1.  A "list" should consist of two addresses:  a pointer to the head of the list and a pointer to the tail of the list.  You can create an empty list in your data section with the directive

list:    .word    0,0

2.  A node consists of three words:  a data item, a prior pointer, and a next pointer.

3.  Use a value of 0 to indicate a null pointer.

4.  Allocate space for list nodes as follows:

5.  Write functions for the following:

a.  Insert a data item in a linked list.

b.  Print the contents of a linked list in forward order.

c.  Print the contents of a linked list in reverse order.

For each of these, pass the address of the list to the function in register a0.  The functions should work on any linked list, so within your functions, you should not refer to the linked list directly by name.


General remarks:

1.  Don't hand in your work until you have completed all three problems.  Your handin directory should contain all three programs.

3.  Document your programs thoroughly.  (This is especially important in assembly language programs, since the code itself is less easily read than high-level language code.)  This should include: