# Assignment 3, due Feb 7

## 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 fragment of SMAL assembly code:
```.       =       100000
A:      B       "h","i","l","l"
.       =       A + 1
B:      B       "e"
.       =       B + 3
C:      ASCII   "o girth"
.       =       C + 2
D:      ASCII   "wo"
.       =       D + 3
E:      B       "l","d"
```

a) When this code is assembled for the Hawk computer system our class is using, does the data go in RAM, ROM, or some other memory region? (0.2 points)

This is RAM.

100000 decimal is 186A0 hex; the Hawk ROM runs from 00000 to 0FFFF hex, and the Hawk RAM runs from 10000 to 1FFFF hex, so this is RAM because it is in the latter range.

b) Show the contents of memory locations 100000 through 100010 after the above code is assembled and loaded into memory. Show it as a sequence of bytes (not halfwords or words) with the contents of each byte interpreted as an ASCII character. (0.8 points)

This code keeps loading data into memory and then replacing that data with new data. The following chart shows the addresses in the left column and the data loaded in memory in the right columns, with each column labeled by the source code line number.

```line     2   4   6   8  10
100000: 'h'
100001: 'i' 'e'
100002: 'l'
100003: 'l'
100004:         'o'
100005:         ' '
100006:         'g' 'w'
100007:         'i' 'o'
100008:         'r'
100009:         't'     'l'
100010:         'h'     'd'
```

The last value loaded in each location is the one that matters, so the net result is to load the string "hello world"

c) Show the contents of memory location 100004 as a 32-bit word in hexadecimal. (0.5 points)

100004: "o wo" or, as bytes #6F #20 #77 #6F or, as one word #6F20776F

Note: You will learn the most if you do this by hand first, and then check your work by using the SMAL assembler to assemble the code and then use the Hawk emulator to load the object file and display the contents of memory in hex and ASCII.

Here is the assembly listing of the code from the problem statement:

```                             1  .       =       100000
0186A0: 68  69  6C  6C      2  A:      B       "h","i","l","l"
3  .       =       A + 1
0186A1: 65                  4  B:      B       "e"
5  .       =       B + 3
0186A4: 6F  20  67  69      6  C:      ASCII   "o girth"
72  74  68
7  .       =       C + 2
0186A6: 77  6F              8  D:      ASCII   "wo"
9  .       =       D + 3
0186A9: 6C  64             10  E:      B       "l","d"
11  END
```

And here is what it looks like loaded in RAM by the Hawk emulator:

``` HAWK EMULATOR
/------------------CPU------------------\   /----MEMORY----\
PC:  00000000                R8: 00000000   0186A0: 6C6C6568 hell
PSW: 00000000  R1: 00000000  R9: 00000000   0186A4: 6F77206F o wo
NZVC: 0 0 0 0  R2: 00000000  RA: 00000000   0186A8: 00646C72 rld
R3: 00000000  RB: 00000000   0186AC: 00000000
R4: 00000000  RC: 00000000   0186B0: 00000000
R5: 00000000  RD: 00000000   0186B4: 00000000
R6: 00000000  RE: 00000000   0186B8: 00000000
R7: 00000000  RF: 00000000   0186BC: 00000000
```

2. Background: Here is a fragment of SMAL assembly code for a linked list, where each list element consists of 2 words, first a pointer to (the address of) the next element and then an integer value:
```.       =       #1000
NIL     =       0
A:      W       N, 1
C:      W       E, 9
E:      W       NIL, 3
N:      W       C, 5
P:      W       R, 1
R:      W       A, 4
```

a) Give the integer values of the list elements starting with the element with the label HEAD and continuing until the end of the list (the value NIL in the pointer field marks the end). (0.5 points)

One way to untangle the list is to sort the lines of code so that each list element is given immediately after the element that references it.

```.       =       #1000
NIL     =       0
P:      W       R, 1
R:      W       A, 4
A:      W       N, 1
N:      W       C, 5
C:      W       E, 9
E:      W       NIL, 3
```

Doing this puts the labels in order, PRANCE (an English word) and the numbers in order 3.141593 (with a point added, it looks like a decimal approximation of π).

b) Give the contents, in hexadecimal, of the 4 words of memory starting at location 100016. (0.5 points)

Here is the assembly listing, with the first 4 words highlighted:

```                             1  .       =       #1000
2  NIL     =       0
001000: 00001028            3  HEAD:   W       P, 3
00000003
001008: 00001020            4  A:      W       N, 1
00000001
001010: 00001018            5  C:      W       E, 9
00000009
001018: 00000000            6  E:      W       NIL, 3
00000003
001020: 00001010            7  N:      W       C, 5
00000005
001028: 00001030            8  P:      W       R, 1
00000001
001030: 00001008            9  R:      W       A, 4
00000004
```

3. A simple question: Give a simple rule describing when an ALIGNdirective should be inserted into the assembly code. (0.2 points)

Quoting chapter 3 of the notes: "The basic rule is simple: Worry about alignment whenever smaller objects are followed by larger ones."

More precisely, if you have been assembling bytes, and the next data objects are halfwords or words, use ALIGN 2 first. If you have been assembling halfwords, and the next data objects are words, use ALIGN 4 first.

4. Background: The following SMAL code assembles a Hawk ADD instruction into memory:
```        B       #31
B       #23
```

a) What is the destination register? (0.1 points)

Convert it to binary with the two bytes organized left to right, as in the Hawk manual, and compare that with the frame for the ADD instruction:

``` 0 0 1 1 0 0 0 1   0 0 1 0 0 0 1 1
|0 0 1 1|  dst  | |  src1 |  src2 |
```

So, the destination register is R1

b) What is the first source register? (0.1 points)

R2

c) What is the second source register? (0.1 points)

R3