## Department of Electrical and Computer Engineering

### The University of Texas at Austin

EE 306, Fall 2006
Problem Set 5
Due: Monday November 13th, before class
Instructor: Yale N. Patt
TAs: Aseem Bathla, Cameron Davison, Lisa de la Fuente, Phillip Duran, Jose Joao,
Jasveen Kaur, Rustam Miftakhutdinov, Veynu Narasiman, Nady Obeid, Poorna Samanta.

#### Instructions

You are encouraged to work on the problem set in groups and turn in one problem set for the entire group. Remember to put all your names on the solution sheet. Also remember to put the name of the TA in whose discussion section you would like the problem set turned back to you.

#### Questions

1. Bob Computer just bought a fancy new graphics display for his LC-3. In order to test out how fast it is, he rewrote the `OUT` trap handler so it would not check the `DSR` before outputting. Sadly he discovered that his display was not fast enough to keep up with the speed at which the LC-3 was writing to the `DDR`. How was he able to tell?

2. Bob also rewrote the handler for `GETC`, but when he typed `ABCD` into the keyboard, the following values were input:

``````
AAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDD
``````

What did Bob do wrong?

1. The following program does not do anything useful. However, being an “electronic idiot,” the LC-3 will still execute it.

``````
.ORIG x3000
LDR R3, R0, #-5
LDR R4, R1, #0
ST R2, #4
STR R1, R0, #3
.END
``````

Without using the simulator, answer the following questions:

1. What will the values of registers `R0` through `R4` be after the LC-3 finishes executing the `ADD` instruction?

2. What will the values of memory locations `Addr1` through `Addr5` be after the LC-3 finishes executing the `STI` instruction?

2. The LC-3 has just finished executing a large program. A careful examination of each clock cycle reveals that the number of executed store instructions (`ST`, `STR`, and `STI`) is greater than the number of executed load instructions (`LD`, `LDR`, and `LDI`). However, the number of memory write accesses is less than the number of memory read accesses, excluding instruction fetches. How can that be? Be sure to specify which instructions may account for the discrepancy.

3. Assume that an integer greater than 2 and less than 32,768 is deposited in memory location `A` by another module before the program below is executed.

``````
.ORIG  x3000
AND    R4, R4, #0
LD     R0, A
NOT    R5, R0
;
REMOD   JSR    MOD
BRz    STORE0
;
BRz    STORE1
BR     REMOD
;
STORE0  ST     R4, RESULT
TRAP   x25
;
NOT    R3, R1
BRp    DEC
RET
;
A       .BLKW  1
RESULT  .BLKW  1
.END
``````

In 20 words or fewer, what does the above program do?

Hint: if you do not know what `RET` does, try the Index at the back of the book. In fact, you might even take care to look at footnotes on the referenced pages, in addition to the body of the page.

1. Write an LC-3 assembly language subroutine `BIN_OUT` that prints the value in `R0` as a 16-bit binary number.

2. Write a similar subroutine `BIN_GET` that puts the zero-extended binary number input by the user into `R0`. Assume that the user will enter at least one and no more than 16 zeroes and/or ones. The user will then hit the “Enter” key (ASCII code `x0A`) to indicate the end of the binary number. Make sure to echo the input to the screen.

4. Assume that you have the following table in your program:

``````
.FILL x0002
.FILL x0004
.FILL x0008
.FILL x0010
.FILL x0020
.FILL x0040
.FILL x0080
.FILL x0100
.FILL x0200
.FILL x0400
.FILL x0800
.FILL x1000
.FILL x2000
.FILL x4000
.FILL x8000
``````
1. Write a subroutine `SET` in LC-3 assembly language that sets a bit in `R0` using the table above. The index of the bit to set is specified in `R1`.

2. Write a similar subroutine `CLEAR` that clears the specified bit instead of setting it.

5. (This problem was added on 11/06/06) (Adapted from 7.18) The following LC-3 program compares two character strings of the same length. The source strings are in the .STRINGZ form. The first string starts at memory location x4000, and the second string starts at memory location x4100. If the strings are the same, the program terminates with the value 1 in R5, otherwise the program terminates with the value 0 in R5. Insert instructions at (a), (b), and (c) that will complete the program. There should be only 1 instruction per blank.

``````
.ORIG  x3000
LD     R1, FIRST
LD     R2, SECOND
AND    R0, R0, #0

LOOP    _________________    ; (a)
LDR    R4, R2, #0
BRz    NEXT

_________________    ; (b)

_________________    ; (c)
BRz    LOOP
AND    R5, R5, #0
BRnzp  DONE
NEXT    AND    R5, R5, #0
DONE    TRAP   x25
FIRST   .FILL  x4000
SECOND  .FILL  x4100
.END
``````
6. Jane Computer (Bob's adoring wife), not to be outdone by her husband, decided to rewrite the `TRAP x22` handler at a different place in memory. Consider her implementation below. If a user writes a program that uses this `TRAP` handler to output an array of characters, how many times is the `ADD` instruction with label `A` executed? Assume that the user only calls this `TRAP` handler once. What is wrong with this `TRAP` handler? Now add the necessary instructions so the `TRAP` handler executes properly.

Hint: `RET` uses `R7` as linkage back to the caller.

``````
; TRAP handler
; Outputs ASCI characters stored in consecutive memory locations.
; R0 points to the first ASCI character.
; The null character (x00) provides a sentinel that terminates the output sequence.

.ORIG x020F
START   LDR R1, R0, #0
BRz DONE
ST R0, SAVER0
TRAP x21
LD R0, SAVER0
1. How many `TRAP` service routines can be implemented in the LC-3? Why?
2. Why must a `RET` instruction be used to return from a `TRAP` routine? Why won't a `BRnzp` (unconditional `BR`) instruction work instead?
3. How many accesses to memory are made during the processing of a `TRAP` instruction?