Primary contact for this homework: Pradip Vallathol [pradip16 at cs dot wisc dot edu]
You should 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 binary 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).
See the Computing page for hints on screenshots.
Submission guidelines:
Please adhere to the following submission guidelines strictly. You will be penalized if your submission violates any of these.
- 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: lastname1lastname2.zip or lastname1lastname2.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 - Readme file that contains the names and student IDs for all members of your group and the HALT addresses for Problem 3 and 4
Submission deadline:
You can submit your code for Problem 3 and 4 as many times as you want until the designated end of lecture of your registered section (i.e., 10:45 AM for Section 1 and 3:15 PM for Section 2) on Friday, April 5. After that time we will consider your latest submissions for grading.
Problem 1 (6 points)
Load the below LC-3 program in PennSim, and answer the following questions:
Address | Memory Content | Comment |
0x3000 | 0010 0000 0000 1011 | R0 <- 5 |
0x3001 | 1110 0010 0000 1011 | R1 <- 300D |
0x3002 | 0001 0000 0011 1111 | R0 <- R0 - 1 |
0x3003 | 0000 0100 0000 0111 | BR on Z to x300B |
0x3004 | 0110 0100 0100 0000 | R2 <- M[R1] |
0x3005 | 0101 0110 1010 0001 | R3 <- R2 AND 1 |
0x3006 | 0000 0100 0000 0001 | BR on Z to x3008 |
0x3007 | 0001 0100 1000 0010 | R2 = R2 * 2 |
0x3008 | 0111 0100 0100 0101 | M[R1+5] <- R2 |
0x3009 | 0001 0010 0110 0001 | R1 <- R1 + 1 |
0x300A | 0000 1111 1111 0111 | BR to x3002 |
0x300B | 1111 0000 0010 0101 | HALT |
0x300C | 0000 0000 0000 0101 | Data Value 5 |
0x300D | 0000 0000 0000 0001 | Data Value 1 |
0x300E | 0000 0000 0000 0010 | Data Value 2 |
0x300F | 0000 0000 0000 0101 | Data Value 5 |
0x3010 | 0000 0000 0000 0111 | Data Value 7 |
0x3011 | 0000 0000 0000 1001 | Data Value 9 |
- Fill in the comments column with a summary of what each instruction does?
- How many times does the instruction at address x3004 execute?
4 times
- How many times does the instruction at address x3008 execute?
4 times
- Describe what this program does in 4 sentences or less.
The program reads 5 numbers starting from x300D. If the read number is even it writes it to the location (read address+5), if not it multiplies the number by 2 and writes it to location (read address+5).
Problem 2 (4 points)
The following LC-3 program finds the length of a string. The first character of the string is stored at memory location 0x4000. 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). For example, if character 'a' is stored at memory location x3101, then the value stored at x3101 will be 0x0061. After the execution of the program, the length of the string is stored in register R1. Fill in the missing instructions of the code.
0011 0000 0000 0000 ; Starting address of the code 0x3000
(a)0010 0100 0000 0111 ; Load <DATA> 0x4000 to R2
(b)0101 0010 0110 0000 ; Clear string length counter R1
(c)0110 0110 1000 0000 ; <READ>: Load character at location R2 to R3
(d)0000 0100 0000 0011 ; If NULL character branch to <END>
(e)0001 0100 1010 0001 ; increment R2
0001 0010 0110 0001 ; increment R1
(f)0000 1111 1111 1011 ; branch to <READ>
1111 0000 0010 0101 ; <END>: HALT
0100 0000 0000 0000 ; <DATA>: 0x4000
Problem 3 (12 points)
-
Consider an algorithm which takes the absolute value of a 2's complement number stored in memory location 0x4022 and writes the result to the memory location 0x4023. Represent the algorithm as a flowchart by decomposing it into its basic constructs. (6 Points)
Note: The absolute value of a number is the positive value of that number, disregarding the sign. For example, the absolute value of -5 is 5, and the absolute value of 15 is 15 itself.
-
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 x4022 to be xA3F7. Your screenshot should show the final value in memory locations x4022 and x4023. Turn in the screenshot as a hard copy. (6 Points)
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.
0011 0000 0000 0000 ; Starting address x3000
1010 0100 0000 0101 ; R2 = M[x4022]
0000 0110 0000 0010 ; BR on zp to x3004
1001 0100 1011 1111 ; R2 = NOT (R2)
0001 0100 1010 0001 ; R2 = R2 + 1
1011 0100 0000 0010 ; M[x4023] = R2
1111 0000 0010 0101 ; HALT
0100 0000 0010 0010 ; Data x4022
0100 0000 0010 0011 ; Data x4023
Problem 4 (8 points)
Consider the given algorithm which sets register R1 to 1 if a given number is a power of 2 less than 2048, else sets R1 to 0. The number is stored in the memory location "0x4000".
This is the algorithm you will use:
- Load the given number from the memory location "0x4000" to the Register R2.
- Check if the number is 1. If it is, the number is a power of 2. Set R1 to 1. Go to step 8.
- Put the integer "2" in register R4.
- If R4 is equal to R2, set R1 as 1. Go to step 8.
- If R4 is not equal to R2, multiply R4 by 2 and put the result in register R4.
- If R4 < 2048, go to step 4.
- If R4 >= 2048, clear R1.
- 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 x4000 to be x0400. Your screenshot should show the final value in R1 and the PC of 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.
0011 0000 0000 0000 ; Starting address x3000
1010 0100 0001 0010 ; R2 = M[x4000]
0001 0010 1011 1111 ; R1 = R2 - 1
0000 1010 0000 0010 ; BR on np to NRZ1
0001 0010 0110 0001 ; R1 = R1 + 1
0000 1110 0000 1101 ; BR to HALT
0101 1001 0010 0000 ; NRZ1: R4 = 0
0001 1001 0010 0010 ; R4 = R4 + 2
1001 0111 0011 1111 ; LOOP: R3 = NOT (R4)
0001 0110 1110 0001 ; R3 = R3 + 1
0001 0010 1100 0010 ; R1 = R3 + R2
0000 1010 0000 0010 ; BR on np to NRZ2
0001 0010 0110 0001 ; R1 = R1 + 1
0000 1110 0000 0101 ; BR to HALT
0001 1001 0000 0100 ; NRZ2: R4 = 2 * R4
0010 1010 0000 0101 ; R5 = -2048
0001 1011 0000 0101 ; R5 = R4 + R5
0000 1001 1111 0110 ; BR on n to LOOP
0101 0010 0110 0000 ; R1 = 0
1111 0000 0010 0101 ; HALT
0100 0000 0000 0000 ; Data x4000
1111 1000 0000 0000 ; Data -2048