# Assignment 5, due Feb 21

## 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 based on instructions introduced in Chapter 5 of the notes:
```        LEA     R1,X
X:	JSRS	R1,R1
BR	X
```

a) Give the result of assembling this code as a sequence of address-value pairs, one per line, where each value is a halfword given in hexadecimal. Assume you are starting at address zero. (0.4 points)

Hint: You will learn the most from this exercise by doing it by hand, first, using the Hawk Manual and the material in Chapter 5 of the notes, and then check your work by using the SMAL assembler.

Here is an assembly listing for this code:

```+00000000: F1  70  0000          3          LEA     R1,X
+00000004: F1  B1                4  X:      JSRS    R1,R1
+00000006: 00  FE                5          BR      X
```

Converting this to address-value pairs where the values are halfwords gives: +00000000: 70F1 +00000002: 0000 +00000004: B1F1 +00000006: FE00

There are two distinct issues here: First, there is the obvious one caused by the Hawk being a lowbyter machine. The sequence of bytes F1 70 is stored as the halfword 70F1, for example.

Second, both the LEA instruction and the BR instruction in the example use PC relative addressing. In the case of the LEA instruction, the second halfword of the instruction is not the address X, but rather, the distance in bytes from the LEA instruction to X -- zero because X labels the immediately following halfword.

For the BR instruction, the second byte of the instruction is not the address X, but rather, the distance in halfwords from the instruction to X. This is negative because X is before the branch instruction, and it is negative two because, skipping backward, we must skip over both the BR instruction itself and the JSRS instruction. It is useful to remember that X:BR X, the shortest possible infinite loop, has a branch displacement of -1, while a branch displacement of zero, the halfword #0000, is a no-operation.

b) What value does the LEA instruction load into R1? (0.2 points)

The address X, which is the address of the JSRS instruction, which is 4 if the code is loaded starting at address zero.

c) How many times is the JSRS instruction executed before the first time the BR instruction is executed? (0.4 points)

Hint: Again, work this out from the manual and Chapter 5, and then check your work by loading and running the code on the Hawk emulator, using the s command (single step) to advance the emulator one instruction at a time to see what it does.

It is executed twice.

The first time, R1, the destination register, is equal to X, the address of the JSRS instruction. This makes the CPU execute the JSRS instruction again.

The second time, R1 holds the return address from the first execution of the JSRS instruction, that is, the address of the BR instruction. So, the second time the JSRS instruction is executed, it will be followed by executing the BR.

2. Background: Consider this fragment of the Hello World program from Chapter 5 of the notes:
```;  --- begin aplication code ---
LIL     R3,HELLO
LIL     R1,PUTS
JSRS    R1,R1        ; puts(HELLO)
;  --- end aplication code ---
```

At the start of this code, R3 holds the width of the display area, R4 holds the height. Note that the PUTAT monitor subroutine sets the coordinates for the next output. Note also that the instruction SR r,1 will divide any register r by two (you can look it up if you want, but all you need to know right now is that it works).

A problem: Modify the hello world program so that the H of the word Hello is displayed at the center of the display area of the screen. You need not run the program (but of course, you can check your work by running it.) Do not turn in the entire program. Just turn in the part of the code between the begin application code and end application code comments. Handwritten solutions are welcome, but as in all programming, legibility is important. Poorly formatted code will be penalized proportionally to its degree of unreadability. (1.0 points)

```;  --- begin aplication code ---
SR      R3,1         ; -- parameter width/2
SR      R4,1         ; -- parameter height/2
LIL     R1,PUTAT
JSRS    R1,R1        ; putat( width/2, height/2 )

LIL     R3,HELLO
LIL     R1,PUTS
JSRS    R1,R1        ; puts(HELLO)
;  --- end aplication code ---
```

Some students found remarkably complicated interpretations of this assignment, for example, interpreting it as asking for only the H of the string "Hello world!" to be centered, while the remaining "ello world!" was to remain where it had been, or requiring that the entire string "Hello world!" be centered, requiring far more complicated arithmetic on the screen dimensions.

These interpretations seem to be the result of reading the question very quickly and carelessly and then starting work on a solution prematurely without taking the time to properly contemplate the question.

3. Background: In high-level programming languages, you can write code something like this:
```    if ((a >= 10) && (a < 20)) a = 0;
```
Assume that R3 is being used to hold the value of the variable a.

A problem: Write a fragment of SMAL Hawk code that is equivalent to the above. (1.0 point)

Hint: It will involve at least two comparisons, at least two branches, some labels and the code for the assignment a=0.

This is a programming problem, so there are, of course, many soltuions. The following is probably the simplest.

```        CMPI    R3,10
BLT     ENDIF   ; if ((a >= 10)
CMPI    R3,20
BGE     ENDIF   ; &&  (a < 20)) {

LIS     R3,0    ;   a = 0;

ENDIF;                  ; }
```

The assignment did not specify whether a was a signed integer or an unsigned integer, so if a student gave BLTU and BGEU instructions, that should be OK, but mixing signed with unsigned comparisons would be wrong becaus either the variable a is signed or it is not, it cannot be both.