Department of Electrical and Computer Engineering

The University of Texas at Austin

EE 306, Fall 2009
Problem Set 6 Solutions
Due: Not to be turned in
Yale N. Patt, Instructor
TAs: Aater Suleman, Chang Joo Lee, Ameya Chaudhari, Antonius Keddis, Arvind Chandrababu, Bhargavi Narayanasetty, Eshar Ben-dor, Faruk Guvenilir, Marc Kellermann, RJ Harden
Note: This problem set is unusually long, and is not to be turned in. We have put it together and handed it out to give you some challenging examples to help you prepare for the final exam.

        .ORIG   x3000
        LEA     R0, DATA
        AND     R1, R1, #0
        ADD     R1, R1, #9
LOOP1   ADD     R2, R0, #0
        ADD     R3, R1, #0
LOOP2   JSR     SUB1
        ADD     R4, R4, #0
        BRzp    LABEL
        JSR     SUB2
LABEL   ADD     R2, R2, #1
        ADD     R3, R3, #-1
        BRp     LOOP2
        ADD     R1, R1, #-1
        BRp     LOOP1
        HALT
DATA    .BLKW   #10
SUB1    LDR     R5, R2, #0
        NOT     R5, R5
        ADD     R5, R5, #1
        LDR     R6, R2, #1
        ADD     R4, R5, R6
        RET
SUB2    LDR     R4, R2, #0
        LDR     R5, R2, #1
        STR     R4, R2, #1
        STR     R5, R2, #0
        RET
        .END

The final values at DATA will be sorted in ascending order.

          .ORIG x3000 
          LD R1, PTR 
          AND R0, R0, #0 
          LD R2, COUNT 
LOOP      LDR R3, R1, #0 
          ADD R0, R0, R3 
          ADD R1, R1, #1
          ADD R2, R2, #-1 
          BRp LOOP 
          STI R0, RESULT
          HALT
PTR       .FILL x4000 
RESULT    .FILL x5000
COUNT     .FILL #10 
          

If the condition codes were not saved as part of initiation of the interrupt service routine, we could end up with incorrect results. In this program, take the case when an interrupt occurred during the processing of the instruction at location x3006 and the condition codes were not saved. Let R2 = 5 and hence the condition codes would be N=0, Z=0, P=1, before servicing the interrupt. When control is returned to the instruction at location x3007, the BRp instruction, the condition codes depend on the processing within the interrupt service routine. If they are N=0, Z=1, P=0, then the BRp is not taken. This means that the result stored is just the sum of the first five values and not all ten.

Steps for handling interrupts:

  1. Saving the State of the machine

  2. Loading the state of the interrupt

  3. Service the Interrupt

  4. RTI


Note: In-depth explanation of interrupt handling on pages 259-261 of the texbook.


  1. The program below counts the number of zeros in a 16-bit word. Fill in the missing blanks below to make it work.

                .ORIG x3000
                AND   R0, R0, #0
                LD    R1, SIXTEEN
                LD    R2, WORD
    A           BRn   B
                ADD   R0, R0, #1
    B           ADD   R1, R1, #-1
                BRz   C
                ADD   R2, R2, R2
                BR    A
    C           ST    R0, RESULT
                HALT
    
    SIXTEEN     .FILL #16
    WORD        .BLKW #1
    RESULT      .BLKW #1
                .END
  2. After you have the correct answer above, what one instruction can you change (without adding any instructions) that will make the program count the number of ones instead?

    Replace the BRn instruction with a BRzp.


MUL        ST  R7, SAVER7
           ST  R0, SAVER0
           ST  R1, SAVER1
           ST  R2, SAVER2
           ST  R5, SAVER5
           AND R2, R2, #0
           JSR POP
           ADD R1, R0, #0
           JSR POP
           ADD R1, R1, #0
           BRz  DONE
AGAIN      ADD R2, R2, R0
           ADD R1, R1, #-1
           BRp AGAIN
DONE       ADD R0, R2, #0
           JSR PUSH
           LD R7, SAVER7
           LD R0, SAVER0
           LD R1, SAVER1
           LD R2, SAVER2
           LD R5, SAVER5
           RET
        
         .ORIG x3000
         AND R2, R2, #0
         LD R3, NUM
         BRz OUTPUT
         NOT R3, R3
         ADD R3, R3, #1
OUTLOOP  ADD R2, R2, #1
         ADD R0, R2, #0
         AND R1, R1, #0
INLOOP   ADD R1, R1, R2
         ADD R0, R0, #-1
         BRp INLOOP
         ADD R1, R1, R3
         BRn OUTLOOP
OUTPUT   LD R0, ZERO
         ADD R0, R0, R2
         TRAP x21
         HALT
NUM      .BLKW 1
ZERO     .FILL x30
         .END
        

R0 = x0000
PC = x3000

KBSR = x8000
KBDR = x0061
DSR = x8000
DDR = x0031

M[x3009] = xFE00
M[x300A] = xFE02
M[x300B] = xFE04
M[x300C] = xFE06


During the entire instruction cycle, memory is accessed between one and three times (why?). The following table lists two consecutive instructions to be executed on the LC-3. Complete the table with the values that each signal or register takes right after each of the memory accesses performed by the instruction. If an instruction does not require three memory accesses, draw a line accross the unused accesses. To help you get started, we have filled some of the values for you.

PC

Instruction

Access

MAR    

A  

B  

C  

D  

E[15:0]  

F[1]  

F[0]  

MDR    

x3000

LD R0, x9

1

x3000

1

0

0

0

x2009

1

1

x2009

2

x300A

1

0

0

0

xFE02

1

1

xFE02

3

---------

---

---

---

---

---------

------

------

---------

x3001

LDR R0, R0, #0

1

x3001

1

0

0

0

x6000

1

1

x6000

2

xFE02

0

0

0

0

x0061

0

0

x0061

3

---------

---

---

---

---

---------

------

------

---------


              .ORIG x3000
              LD R0, INPUT
              AND R3, R3, #0
              LEA R6, MASKS
              LD R1, COUNT
LOOP          LDR R2, R6, #0
              ADD R3, R3, R3
              AND R5, R0, R2
              BRz SKIP
              ADD R3, R3, #1
              ADD R0, R5, #0
SKIP          ADD R6, R6, #1
              ADD R1, R1, #-1
              BRp LOOP
              ST R3, RESULT
              HALT
COUNT         .FILL #4
MASKS         .FILL 0xFF00
              .FILL 0xF0F0
              .FILL 0xCCCC
              .FILL 0xAAAA
INPUT         .BLKW 1
RESULT        .BLKW 1
              .END      
        

This program identifies the most significant bit position that is set in the value stored at INPUT and stores that bit position in RESULT. For example, if INPUT contained the value 0010 0100 0101 0110, RESULT would contain the value 13 since bit 13 is the most significant bit postition that is a 1.


        
        .ORIG X3000
        LEA R2, C
        LDR R1, R2, #0
        LDI R6, C
        LDR R5, R1, #-3
        ST R5, C
        LDR R5, R1, #-4
        LDR R0, R2, #1
        JSRR R5
        AND R3, R3, #0
        ADD R3, R3, #7
        LEA R4, B       
A       STR R4, R1, #0
        ADD R4, R4, #2
        ADD R1, R1, #1
        ADD R3, R3, #-1
        BRP A
        HALT 
B       ADD R2, R2, #1
        LDR R0, R2, #0
        JSRR R5
        TRAP X29
        ADD R2, R2, #15
        ADD R0, R2, #3
        LD R5, C
        TRAP X2B
        ADD R2, R2, #5
        LDR R0, R2, #0
        JSRR R5
        TRAP X27
        JSRR R5
        JSRR R6
C       .FILL X25
        .STRINGZ "EE306 and tests are awesome"
        .END
        

 

        .ORIG X3000
        LEA R2, C
        LDR R1, R2, #0 ; load x25 into R1
        LDI R6, C      ; loads the starting address of the HALT trap service routine into R6
        LDR R5, R1, #-3 ; loads the starting address of (x25 – 3) trap x22 (puts) into R5
        ST R5, C       ; stores the starting address or puts into C
        LDR R5, R1, #-4 ; loads the starting address of (x25 – 4) trap x21 (out) into r5
        LDR R0, R2, #1 ; loads R0 with the first charater of the stringz “E”
        JSRR R5        ; does the out routine (outputs “E” to the display)
        AND R3, R3, #0 ; clears r3
        ADD R3, R3, #7 ; makes r3 7
        LEA R4, B      ; loads the address of B into r4
 
;NOTE Loop A overwrites the trap vector table, x25 to x2b
; This makes trap x25 – trap x2b point to this program, see label B and below


A       STR R4, R1, #0 ; overwrites the trap vector with the address in R4  
        ADD R4, R4, #2
        ADD R1, R1, #1
        ADD R3, R3, #-1
        BRP A
        HALT           ; What does this do? Trap x25, what is now at memory location x25?
 
;In the following section <- trap xY indicates what address is in memory location Y
B       ADD R2, R2, #1 ; <- trap x25 (makes R2 point to the first character in the stringz “E”)
        LDR R0, R2, #0 ; (loads r0 with the ascci code for “E“)
        JSRR R5         ; <- trap x26 (what is in r5? The starting address of out,outputs “E” on the screen)
        TRAP X29
        ADD R2, R2, #15; <- trap x27 (makes r2 points to the (6 + 15) 21th character of the .stringz
        ADD R0, R2, #3 ; (makes to point to the (21+3) 24th character of the stringz the s in awesome)
        LD R5, C       ; <- trap x28 (LD R5, C loads r5 with the starting address of puts)
        TRAP X2B       
        ADD R2, R2, #5 ; <- trap x29 (“makes R2 point to the 6th character in the .stringz “ “)
        LDR R0, R2, #0 ; (loads r0 with the ascci code for “ “)
        JSRR R5        ; <- trap x2a (outputs a space on the screen)
        TRAP X27
        JSRR R5        ; <- trap x2b (jsrr to puts outputs “some” to the screen)
        JSRR R6        ; remember r6 contains the starting address of trap x25 (halt) so this halts

C       .FILL X25
        .STRINGZ "EE306 and tests are awesome"
        .END