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.
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 (
STI) is greater than the number of executed load instructions (
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.
A large number of LDI instructions (two read accesses) and STI instructions (one read access and one write access) could account for this discrepancy.
(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 one instruction each at (a), (b), and (c) that will complete the program. Note: The memory location immediately following each string contains x0000.
|LD R1, FIRST|
|LD R2, SECOND|
|AND R0, R0, #0|
|LOOP||LDR R3, R1, #0||; (a)|
|LDR R4, R2, #0|
|ADD R1, R1, #1|
|ADD R2, R2, #1|
|NOT R4, R4||; (b)|
|ADD R4, R4, #1||; (c)|
|ADD R3, R3, R4|
|AND R5, R5, #0|
|NEXT||AND R5, R5, #0|
|ADD R5, R5, #1|
|FIRST  ||.FILL x4000|
|SECOND  ||.FILL x4100|
OUTtrap handler so it would not check the
DSRbefore 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?
Some of the characters written to the DDR weren't being output to the screen.
Bob also rewrote the handler for
GETC, but when he typed
ABCD into the keyboard, the following values were input:
What did Bob do wrong?
The handler didn't check the KBSR before inputting the character.
Stack is a storing mechanism. The concept of a stack is the specification of how it is to be accessed. That is, the defining ingredient of the stack is that the last thing you stored in it is the first things you remove from it. LAST IN FIRST OUT (LIFO)
Two Implementations and differences between them:
1. Stack in hardware: Stack pointer points to the top of the stack and data entries move during push or pop operations. (ex. Coin holder)
2. Stack in memory: Stack pointer points to the stack and moves during push or pop operations. Data entries do not move.
PUSH V PUSH W PUSH X PUSH Y MUL ADD PUSH Z SUB DIV POP U
Note: Possible answer in case of assuming top of stack as first operand in SUB and DIV instructions:PUSH A PUSH B PUSH C SUB PUSH D ADD MUL PUSH A PUSH C ADD DIV POP E
PUSH A PUSH C ADD PUSH A PUSH D PUSH C PUSH B SUB ADD MUL DIV POP E pre>
Moved to Problem Set 6 (updated 10/27/15) 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
instruction at the location with label
Assume that the user only calls this "new"
TRAP x22 once. What is wrong with this
Now add the necessary instructions so the
TRAP handler executes properly.
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 ST R7,SAVER7 ST R1,SAVER1 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 LD R7,SAVER7 LD R1,SAVER1 RET SAVER0 .BLKW #1 SAVER7 .BLKW #1 SAVER1 .BLKW #1 .END
TRAP service routines can be implemented in the LC-3? Why?
Why must a
RET instruction be used to return from a
TRAP routine? Why won't
BR) instruction work instead?
How many accesses to memory are made during the processing of a
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
Write a subroutine
CLEAR in LC-3 assembly language that clears
a bit in
R0 using the table above. The index of the bit
to clear is specified in
are inputs to the subroutine.
CLEAR: ST R2,TEMP LEA R2,MASKS ADD R2,R1,R2 LDR R2,R2,#0 NOT R2,R2 AND R0,R2,R0 LD R2,TEMP RET TEMP: .BLKW #1
Write a similar subroutine
SET that sets the specified bit instead of clearing it.
Hint: You should remember to save and restore any registers your subroutine
uses (the "callee save" convention). Use the
RET instruction as
the last instruction in your subroutine (
R7 contains the address
of where in the caller to return to.)
SET: ST R2,TEMP LEA R2,MASKS ADD R2,R1,R2 LDR R2,R2,#0 NOT R2,R2 NOT R0,R0 AND R0,R2,R0 NOT R0,R0 LD R2, TEMP RET TEMP: .BLKW #1
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 multiplies four values, and returns the product. The mult_all subroutine assumes the source operands 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. Store your result at location x7000. Assume there is at least one value in the array(i.e., M[x6000] is greater than 0).
Hint: Feel free to include in your program
PTR .FILL x6001 CNT .FILL x6000
.ORIG x3000 LD R5, PTR LDI R6, CNT BRz DONEz ;checks if more numbers to multiply(CNT=0) MORE LDR R1,R5,#0 ADD R5,R5,#1 ADD R6,R6,#-1 BRz DONE1 ;continues if more numbers to multiply LDR R2,R5,#0 ADD R5,R5,#1 ADD R6,R6,#-1 BRz DONE2 ;continues if more numbers to multiply LDR R3,R5,#0 ADD R5,R5,#1 ADD R6,R6,#-1 BRz DONE3 ;continues if more numbers to multiply LDR R4,R5,#0 ADD R5,R5,#1 ADD R6,R6,#-1 BRnzp READY ;CNT is multiple of 4 DONEz AND R0,R0,#0 ADD R0,R0,#1 BRnzp END ;(CNT = 4x+1) multiplies R1 by three 1's DONE1 AND R2,R2,#0 ADD R2,R2,#1 ;R2 = 1 ADD R3,R2,#0 ;R3 = 1 ADD R4,R2,#0 ;R4 = 1 BRnzp READY ;(CNT = 4x+2) multiplies R1,R2 by two 1's DONE2 AND R3,R3,#0 ADD R3,R3,#1 ;R3 = 1 ADD R4,R4,#0 ;R4 = 1 BRnzp READY ; ;(CNT = 4x+3) multiplies R1,R2,R3 by 1 DONE3 AND R4,R4,#0 ADD R4,R4,#1 READY JSR mult_all ADD R6,R6,#0 BRz END ;checks CNT ; ;if CNT is not zero takes R0 from subroutine and puts back into memory to multiply more numbers ADD R5,R5,#-1 STR R0,R5,#0 ;add one back to CNT because R0 is back into memory ADD R6,R6,#1 BRnzp MORE ; ;store result of multiplication in memory location RESULT END ST R0,RESULT HALT RESULT .BLKW 1 mult_all ... ;multiples R1,R2,R3,R4 and stores result in R0 ... ... RET PTR .FILL x6001 CNT .FILL x6000 .END
.ORIG x3000 JSR A OUT ;TRAP x21 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
.ORIG x3000 LD R3, A STI R3, KBSR AGAIN LD R0, B TRAP x21 BRnzp AGAIN A .FILL x4000 B .FILL x0032 KBSR .FILL xFE00 .END
.ORIG x1000 LDI R0, KBDR TRAP x21 TRAP x21 RTI KBDR .FILL xFE02 .END
Letter ABCDEFGHI will be displayed on console.
.ORIG x3000 LD R0, ASCII LD R1, NEG AGAIN LDI R2, DSR BRzp AGAIN STI R0, DDR ADD R0, R0, #1 ADD R2, R0, R1 BRnp AGAIN HALT ASCII .FILL x0041 NEG .FILL xFFB6 DSR .FILL xFE04 DDR .FILL xFE06 .END
.ORIG x3000 ST R0, x3007 LEA R0, LABEL TRAP x22 TRAP x25 LABEL .STRINGZ "FUNKY" LABEL2 .STRINGZ "HELLO WORLD" .END
Address Contents x4000 x4016 x4001 x4003 x4002 x4008 x4003 x004D x4004 x0061 x4005 x0072 x4006 x0063 x4007 x0000 x4008 x0039 x4009 x0030 x400A x0000 x400B x0000 x400C x4019 x400D x401E x400E x004A x400F x0061 x4010 x0063 x4011 x006B x4012 x0000 x4013 x0031 x4014 x0038 x4015 x0000 x4016 x400B x4017 x400E X4018 x4013 x4019 x004D x401A x0069 x401B x006B x401C x0065 x401D x0000 x401E x0037 x401F x0036 x4020 x0000
.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 ; note: BR = BRnzp C ST R0, RESULT HALT SIXTEEN .FILL #16 WORD .BLKW #1 RESULT .BLKW #1 .END
; Main Program ; .ORIG x3000 ..... ..... JSR F ..... ..... HALT ; R3 and R4 are input. ; Modifies R0, R3, R4, R5, and R6 ; R0 is the output ; F JSR SaveRegisters .... .... .... JSR RestoreRegisters RET .END
.ORIG x4000 LD R2,BOBO LD R3,SAM AGAIN ADD R3,R3,R2 ADD R2,R2,#-1 BRnzp SAM BOBO .STRINGZ "Why are you asking me this?" SAM BRnp AGAIN TRAP x25 .BLKW 5 JOE .FILL x7777 .END