Primary contact for this homework: Pradip Vallathol [pradip16 at cs dot wisc dot edu]
You must do this homework in groups of two. Please write the full name and the student id of each member on every page and 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 them as hard copies, along with Problem 1 and 2. Your programs should start at address x3000 and end with a HALT instruction (0xF025).
Submission guidelines:
- For your your code submit only one archive file (*.zip or *.tar.gz) per group to the folder homework6.
- Name the file with the following convention: hw6.zip or hw6.tar.gz
- Your archive file should contain the following (The files MUST be named exactly like this):
- hw6_p3.txt - Binary code for problem 3
- hw6_p4.txt - Binary code for problem 4
- README.txt - A file that contains the full names of all members of your group. For example, if the TAs were submitting this homework, the README.txt file will look like this:
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.
Submission Deadline:
You can submit your code for problem 3 and 4 as many times as you want until the beginning of lecture (i.e., 2:25 PM) on Monday, November 11. After that time we will consider your latest submissions for grading.
See the Computing page for hints on screenshots.
Problem 1 (6 points)
Load the below LC-3 program in PennSim, and answer the following questions:
Address | Memory Content | Comment |
0x3000 | 0101 0000 0010 0000 | R0 = 0 |
0x3001 | 1110 0010 0000 1011 | R1 = x300D |
0x3002 | 0101 0100 1010 0000 | R2 = 0 |
0x3003 | 0001 0100 1010 0100 | R2 = R2 + 4 |
0x3004 | 0000 0100 0000 0111 | Brz x300C |
0x3005 | 0110 0110 0100 0000 | R3 = M[R1] |
0x3006 | 0101 1000 1110 0001 | R4 = R3 AND x0001 |
0x3007 | 0000 0100 0000 0001 | BRz x3009 |
0x3008 | 0001 0000 0000 0011 | R0 = R0 + R3 |
0x3009 | 0001 0010 0110 0001 | R1 = R1 + 1 |
0x300A | 0001 010 01011 1111 | R2 = R2 - 1 |
0x300B | 0000 1111 1111 1000 | BR x3004 |
0x300C | 1111 0000 0010 0101 | HALT |
0x300D | 0000 0000 0000 0001 | Data Value 1 |
0x300E | 0000 0000 0000 0010 | Data Value 2 |
0x300F | 0000 0000 0000 0011 | Data Value 3 |
0x3010 | 0000 0000 0000 0101 | Data Value 5 |
0x3011 | 0000 0000 0000 0111 | Data Value 7 |
- (3 Points) Fill in the comments column with a summary of what each instruction does?
- (1 Point) How many times does the instruction at address x3004 execute?
5 times
- (1 Point) What is the value of R0 before the instruction at x300C is executed?
9
- (1 Point) Describe what this program does in 1-2 sentences.
The program adds all the odd numbers from memory locations x300D to x3010 and stores the sum in R0.
Problem 2 (6 points)
The following LC-3 program does a left shift operation. It takes a 16-bit value at memory location "x3800" that contains the number that will have its bits shifted and a value at memory location "x3801" that contains the number of bits to shift and store the 16-bit result in memory location "x3802". For example, if M[x3800] = 0x0102 and M[x3801] = 0x0003, then after the program completes M[x3802] = 0x0810.
- (4 Points) Fill in the missing instructions of the code.
Suggestion: Verify your solution by executing it in PennSim.
0011 0000 0000 0000 ; Program starts at x3000
(a)1010 0010 0000 0110 ; Load value at x3800 into R1
(b)1010 0100 0000 0110 ; Load value at x3801 into R2
(c)0001 0010 0100 0001 ; LOOP: Left shift R1 by 1 bit
(d)0001 0100 1011 1111 ; Decrement R2
(e)0000 0011 1111 1101 ; Branch if positive to LOOP
(f)1011 0010 0000 0011 ; Store the value of R1 to x3802
1111 0000 0010 0101 ; HALT
0011 1000 0000 0000 ; DATA1: x3800
0011 1000 0000 0001 ; DATA2: x3801
0011 1000 0000 0010 ; DATA3: x3802
- (2 Points) Suppose the above program had to execute correctly (produce the correct result in memory location x3082 after the execution of the program) for the following constraints on the input (values at memory locations x3800 and x3801 before execution of the program):
- The memory location x3800 can have any value.
- The integer value at x3801 has to be greater than or equal to 0
Given the constraints, there is an error in the above code. Identify the error by giving an example input for which this program will give a wrong result (final value at x3802).
If the input at x3081 is 0, the code will give an incorrect output.
Problem 3 (8 points)
Consider the following algorithm which sets register R3 to 1 if a given number is a sum of consecutive integers starting from 1. 6 is an example of such a number since 6 = 1 + 2 + 3. The number is stored in the memory location "0x3800".
This is the algorithm you will use:
- Load the given number from the memory location "0x3800" to the Register R2.
- Check if the number is 0 or negative. If it is, set R3 to 0. Go to step 9.
- Clear R0 and R1
- Set the value of R0 to 1
- Add R0 to R1 and put the result in R1
- If R2 < R1, set R3 to 0. Go to step 9
- If R2 == R1, set R3 to 1. Go to step 9
- Increment R0. Go to step 5
- Halt
Convert the above algorithm to an LC-3 program. Submit the binary code as a .txt file to the dropbox. Print out a screenshot of your code running in PennSim. For the screenshot, make the value in memory location x3800 to be x0000. Your screenshot should show the final value in R3 at the HALT instruction. Turn in the screenshot as a hard copy.
To test your code use the script, found here. "README.txt" in the zip package contains instructions on how to run the tests and verify your program.
Sample Solution: hw6_p3.txt
Problem 4 (10 points)
- (4 Points)
Consider an algorithm which counts the number of times the ASCII characters 'a' and 'A' occur in a string that is stored starting at location x3800. The count is stored in register R3. Represent the algorithm as a flowchart by decomposing it into its basic constructs.
Note: The first character of the string is stored at memory location 0x3800. The last character of the string is the NULL character (having ASCII value 0x00). One character is stored in one memory location (the lower order 8 bits represent the ASCII character and higher order 8 bits are 0).
- (6 Points)
Convert the above algorithm to an LC-3 program. Submit the binary code as a .txt file to the dropbox. Print out a screenshot of your code running in PennSim. Your screenshot should show the final value in register R3 before the program executes the HALT instruction (keep a breakpoint at HALT, and take a screenshot when the program hits the breakpoint). Turn in the screenshot as a hard copy.
To test your code use the script, found here. "README.txt" in the zip package contains instructions on how to run the tests and verify your program.
Sample Solution: hw6_p4.txt