Department of Electrical and Computer Engineering

The University of Texas at Austin

EE 306, Fall 2008
Problem Set 5
Due: 10 November, before class
Yale N. Patt, Instructor
TAs: Jeffrey Allan, Arvind Chandrababu, Eiman Ebrahimi, Aravind Jakkani, Khubaib,
        Allison Korczynski, Pratyusha Nidamaluri, Zrinka Puljiz, Che-Chun Su, Christopher Wiley

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 and the time for the discussion section you would like the problem set turned back to you. Show your work.

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
            LD R0, Addr1
            LEA R1, Addr1
            LDI R2, Addr1
            LDR R3, R0, #-6
            LDR R4, R1, #0
            ADD R1, R1, #3
            ST R2, #5
            STR R1, R0, #3
            STI R4, Addr4
            HALT
    Addr1   .FILL x300B
    Addr2   .FILL x000A
    Addr3   .BLKW 1
    Addr4   .FILL x300D
    Addr5   .FILL x300C
            .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 HALT 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 you have the following table in your program:

    
    MASKS   .FILL x0001
            .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.

  4. 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 at the location with label A executed? Assume that the user only calls this "new" TRAP x22 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 ASCII characters stored in consecutive memory locations.
    ; R0 points to the first ASCII character before the new TRAP x22 is called.
    ; The null character (x00) provides a sentinel that terminates the output sequence.
    
            .ORIG x020F
    START   LDR R1, R0, #0
            BRz DONE
            ST R0, SAVER0
            ADD R0, R1, #0
            TRAP x21
            LD R0, SAVER0
    A       ADD R0, R0, #1
            BRnzp START
    DONE    RET
     
    SAVER0  .BLKW #1
            .END
    
  5. (Adapted from 9.2)
    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?

  6. Suppose we are writing an algorithm to multiply the elements of an array (unpacked, 16-bit 2's complement numbers), and we are told that a subroutine "mult_all" exists which multiples four values, and returns the product. The mult_all subroutine assumes the source oprands are in R1, R2, R3, R4, and returns the product in R0. For purposes of this assignment, let us assume that the individual values are small enough that the result will always fit in a 16-bit 2's complement register.

    Your job: Using this subroutine, write a program to multiply the set of values contained in consecutive locations starting at location x6001. The number of such values is contained in x6000.

    1. Assume the value in x6000 is a multiple of 4.
    2. Assume the value in x6000 is not a multiple of 4.
    3. Hint: Feel free to include in your program

      
      PTR	.FILL x6001
      CNT	.FILL x6000
      

  7. (Adapted from 9.13)
    The following program is supposed to print the number 5 on the screen. It does not work. Why? Answer in no more than ten words, please.
    
    	.ORIG 	x3000
    	JSR	A
    	OUT	
    	BRnzp	DONE
    A 	AND	R0,R0,#0
    	ADD	R0,R0,#5
    	JSR	B
    	RET	
    DONE	HALT
    ASCII	.FILL	x0030
    B	LD	R1,ASCII
    	ADD	R0,R0,R1
    	RET
    	.END
        
    
    
  8. (Adapted from 10.1)
    What are the defining characteristics of a stack? Give two implementations of a stack and describe their differences.

  9. A zero-address machine is a stack-based machine where all operations are done by using values stored on the operand stack. For this problem, you may assume that the ISA allows the following operations:

    PUSH M - pushes the value stored at memory location M onto the operand stack.

    POP M - pops the operand stack and stores the value into memory location M.

    OP - Pops two values off the operand stack and performs the binary operation OP on the two values. The result is pushed back onto the operand stack.

    Note: OP can be ADD, SUB, MUL, or DIV for parts a and b of this problem.
    Note: See the stack machine supplemental handout for help on this problem.

    1. Draw a picture of the stack after each of the instructions below are executed. What is the minimum number of memory locations that have to be used on the stack for the purposes of this program? Also write an arithmetic equation expressing u in terms of v, w, x, y, and z. The values u, v, w, x, y, and z are stored in memory locations U, V, W, X, W, and Z.
                  PUSH V
                  PUSH W
                  PUSH X
                  PUSH Y
                  MUL
                  ADD
                  PUSH Z
                  SUB
                  DIV
                  POP U
      	    
    2. Write the assembly language code for a zero-address machine (using the same type of instructions from part a) for calculating the expression below. The values a, b, c, d, and e are stored in memory locations A, B, C, D, and E.

      e = ((a * ((b - c) + d))/(a + c))