Assignment 3 Solutions

CSCI 250 Computer Organization and Assembly Language Programming
Spring 2013
G. Pothering
Assignment 3 – Solutions
Note: No final answer, even if correct, will receive any credit (that is, credit = 0) unless
there is an appropriate amount of work shown that leads to the answer.
1. (0 points) Page 146 problem 5.9 – I should not have included this, and so will not count it.
2. (3 points) Page 146, problem 5.23: Suppose the following LC-3 program is loaded into
memory starting at location x30FF. If the program is executed, what is the value in R2 at the
end of execution?
1110
0110
1111
0001
0001
0010
0100
0000
0100
0100
0000
0100
0010
0100
1000
0001
0010
0101
0001
0010
=
=
=
=
=
1110
0110
1111
0001
0001
001 0 0000 0001
010 001 000010
0000 0010 0101
010 001 000 001
010 010 000 010
=
=
=
=
=
LEA R1 x001
LDR R2, R1, #2
TRAP x25
ADD R2, R1, R1
ADD R2, R2, R2
Results of program execution
x30FF LEA R1, x001
x3100 LDR R2, R1, x2
x3101 TRAP x25
x3102 ADD R2, R1, R1
x3103 ADD R2, R2, R2
R1 <-- x3100 +x0001 = x3101
R2 <-- value in memory location (R1 = x3101 + x2) =
value at x3103 = x1482
HALT
Note, for those who did not stop at the HALT instruction, but continued on to the last
"instruction" the results would be the following
x30FF LEA R1, x001
x3100 LDR R2, R1, x2
x3101 TRAP x25
x3102 ADD R2, R1, R1
x3103 ADD R2, R2, R2
3.
R1 <-- x3100
R2 <-- value
value
HALT
R2 <-- x3101
R2 <-- x6202
+x0001 = x3101
in memory location (R1 = x3101 + x2) =
at x3103 = x1482
+ x3101 = x6202
+ x6202 = xC404
(4 points) Page 146, problem 5.14. Give your answer in hex. Recall DeMorgan's law:
NOT (x AND y ) = NOT x OR NOT y
The LC-3 does not have an OR instruction; however we can write a sequence of instructions
to implement the OR operation. Fill in the two missing instructions -- (2) and (4) -- so the
instruction sequence performs OR R3, R1, R2
Solution: By DeMorgan's Law NOT(x AND y) = (NOT x) OR (NOT y), whence
NOT ((NOT x) AND (NOT y)) = NOT(NOT x) AND NOT(NOT y) = x OR y
We use this in implementing the solution below
(1)
(2)
(3)
(4)
1001
1001
0101
1001
100
101
110
011
001
010
100
110
111111
111111
000 101
111111
NOT
NOT
AND
NOT
R4,
R5,
R6,
R3,
R1
R2
R4, R5
R6
Translate the following high-level statements into LC-3 mnemonic machine language. In all
cases assume that:
 variables i and j are integer variables associated with the registers R0 and R1 respectively
 the elements of the array A are allocated sequentially in memory starting at address
x3018.
 the first instruction of your code is stored in memory at address x3000.
4. (2 points) A[2] = j
x3000
x3001
LEA R2 x017
STR R1, R2, #2
R2 contains address x3001 + x017 = x3018
5. (3 points) j = A[2] AND A[3]
x3000
x3001
x3002
x3003
LEA
LDR
LDR
AND
R2 x017
R3, R2, #2
R4, R2, #3
R1, R3, R4
R2 contains address x3001 + x017 = x3018
6. (2 points) j = A[2] - 1
x3000
x3001
x3002
LEA R2 x017
LDR R4, R2, #2
ADD R1, R4 #-1
R2 contains address x3001 + x017 = x3018
7. (3 points) j = A[i-1]
x3000
x3001
x3002
LEA R2 x017
ADD R4, R2, R3
LDR R1, R4, #-1
R2 contains address x3001 + x017 = x3018
R4 contains address A + i
R1 gets the value at address (A+i) - 1
8. (3 points) j = A[i] - 1
x3000
x3001
x3002
x3003
9.
LEA
ADD
LDR
ADD
R2 x017
R3, R2, R0
R4, R3, #0
R1, R4, #-1
R2 contains address x3001 + x017 = x3018
(5 points) A[i + j] = A[i] + A[j]
x3000
LEA R2 x017
R2 contains address x3001 + x017 = x3018
x3001
x3002
x3003
x3004
x3005
x3006
x3007
ADD
LDR
ADD
LDR
ADD
ADD
STR
R3,
R4,
R3,
R5,
R5,
R3,
R5,
R2,
R3,
R2,
R3,
R4,
R3,
R3,
R0
#0
R1
#0
R5
R0
#0
R4 contains A[i]
R5 contains A[j]
R5 contains A[i] + A[j]
R3 has address (A + j) + i
A[i+j] = A[i] + A[j]