Assignment 3, due Feb 6

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: Here is some code that can be assembled, linked and run with mp1test to produce some output. In this case, it is written to help you explore what it is that the assembler does:
```        TITLE   "hw3.a by Douglas W. Jones, Jan. 29, 2015"
; -- another demo of the MP1 data structure

NULL    =       0               ; declare the value used for NULL pointers
INT     ARRAY           ; make ARRAY visible to the test program

ARRAY:  W       ITEM1           ; array[0] points to the record ITEM1
W       ITEM2           ; array[1] points to the record ITEM1
W       NULL            ; array[2], a null pointer

; -- the records:

ITEM1:  W       TEXT1           ; pointer to text
H       8, 1            ; X = 8, Y = 1

ITEM2:  W       TEXT2
H       2, 1            ; X = 2, Y = 1

TEXT1:  ASCII   "world", 0
TEXT2:  B       83              ; *
H       #7075           ; *
W       -#FFFF8D9B      ; *

END
```

a) What are the values of the identifiers defined by this code (NULL, ARRAY, ITEM1, ITEM2, TEXT1 and TEXT2)? (1 point)

```NULL  = #00000000
ITEM1 = #0000000C
ITEM2 = #00000014
ARRAY = #00000000
TEXT1 = #0000001C
TEXT2 = #00000022
```

To see where these values come from, take a look at the assembly listing:

```                                 1          TITLE   "hw3.a by
2  ; -- another demo of the
3
4  NULL    =       0
5          INT     ARRAY
6
+00000000:+0000000C              7  ARRAY:  W       ITEM1
+00000004:+00000014              8          W       ITEM2
+00000008: 00000000              9          W       NULL
10
11  ; -- the records:
12
+0000000C:+0000001C             13  ITEM1:  W       TEXT1
+00000010: 0008  0001           14          H       8, 1
15
+00000014:+00000022             16  ITEM2:  W       TEXT2
+00000018: 0002  0001           17          H       2, 1
18
+0000001C: 77  6F  72  6C       19  TEXT1:  ASCII   "world", 0
+00000020: 64  00
+00000022: 53                   20  TEXT2:  B       83
+00000023: 7075                 21          H       #7075
+00000025: 00007265             22          W       -#FFFF8D9B
23
24          END
```

In the above listing (with the tail end of each line deleted to prevent lines from wrapping, the values of the symbols are highlighted where the assembler shows them on the left side of the page.

b) Rewrite the 3 lines of code that are commented with stars to use an equivalent ASCII directive. (For full credit, make sure this directive assembles the correct number of bytes into memory; the obvious answer may fall short.) (1 point)

```        ASCII   "Super",0,0
```

The note the two zeros at the end of string. One would be enough to make the program work correctly, but the original code had a B, H and W directive at the end, so the equivalent ASCII directive must output 7 bytes, with the final two being zeros. We can test that it is equivalent by assembling it and comparing what goes into memory for the original and the new version:

```+00000022: 53  75  70  65       20  TEXT2:  ASCII   "Super", 0, 0
+00000026: 72  00  00
```

c) Are any of the byte, halfword or word directives used in the above code non-aligned? If so, which ones? (0.5 points)

The H on line 21 assembles into memory address #23, which is not divisible by 2.

The W on line 22 assembles into memory address #25, which is not divisible by 4.

2. A Problem: If the Hawk CPU fetches the value FEDC16 into the instruction register, what does it do in response -- that is, what instruction is it and what changes does it make to memory or registers in response? (0.5 points)

The instruction FEDC16 has the first byte DC16 and the second byte FE16.

The first byte DC16 translates to LIS with register R12 as a destination register, while the second byte is the 8-bit constant -2. So, we can write this code:

```        LIS     R12,-2
```

Check this analysis by assembling it:

```                                 1          USE     "hawk.h"
+00000000: DC  FE                2          LIS     R12,-2
```

Some students were puzzled that the assembler complains about LIS R12,#FE. That is because the LIS instruction only allows operands between –128 and +127, inclusive. The hexadecimal value #FE is interpreted by the assembler as being +254, which is outside this range. To express -2 as an unsigned 32-bit quantity in hexadecimal, you would have to write #FFFFFFFE, or you could also write -#02.