# Assignment 9, due April 3

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of class on the day indicated (usually Friday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

1. Background: Suppose you have the following two routines in a math library:
URECIP — fixed point unsigned reciprocal
Parameter: R3 — an unsigned integer
Return value: R3 — an unsigned fixed point fraction
Side effects: May disturb R4 to R7
The return value has its point to the left of bit 31, so 1/2, for example, is represented as 8000000016.
LUMUL — long unsigned multiply
Parameter: R3, R4 — unsigned integers to multiply
Return value: R3R4 — the 64-bit unsigned product
Side effects: May disturb R5 to R7
R3 is the least significant word of the product, R4 is the most significant word.

One way to compute the quotient a/b is to compute a × 1/b.

A problem: Write a divide subroutine routine using the above that conforms to the following specification: (1 point)

UDIV — unsigned divide
Parameter: R3 — dividend, R4 — divisor
Return value: R3 — the quotient
Side effects: May disturb R4 to R7
This code does not compute the remainder, and the quotient will be truncated to the next lower integer.

2. A problem: To call an external subroutine called SUBR on the Hawk, you write:
```        EXT     SUBR
LIL     R1,SUBR
JSRS    R1,R1
```

To get the value of a one-word global variable called GLOB on the Hawk, you might write:

```	COMMON	GLOB,4
LIL     R1,GLOB
```

If you have a Sparrowhawk (see Chapter 16 of the Hawk manual) there are no 32-bit instructions. Section 16.2 suggests that you can rewrite an LIL as an LIS followed by two ORIS instructions.

a) This does not work for external symbols such as SUBR or GLOB from the above examples. Try them, see what happens, and then explain why the advice in Section 16.2 fails in this context. (0.5 points)

Note: The Hawk manual is correct in asserting that an LIL can, in principle, be used. You may need to review Sections 4.1 and 4.3 of the SMAL manual (about external symbols) as well as Section 2.3 in order to see what the problem is.

b) (0.5 points) How can you use a PC-relative LOAD instead of an LIL to load the value of SUBR or GLOB into a register. This is step one along the road to converting Hawk code to run on the Sparrowhawk. Step two is to replace the LOAD instruction, following the suggestion in Section 16.2 of the Hawk manual (this time, it will work).

Note: You may need to review Chapter 5 of the notes. Note also that the Sparrowhawk has no LOAD instruction, only LOADS, but that the suggestion in Section 16.2 of the Hawk manual works here where it failed for part a.

3. Background: The Hawk monitor is awful. Suppose we wanted to design a better monitor that supported output windows. There would be one global window object provided by the new monitor, call it ROOTWINDOW. Note that the common block named ROOTWINDOW holds the object, it does not hold a variable that points to the object. The following methods would apply to any window object w:
w.height() — returns the height of window w.
w.width() — returns the width of window w.
w.setat(x,y) — set the coordinates for output to w.
Each window remembers a current location for plotting data in that window.
w.putchar(c) — plot the character c in window w
Ploting a character updates the current locaiton in the window.
Plotting wraps to the next line if you reach the margin of the window.
w.puts(s) — Equivalent to w.putchar for each character in s.
w.newwin(x,y,h,w) — returns the handle for a new window.
The parameters x and y give the upper left corner of the window;
h and w specify its height and width.

Assume we are using the compact approach to polymorphic object representation, so the first field of each object is the pointer to the class descriptor for the object, and the fields of the descriptor are pointers to the corresponding methods. This class must be polymorphic because plotting on the base window references the display hardware, while plotting on a sub-window references a field of the window representating that sub-window.

Assume that the identifiers SETAT, NEWWIN etc are defined displacements into the method table.

A problem: Write SMAL Hawk code to write "Hello World" in a new window that is just large enough for that string (you can count the characters), where that window is centered in the root window. (1 point)