Homework 6

Name: __________________________
Section: _________________________
NetID: __________________________
CampusID#: ______________________
CS/ECE 252 Introduction to Computer Engineering
Fall 2016 Sections 2, 5
Instructors: Guri Sohi, Kai Zhao
TAs: Annie Lin, Daniel Griffin, Mohit, Neha Mittal, Yuzhe Ma
URL: http://www.cs.wisc.edu/~sohi/cs252/Fall2016/
Homework 6 [Due at lecture on Wed, Nov 9]
Primary contact for this homework: Yuzhe Ma [ma234 at wisc dot edu]
You must do this homework alone. Please staple multiple pages together.
Important Notes:
Problems 3 and 4 ask you to submit your binary code as a text file (*.txt) to the Learn@UW
dropbox. Additionally, they ask you turn in a printed screenshot of PennSim. Submit these
printed screenshots as hard copies, along with Problem 1 and 2.
Submission guidelines:
a. For your your code submit only one archive file (*.zip) to the folder homework6.
b. Name the file with the following convention: NetID_hw6.zip
c. Your archive file should contain the following (The files MUST be named exactly
like this):
a. hw6_p3.txt - Binary code for problem 3
b. hw6_p4.txt - Binary code for problem 4
It is important that you follow the above submission guidelines since your code
submission will be graded automatically. Any submission that deviates from the
above guidelines will be penalized.
You can submit your code for problem 3 and 4 as many times as you want until the
beginning of lecture (i.e., 9:55 AM) on Wednesday, November 9. After that time we will
consider your latest submissions for grading.
Problem 1 (4 points)
Shown below are the contents of registers before and after the LC-3 instruction at
location x3450 is executed. If the instruction is BRp, then what is the hexadecimal
representation of the instruction? What if the instruction is JMP (also in hexadecimal
form)?
Before
After
R0
x0A32
x0A32
R1
x0019
x0019
R2
x0253
x0253
R3
xFF12
xFF12
R4
xF1F1
xF1F1
R5
x114A
x114A
R6
x00B1
x00B1
R7
x3481
x3481
PC
x3450
x3481
N
0
0
Z
0
0
P
1
1
Problem 2 (6 points)
Suppose R1 contains the value n(n>=0) initially. Consider the following program for
calculating 2^n and storing the result in R5.
Address
Instruction
x3012
0101000000100000
x3013
0001000001000000
x3014
0101010010100000
x3015
0001010010100010
x3016
0001000000111111
x3017
0000010000000010
x3018
0001010010000010
x3019
0000111111111100
x301A
0101101101100000
x301B
0001101101000010
x301C
1111000000100101
a) Suppose n=3, what are the values in R0, R1, R2, and R5 respectively after the
program terminates?
b) Does the program work for all values of n>=0? If not, specify the value n for
which the program doesn’t work and explain why the program fails in that case?
c) Fill in the blank in the following modified program such that it works for all n>=0.
(updated)
Address
Instruction
x3012
0101000000100000
x3013
0101010010100000
x3014
x3015
0001000001000000
x3016
0000010000000011
x3017
0001010010000010
x3018
x3019
x301A
0101101101100000
x301B
0001101101000010
x301C
1111000000100101
Problem 3 (8 points)
The LC-3 does not have subtract nor absoluteValue instructions. But it has three
operate instructions: ADD, AND, NOT which can be used to perform these operations.
An absoluteValue operation outputs the absolute value of a number.
For example: The absolute value of -5 is 5, and the absolute value of 3 is 3.
Write a LC-3 program that subtracts the absolute value of an integer at memory location
0x2000 from the absolute value of an integer at memory location 0x3000. Place the
result is at the memory location 0x2000. You do not need to worry about the absolute
value of -2^15 because +2^15 cannot be represented in LC-3. Submit the binary code
as a .txt file to the dropbox. Turn in the screenshot as a hard copy. For the
screenshot, make the value in memory location 0x2000 to be 0x03e5 and the value in
memory location 0x3000 to be 0xf1c0. Your screenshot should show the PC at the
HALT instruction and show the final value in memory location x2000 (All of your code
does not have to be completely visible).
Problem 4 (10 points)
Come up with an algorithm that searches for the minimum integer in 100 memory
locations starting from 0x5a00, and sets R7 to the minimum integer. Your solution
should use a looping construct. Note: Both positive and negative integers are allowed.
a) Show the algorithm as a flowchart by decomposing it into its basic constructs. (4
points)
b) Convert the above algorithm to an LC-3 program. Write the program in LC-3
binary code. Comment each line of code and submit the binary code as a text
file to the dropbox. The program should start at memory address x4000. Print
out a screenshot of your code with the PC at the HALT instruction. Turn in the
screenshot as a hard copy. (6 points)