Test 2 Solutions

The College of Charleston
Computer Science Department
CSCI 250 Computer Org. and Assembly Lang. Prog.
Spring, 2013
G. Pothering
Test 2
Name___________Solutions_______________
Problem
Points
1
24
2
8
3
15
4
9
5
6
6
12
7
12
Total
86
Percentage of 100
Score
CSCI 250 - Spring, 2013
1.
Test 2 Solutions
Page - 1
(24 points; 3 points each) For each of the following, give the LC-3 (mnemonic) machine language statement to do what is described.
Solutions that are correct but unnecessarily cumbersome may not get full credit.
a.
Increment the value of register R5 by 2.
ADD R5, R5, #2
b.
Place a copy the value of register R6 in register R0
ADD R0, R6, #0
c.
or also
AND R0, R6, x1F
Copy the value of variable X to variable Y. Assume register R4 contains the address x2588, that variable X is implemented at the
address x2589 and that variable Y is implemented at address x2590.
First note that x2590 – x2588 = x0008, not x0002
LDR R0, R4, #1
STR R0, R4, #8
d.
Implement the statement A[5] = X, where A is an array of integers whose base address is contained in register R5, and the
variable X is represented by register R0.
STR R0, R5, #5
e.
Implement the statement A[i] = A[i-1], where A is an array of integers whose base address is contained in register R5, and the
variable i is represented by register R0.
ADD R1, R5, R0 ; R1 <- address (A + i)
LDR R2, R1, #-1 ; R2 <- A[i-1] = value at (A+i)–1 = value at A+(i-1)
STR R2, R1, #0 ; A[i] <- R2
f.
Branch to the statement at address x3156 if the values in registers R5 and R6 are different. Assume your code will begin starting
at location x314A
x314A
x314B
x314C
x314D
NOT
ADD
ADD
BR
R0, R6
R0, R0, #1
R1, R5, R1
np #8
; R0 = -R6
; R1 = R5 – R6
; x3156 – (x314D + 1) = x3156 – x314E = x0008 = #11 or x08
[No deduction if you mistook x314A to be the location of the BR instruction and had #11 or x0B as the offset.]
g.
Branch unconditionally to the statement at address x314A from location x3156
BR nzp #-13
h.
; x314A - (x3156+1) = -(x3157 – x314A) = - x000D = #-13 or x1F3
Decrement the value in register R6 by 1 if the value in register R6 is less than the value in register R4; otherwise the value 0 gets
placed in R6. Afterwards copy the new value in register R6 into R0
NOT R0, R6
ADD R0, R0,
ADD R1, R4,
BR zp, #2
ADD R6, R6,
BR nzp #1
AND R6, R6,
ADD R0, R0,
#1
R1
#1
x00
R6
; R0 = -R6
; R1 = R4 – R6
; is R6 >= R4 (or 0 >= R4 – R6) skip next 2 statements
; R4 < R6 so increment R6
; jump over the "else" statement
; R6 >= R0 so R6 = 0
; copy R6 into R0
CSCI 250 - Spring, 2013
2.
Test 2 Solutions
Page - 2
(8 points; 2 points each) If register R0 contain s the value x00E4 and register R1 contains the value xFFFA. What is the value in
register R2 (in hex; -2 overall if you leave your answers in binary) after executing each of the following statements
a.
AND R2, R0, R1
x00E0
b.
since
x00E4 =
x00 E 0100
 xFFFA =  xFF F 1010
x00E0 =
x00 E 0000
ADD R2, R0, # -6
x00DE
since x00E4 + #-6 = x00E4 – x0006 =
-
D(16+4)
x00E 4 = x00D(20)
x000 6
- x000 6
x00DE
Alternatively #-6 = two's complement of x0006 = (xFFF9 + 1) = xFFFA
x0101E4
+ xF F FA
x0 0 DE
c.
AND R2, R1, x0F
Here the sign bit of the 5 bit operand x0F is "0" which we use to extend x0F to 16 bits, giving us x000F. Our answer is
x000A
d.
since
xFFFA
 x000F
x000A
ADD R2, R1, x1F
The key observation here is that the sign bit of 1 in the 5-bit 2s complement operand x1F gets extended to 16 bits, meaning
the actual value added to R1 is xFFFF = #-1. Thus this is the same as ADD R2, R1, #-1 so
R2 ← xFFFA – 1 = xFFF9
Had you not recognized that xFFFF = #-1 you could still get the same result by doing the actual addition
xF1F1 F1A
+ xF F F F
x1F F F 9
3.
(15 points; 3 points each) You are given the following memory locations and their values, and the registers and their values.
Address
x2578
x2579
x257A
x257B
x257C
x257D
x257E
x257F
Value
x2580
x257F
x257E
x257D
x257C
x257B
x257A
x2579
Register
R0
R1
R2
R3
R4
R5
R6
R7
Value
x0000
x1111
xFFFF
x257C
x257F
x2570
x003E
x257A
Give the value of the register R2 after executing each of the following instructions. Assume in each case that the instruction appears
at location x2570. For each part reverting back to the given memory/registers configuration.
CSCI 250 - Spring, 2013
Test 2 Solutions
Page - 3
Note that the first three instructions require a PC-offset-derived address to start, which means we must first calculate this
address as
PC+8 = (x2570 + 1) + 8 = x2579
a. LD
R2, #8
R2 ← the value at address x2579 = x257F
b. LDI R2
#8
R2 ← the value whose address is stored at address x2579 = x2570 + 8 = x2578
the value stored at address x2578 = x2579
c. LEA R2, #8
R2 ← x2579
d. LDR R2, R5, #8
R2 ← the value whose address is (the value in R5) + 8 = x2570 + 8 = x2578
the value stored at address x2578 = x2580
e. LDR R2, R3, x3E
First note that as a 6-bit offset xE3 has a sign bit of 1, which means when extended to 16 bits yields =
xFFFE = #-2
R2 ← the value whose address is (the value in R3) - 2 = x257C – 2 = x257A
the value stored at address x257A = x257E
If you did not recognize that xFFFE = -2, you could still get the same address x257A because
x257C + xFFFE = x257A
4.
(9 points; 3 points each) Describe how the appropriate memory address for a load or store operation is calculated under each of the
following addressing modes. You must express your answer in complete sentences. You may not illustrate your answer with an
example.
a.
PC-relative mode, with offset x004:
The relevant memory address is calculated as (PC + x0004)
b.
indirect mode, with offset x004:
The relevant memory address is the one stored at the address calculated as (PC + x0004)
c.
base+offset mode, with register R5 as the base register and offset x04
The relevant memory address is calculated as (value in value R5 + x0004)
CSCI 250 - Spring, 2013
Test 2 Solutions
Page - 4
5. (6 points) The following program is "supposed" to add the numbers of an array A stored in the four locations starting with location
x3200, leaving the result in register R1.
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
x3108
x3109
x310A
LEA
AND
AND
ADD
BR
ADD
ADD
ADD
ADD
BR
HALT
R2, x0FF
R1, R1, x00
R4, R4, x00
R4, R4, #4
nz, #5
R6, R4, #-1
R5, R2, R6
R1, R1, R5
R4, R4, #-1
nzp, #-6
; R2 <- A = x3200
; sum = 0
;
;
;
;
;
;
;
i = 4
if i <= 0 exit
R6 = i-1
R5 <- A[i-1]
sum = sum + R5
i = i - 1
test loop condition again
Contents of mem locs
x3200
x3107
x3201
x2819
x3202
x0110
x3203
x0210
x3204
x0110
x3205
x1110
x3206
x11B1
Given below is the high-level code on which this code is based (the high-level code is correct!), where the variables sum and i are
implemented in registers R1 and R4 respectively
sum =
i = 4
while
{
sum
i =
}
0
(i > 0)
= sum + A[i-1]
i – 1;
When the code concludes executing, however, register R1 contains xC806 instead of the value x5C40, which is the sum of the first
four values (highlighted in bold above).
a.
Describe why the error arose.
The ADD instruction at address x3106
x3106 ADD
R5, R2, R6
; R5 <- A[i-1]
implies that the value in location A[i-1] is loaded into register R5, but this is not what happens. Instead only the address
associated with A[i-1] was placed in R5.
b.
Rewrite the above machine code to correct the error. Just give the list of instructions, You do not have to write the memory
address associated with each instruction. [Note, if you think the error is simple and you want to modify it in the above code,
you can do so. On the other hand if you feel a more extensive re-write is necessary, you can do it below. You can give
your code in two columns to make it fit in the allowed space if you would like].
x3108
x3109
x310A
x310B
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
x3107
x3108
x3109
x310A
LEA
AND
AND
ADD
BR
ADD
ADD
LDR
ADD
ADD
BR
HALT
R2, x0FF
R1, R1, x00 ;
R4, R4, x00
R4, R4, #4
;
nz, #5 #6
R6, R4, #-1 ;
R5, R2, R6
;
R5, R5, #0
;
R1, R1, R5
;
R4, R4, #-1 ;
nzp, #-6 #-7
sum = 0
i = 4
; if i <= 0 exit
R6 = i-1
R5 <- A[i-1] R5 <- A + (i-1)
R5 <- A[i-1]
sum = sum + R5
i = i - 1
; test loop condition again
CSCI 250 - Spring, 2013
6.
Test 2 Solutions
Page - 5
(12 points) Write LC-3code in mnemonic machine language to implement a shift left operation. In particular it is to store in register
R0, the value in register R1 but with the R1 value's bits shifted to the left a number of positions designated in register R2 (which you
can assume is a number between 0 and 15), and with 0s appended on the right-hand side of the shifted value. This is a common
instruction in ISAs. Note that doubling a number is equivalent to shifting its bits to the left one position and appending a zero
on the right.
You should first give solution to this problem using a high-level language-like syntax.
High-level code:
R1 = R0
while (R2 > 0)
{
R1 = R1 + R1
}
; (same as R1 = 2*R1)
LC-3 Code
ADD R1, R0 #0 ;R1 = R0
BR nz #2
ADD R1, R1, R1
BR nzp #-3
some appropriate instruction
7.
(12 points) Write an LC-3 code in mnemonic machine language that accepts a single input character from the keyboard and displays
the message Yes on the console if this character's ASCII matched the value in register R3 and No if they do not match. Assume your
program begins at location x3000 and that null-character terminated string "Yes" is stored starting at location x3020 and the nullcharacter terminated string "No" is stored starting at location .x3024. End your code with a HALT instruction. You should first give
solution to this problem using a high-level language-like syntax.
High-level code:
R0 = keyboard value
if (R0 == R3)
{
print "Yes"
}
else
{
print "No"
}
LC-3 Code: Here in the if-else clauses, we only place in R0 the address of the string to be printed, and then subsequently do
the printing with PUTS
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
IN
NOT R2, R0
AND R1, R2, R3
; will be x0000 if R2 and R3 have the same value
BR np #2
; if R2 != R3 skip next two statements
LEA R0, #27
; R0 <- x3020. Note: x3020 – x3005 = x001B = #27
BR nzp, #1
; skip over "else" statement
LEA R0, #29
; R0 <- x3024 Note: x3024 – x3007 = x001D = #29
PUTS
; PUTS required address of string in R0
CSCI 250 - Spring, 2013
Test 2 Solutions
Alternatively we can do the printing in the if-else clauses
x3000
x3001
x3002
x3003
x3004
x3005
x3006
x3007
x3008
IN
NOT R2, R0
AND R1, R2, R3
BR np #3
LEA R0, #27
PUTS
BR nzp, #2
LEA R0, #29
PUTS
; will be x0000 if R2 and R3 have the same value
; if R2 != R3 skip next three statements
; R0 <- x3020. Note: x3020 – x3005 = x001B = #27
; skip over "else" statement
; R0 <- x3024 Note: x3024 – x3007 = x001D = #29
; PUTS required address of string in R0
Page - 6