# Assignment 10, due Apr 11

## Solutions

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: Consider this floating point format:
```  _______________
|_|_______|_____|
| | | | | | | | |
|s|   e   |  m  |
```
• s = the sign of the mantissa
• e = the biased exponent, where 1000 = 0
• m = magnitude of the mantissa, where 100 = 0.5

There are no hidden bits here, and no special cases for e = 0000 or e = 1111.

a) What is the exact decimal representation of the largest positive number in this floating point system? (0.3 points)

01111111 = 0.1112 × 2111 = 7/8 × 27 = 7 × 24 = 112

b) What is the exact decimal representation of the most extreme negative number in this floating point system? (0.1 points)

11111111 = -112

c) What is the exact decimal representation of the smallest nonzero positive number in this floating point system? (0.3 points)

00000001 = 0.0012 × 2-8 = 1/8 × 2-8 = 0.00048828125

2. Background: Now, consider what you gain by adding an IEEE-style hidden bit to this floating point number system:
• s = the sign of the mantissa
• e = the biased exponent, where 1000 = 0,
where 0000 and 0001 indicate the same exponent value,
but 0000 indicates a hidden bit value of zero,
and 1111 is reserved for NaN values like infinity.
• m = magnitude of the mantissa,
if the hidden bit is 1, 100 = 1.5
if the hidden bit is 0, 100 = 0.5
the hidden bit is 1 for all normalized mantissas.

Note that we did not move the point when we added the hidden bit, so the hidden bit is in the one's place of the mantissa.

a) What is the exact decimal representation of the largest non-infinite positive number in this floating point system? (0.3 points)

01110111 = 1.1112 × 2110 = 1 7/8 × 26 = 15 × 23 = 120

b) What is the exact decimal representation of the most extreme non-infinite negative number in this floating point system? (0.1 points)

11110111 = -120

c) What is the exact decimal representation of the smallest nonzero positive number in this floating point system? (0.3 points)

00000001 = 0.0012 × 2-7 = 2-3 × 2-7 = 2-10 = 0.0009765625

d) What is the binary representation of 1.0 in this floating point system. (0.3 points)

01000000 = 1.0002 × 20

3. Background: Consider the problem of computing the distance between two points in the plane -- given that R3 points to one point and R4 points to another point, where each point is represented as 2 consecutive words in memory, where the words of each point represent the x and y coordinates of that point.

A problem: Write Hawk code to compute the distance, leaving the result in FPA0. Assume the floating point coprocessor is already turned on in single-precision mode. (1.0 point)

In the following solution, the code has been optimized to give the coprocessor time to finish each operation. So, once it is busy computing on one accumulator, the next operation, if possible, involves the other acumulator. Toward the end, we have nothing else to do. A comment indicates where it would be good to do something else.

The assignment did not require or even suggest this optimization. There are many variations that are possible, involving reordering the earlier instructions here or using other registers. If you COGET each result immediately, you can use just one floating point accumulator, but you will need more general purpose registers.

```        LOADS   R5,R3
COSET   R5,FPA0         ; -- fpa0 = p1.x
COSET   R5,FPSUB+FPA0   ; -- fpa0 = fpa0 - p1.x
COSET   R5,FPA1         ; -- fpa1 = p1.y
COSET   R5,FPSUB+FPA1   ; -- fpa1 = fpa1 - p2.y
COGET   R5,FPA0
COSET   R5,FPMUL+FPA0   ; -- fpa0 = fpa0 ** 2 = (p1.x - p1.y)**2
COGET   R5,FPA1
COSET   R5,FPMUL+FPA1   ; -- fpa1 = fpa1 ** 2 = (p1.y - p2.y)**2
COGET   R5,FPA0
COSET   R5,FPADD+FPA1   ; -- fpa1 = fpa1 + fpa0
;                         -- can we do something here?
COGET   R5,FPA1
COSET   R5,FPSQRT+FPA0  ; fpa0 = sqrt((p1.x-p1.y)**2+(p1.y-p2.y)**2)
```

4. A small problem: Look at the code near the start of Chapter 12 for the GETCHAR routine. This uses the BITTST and BBR macros. What are the actual machine instructions this uses. You can solve this problem using the manual, but it might be faster to just assemble the code and look at how the Hawk emulator disassembles it. (0.3 points)

The code in question is:

```        BITTST  R3,KSTATRD
BBR     KBDPOLL         ; } while ((kbdstat & kstatrd) == 0);
```

Substituting the definition for KSTATRD this is:

```        BITTST  R3,0
BBR     KBDPOLL         ; } while ((kbdstat & kstatrd) == 0);
```

The BITTST macro in hawk.h translates this to:

```        ADDSR   R0,R3,1
BCR     KBDPOLL
```

This can also be described as:

```        SR      R3,1
BCR     KBDPOLL
```