|
Homework 6 // Due at lecture Monday, Nov 7
Primary contact for this homework: Rebecca Lam [rjlam@cs.wisc.edu]
You must do this homework in groups of two. Please write the full name and the student id of each member on every pages and staple multiple pages together. Only turn in ONE copy of homework per group.
Problem 1 (4 points)
The LC-3 does not have a logical XOR instruction, but we can write a sequence of instructions to implement one. The following instructions perform the XOR function on the values stored in R1 and R2 and store the result in R3. Fill in the missing two instructions and write comments on what the instructions do.
*Hint: DeMorgan's Law
| 0x987F |
NOT R4, R1 |
R4=NOT(A) |
| 0x9ABF |
NOT R5, R2 |
R5=NOT(B) |
| 0x5A45 |
AND R5, R1, R5 |
R5=A AND NOT(B) |
| 0x5884 |
AND R4, R2, R4 |
R4=NOT(A) AND B |
| 0x9B7F |
NOT R5, R5 |
R5=NOT(A AND NOT(B)) |
| 0x993F |
NOT R4, R4 |
R4=NOT(NOT(A) AND B) |
| 0x5905 |
AND R4, R4, R5 |
R4=NOT(A AND NOT(B)) AND NOT(NOT(A) AND B) |
| 0x973F |
NOT R3, R4 |
R3=NOT(NOT(A AND NOT(B)) AND NOT(NOT(A) AND B)) = [A AND NOT(B)] OR [NOT(A) AND B] |
Problem 2 (4 points)
Answer the following questions for the following snippet of code. Assume R0=0x1000, R1=0x0100, R2=0x0010, R3=0x0001 before the instruction at 0x3000 is executed.
| Address |
Instruction |
| 0x3000 |
0x1000 add R0, R0, R0 |
| 0x3001 |
0x0201 brp #1 |
| 0x3002 |
0x5483 and R2, R2, R3 |
| 0x3003 |
0x1242 add R1, R1, R2 |
| 0x3004 |
0x05FB brz #-5 |
- What is the value of the PC after the instruction at 0x3001 is executed?
0x3003
- What is the value of the PC after the instruction at 0x3004 is executed?
0x3005
Problem 3 (10 points)
Write a program in LC-3 binary code that searches for odd numbers in 200 memory locations starting from 0x4000. Set R0 to the number of found odd numbers. Your solution should use a looping construct. Comment each line of code and submit the binary code as a textfile to the dropbox. Print out a screenshot of your code with the PC at the HALT instruction.
0011 0000 0000 0000; 0x3000; .ORIG x3000
0010 0110 0000 1010; 0x260A; ld R3, numLoop ;load value 200 into R3
0101 0000 0010 0000; 0x5020; and R0, R0, #0 ;clear R0, our result register
0010 0010 0000 1001; 0x2209; ld R1, addr ;load starting address into R1
0110 0100 0100 0000; 0x6440; loop ldr R2, R1, #0 ;load value at R1
0101 0100 1010 0001; 0x54A1; and R2, R2, 0x01 ;check LSB
0000 0100 0000 0001; 0x0401; brz dec1 ;if LSB is 0 then it's even. Skip to dec1
0001 0000 0010 0001; 0x1021; add R0, R0, #1 ;else, increment result
0001 0010 0110 0001; 0x1261; dec1 add R1, R1, #1 ;increment R1, our address pointer
0001 0110 1111 1111; 0x16FF; add R3, R3, #-1 ;decrement R3, our counter
0000 0011 1111 1001; 0x03F9; brp loop ;loop if R3 > 0
1111 0000 0010 0101; 0xF025; halt
0000 0000 1100 1000; 0x00C8; numLoop .FILL #200 ; Counter
0100 0000 0000 0000; 0x4000; addr .FILL 0x4000 ; Starting address
Problem 4 (12 points)
The LC-3 has no left shift instruction. The algorithm should take in a 16-bit value at memory location M1 that contains the number that will have its bits shifted and a value at M2 that contains the number of bits to shift and store the 16-bit result in M3. For instance, suppose M1 = 0x0010 and M2 = 0x0004. After execution of the program, M3 = 0x0100. Note that the lowest bits will be filled with zeros.
Show the process of designing the algorithm:
- (4 points) Write the pseudo code for the left shift algorithm as described above.
Load value at M1 into R1
Load value at M2 into R2
Add R1 to R1 (Doubling = left shift 1)
Decrement R2
If p flag, go back two steps
Else Store R1 into M3
- (6 points) Write a program using LC-3 binary code that implements your algorithm, with the following as your memory locations:
M1: 0x5000
M2: 0x5001
M3: 0x6000
Write comments for each line of code explaining what it does. Submit the binary code as a text file to the dropbox.
0011 0000 0000 0000; 0x3000; .ORIG x3000
1010 0010 0000 0110; 0xA206; ldi R1, M1 Load value at M1
1010 0100 0000 0110; 0xA406; ldi R2, M2 Load value at M2
0001 0010 0100 0001; 0x1241; loop add R1, R1, R1 Left shift 1 bit
0001 0100 1011 1111; 0x14BF; add R2, R2, #-1 Decrement R2
0000 0011 1111 1101; 0x03FD; brp loop Branch if positive to loop
1011 0010 0000 0011; 0xB203; sti R1, M3 Store value at M3
1111 0000 0010 0101; 0xF025; halt
0101 0000 0000 0000; 0x5000; M1 x5000 Pointer to 0x5000
0101 0000 0000 0001; 0x5001; M2 x5001 Pointer to 0x5001
0110 0000 0000 0000; 0x6000; M3 x6000 Pointer to 0x6000
- (2 points) Print a screenshot of your code in PennSim with the PC at the HALT instruction. Show the result of M1 = 0x1234, M2=0x0005
Result should be 0x4680
|