Department of Electrical and Computer Engineering
The University of Texas at Austin




EE 306 Fall 2002
Yale Patt, Instructor
TAs: Asad Bawa, Linda Bigelow, Mustafa Erwa, Lester Guillory, Kevin Major,
Moinuddin Qureshi, Paroma Sen, Tanay Shah, Santhosh Srinath,
Matt Starolis, David Thompson, Vikrant Venkateshwar

Problem Set #6
Due November 18, 2002 before class

Problem 1

  1. What problem is likely to occur if the keyboard hardware does not check the KBSR before writing to the KBDR?
  2. What problem could occur if the CRT output hardware does not check the CRTSR before writing to the screen?


Problem 2

  1. How many TRAP service routines can be implemented in the LC-2? Why?

  2.  
  3. Why must a RET instruction be used to return from a TRAP routine? Why won't a BRnzp (Unconditional BR) instruction work instead?

  4.  
  5. How many accesses to memory are made during the processing of a TRAP instruction? Assume the TRAP is already in the IR.


Problem 3
Which of the following combinations describe the system in Section 8.2.2 of the textbook? Please select one.


Problem 4
Consider the following LC-2 assembly langauge program:

          .ORIG x3000
    L1    LEA      R1, L1
          AND      R2, R2, x0
          ADD      R2, R2, x2
          LD       R3, P1
    L2    LDR      R0, R1, xC
          OUT
          ADD      R3, R3, -1
          BRZ      GLUE
          ADD      R1, R1, R2
          BRNZP    L2
    GLUE  HALT
    P1    .FILL    xB
          .STRINGZ "HBoeoakteSmtHaotren!s"
          .END
  1. After this program is assembled and loaded, what binary pattern is stored in memory location x3005?

  2.  
  3. Which instruction (provide a memory address) is executed after the instruction at location x3005 is executed?

  4.  
  5. Which instruction (provide a memory address) is executed prior to the instruction at location x3006 is executed?

  6.  
  7. What is the output of this program?


Problem 5
Consider the following LC-2 assembly language program:

       .ORIG x3000
       LEA   R0, DATA
       AND   R1, R1, #0
       ADD   R1, R1, #9
LOOP1  ADD   R2, R0, #0
       ADD   R3, R1, #0
LOOP2  JSR   SUB
       ADD   R2, R2, #1
       ADD   R3, R3, #-1
       BRP   LOOP2
       ADD   R1, R1, #-1
       BRP   LOOP1
       HALT
DATA   .BLKW #10
SUB    LDR   R5, R2, #0
       NOT   R4, R5
       ADD   R4, R4, #1
       LDR   R6, R2, #1
       ADD   R4, R4, R6
       BRZP   CONT
       STR   R5, R2, #1
       STR   R6, R2, #0
CONT   RET
       .END
Assuming that the memory locations starting at label DATA contains ten 2's complement numbers. These numbers are filled in before the program executes. What is the relationship between the final values at DATA and the initial values at DATA?
 

Problem 6
Peter decided to design a variant of the LC-2 that did not need a keyboard status register. Instead, he created a readable/writable keyboard data and status register (KBDSR), which contains the same data as the KBDR. With the KBDSR, a program requiring keyboard input would wait until a nonzero value appeared in the KBDSR. The nonzero value would be the ASCII value of the last key press. Then the program would write a zero into the KBDSR indicating that it had read the key press. Modify the basic input service from Section 8.2.2 of the textbook to implement Peter's scheme. Assume that the KBDSR register is stored at xF400.
 

Problem 7
Suppose we are writing an algorithm to multiply the elements of an array (unpacked, 16-bit 2's complement numbers) Because we are doing this procedure on several different arrays, we do not want to have to copy our code every time we want to do the array multiply. Assume that you can jump to an a subroutine with label mult_array to carry out this procedure (YOU DO NOT HAVE TO WRITE mult_array). The subroutine mult_array assumes the following: R1 contains the number of elements in the array, registers R2 through R5 contain the elements of the array, and mult_array returns the result in R0. (assume there is at least one and no more than 4 elements in the array)

a) Write assembly code that calls the subroutine at label mult_array, and stores the result at label result. You need to prepare the data before calling the subroutine. Assume the label N references the number of elements in the array, and that label array_a references the first element of our array. Only load the registers that are needed for multiply subroutine (eg. If N=3 then only load registers R2, R3 and R4).

              .orig x3000
	      ...               ; your code calls mult_array
	      ...		;  "
	      ...		;  "
	      ...		;  "
	      ...               ;  "
	      ...               ;  (not necessarly this many lines of code)
	      ...		;  
	      HALT
  mult_array  ...               ; DO NOT WRITE mult_array
              ...		; 
	      ...
	      ...
	      ...
	      RET
  N	      .blkw 1           ; N is filled in at runtime
  array_a     .blkw 4		; 4 is the maximum number of elements in our array
  result      .blkw 1		; store result here
              .end
b) Suppose we now want to call mult_array for an array with more than 4 elements. What recomendation would you give the programer writing mult_array?

Problem 8
An Engineer writes a program that inputs the social security number (9 digits) of the user and stores it in memory. Before storing any digit, the pragram checks if the user's input was a decimal digit (0-9). If, at any point, the user inputs anything other than one of the decimal digits, the program prints an error message and halts the machine. The program that the engineer wrote is shown below. When testing the program, he finds a crucial mistake in his code.

a) What is the problem with this program?
b) Can the assembler detect this problem?
c) To fix the problem, the engineer ADDs two instructions to the program. Specify the two instructions that solves the problem in the order that they would appear in the program. Indicate where the first instruction is inserted.
Hint: There is an EXTRA memory location allocated at the end of the program.
Answer to (c):
First instruction _____________________.
Second instruction _____________________.
He added the instruction _____________________ and put it before the instruction ______________________.


          .ORIG   x3000
	  LEA     R1, SSN           ;ADDRESS OF WHERE TO STORE SSN IN R1
	  LD	  R2, MASK0	    ;MASKS TO CHECK IF INPUT IN...
	  LD	  R3, MASK1	    ;30-3F IN R2 AND R3
	  LD	  R4, NLINE	    ;ASCII CODE FOR NEW LINE
	  LD	  R5, FILTER	    ;MASK TO CONVERT DIGIT FROM ASCII TO BINARY
	  AND	  R6, R6, #0	    ;CLEAR R6 FOR MASKING 
	  ADD	  R0, R4, #0
	  TRAP	  x21               ;GO TO A NEW LINE
	  LEA	  R0, PROMPT
	  TRAP	  x22               ;PROMPT USER
	  AND	  R7, R7, #0
	  ADD	  R7, R7, #9        ;COUNT
LOOP	  ADD	  R7, R7, #-1
          BRn	  DONE
	  TRAP	  x20               ;GET NEXT DIGIT
	  TRAP	  X21		    ;ECHO TO SCREEN
	  AND	  R6, R0, R2        ;CHECK IF BIT 4 IS SET (INPUT IN 30-3F)
	  BRz	  STOP
	  AND	  R6, R0, R3        ;CHECK IF BIT 5 IS SET (INPUT IN 30-3F)
	  BRz	  STOP
	  AND	  R0, R0, R5        ;EXTRACT LAST 4 BITS
	  ADD	  R6, R0, #-10      ;CHECK IF A DECIMAL DIGIT
	  BRzp	  STOP
	  STR	  R0, R1, #0        ;STORE DIGIT
	  ADD	  R1, R1, #1
	  BRnzp	  LOOP
STOP	  LD	  R0, NLINE
	  TRAP	  x21               ;GOT TO A NEW LINE
	  LEA	  R0, ERROR
	  TRAP	  x22               ;PRINT ERROR MESSAGE AND HALT 
DONE	  ADD	  R0, R4, #0
	  TRAP	  x21
	  HALT
SSN	  .BLKW   9
EXTRA	  .BLKW	  1
MASK0	  .FILL	  x0010
MASK1	  .FILL	  X0020
NLINE	  .FILL	  x000A
FILTER	  .FILL	  x000F
PROMPT	  .STRINGZ "ENTER YOUR SSN#: "
ERROR	  .STRINGZ "ERROR: THIS IS NOT A NUMBER"
	  .END