CS ECE 252 Introduction to Computer Engineering Computer Sciences Department University of Wisconsin Madison Midterm III Professor David Wood Friday April 10th 2009 This exam is closed book There is to be nothing used during the exam There are 7 pages in this exam not counting this cover sheet See the back page for the LC 3 instruction set Last Name First Name Section Student ID Question Points 1 10 2 10 3 12 4 20 5 24 6 24 Total 100 Score 1 10 points Machine Language to Pseudo Code Translate the binary value to pseudo code to and give the value of R3 in binary after execution of the code fragment The first line is filled in for you Binary Value Pseudo Code 0101 0100 1010 0000 R2 R2 AND 0 0001 0100 1000 0010 1001 0110 1011 1111 0010 0010 0000 0000 1100 0000 0100 0000 R3 Solution Binary Value Pseudo Code 0101 0100 1010 0000 R2 R2 AND 0 0001 0100 1000 0010 R2 R2 AND R2 1001 0110 1011 1111 R3 NOT R2 0010 0010 0000 0000 R1 MEM PC 0 1100 0000 0100 0000 PC R1 R3 1111 1111 1111 1111 2 10 points Pseudo Code to Machine Language Translate the pseudo code to binary machine language and give the value of R4 in binary after execution of the code fragment The first line is filled in for you Binary Value 0101011011100000 Pseudo Code R3 R3 AND 0 R4 R3 11 R5 PC 1 MEM PC 5 R4 R4 R4 R3 R4 Page 1 of 7 Solution Binary Value Pseudo Code 0101011011100000 R3 R3 AND 0 0001100011101011 R4 R3 11 1110101111111111 R5 PC 1 0011100000000101 MEM PC 5 R4 0001100011000100 R4 R4 R3 R4 0000 0000 0000 1011 Page 2 of 7 3 12 points Note that there is no OR instruction in the LC 3 ISA Complete the code so that the following 4 instruction sequence stores the result of R1 OR R2 in the register R3 1 2 1001 1000 0111 1111 3 0101 1101 0000 0101 4 Solution 1 2 3 4 1001 1001 0101 1001 1010 1000 1101 0111 1011 0111 0000 1011 1111 1111 0101 1111 R5 R4 R6 R3 NOT R2 NOT R1 R4 AND R5 NOT R6 Page 3 of 7 4 20 points Addressing Let R0 R1 R2 and R3 be initialized to 0 The PC initially has value x3000 What are the values of R0 R1 R2 and R3 when we terminate Address x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C x300D x300E x300F Value x2009 xA20B xC000 x6448 x6647 xF025 x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 Translation R0 MEM PC 9 R1 MEM MEM PC 11 PC R0 R2 MEM R1 8 R3 MEM R1 7 HALT MEM PC 0 R0 MEM PC 1 R0 MEM PC 2 R0 MEM PC 3 R0 MEM PC 4 R0 MEM PC 5 R0 MEM PC 6 R0 MEM PC 7 R0 MEM PC 8 R0 MEM PC 9 R0 Solution x3000 LD R0 9 R0 MEM x3000 1 9 x3004 x3001 LDI R1 11 R1 MEM MEM x3001 1 B MEM x3007 x3001 x3002 JMP R0 x3003 LDR R2 R1 8 R2 MEM R1 8 x3003 never run x3004 LDR R3 R1 7 R3 MEM R1 7 x3002 x3005 HALT x3006 x3000 x3007 x3001 x3008 x3002 x3009 x3003 x300A x3004 x300B x3005 x300C x3006 x300D x3007 x300E x3008 x300F x3009 Therefore R0 x3004 R1 x3000 R2 0 R3 x3003 Page 4 of 7 5 24 points Iteration Everytime a register is written write the new value update the table Updating the table consists of finding the registers row and writing in hex the value that is written The first 3 entries to the table have been written for you corresponding the the first 3 lines of the program Complete the partially filled table to match the execution of the program Address x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C 0101 0001 0101 0101 0001 0001 0001 0001 0000 0001 0000 0001 1111 Value 0110 1110 0110 1110 1011 0110 1001 0010 1001 0010 1011 0110 1011 0100 1001 0011 0011 1111 0110 1111 0011 1111 1101 0110 0000 0010 0000 0010 0000 0000 0010 0011 0011 1111 1101 1111 1000 0101 0101 Translation R3 R3 AND 0 R3 R3 2 R5 R5 AND 0 R4 R4 AND 0 R4 R4 2 R5 R5 3 R5 R5 R3 R4 R4 1 BRp 3 R3 R3 1 BRp 8 R6 R5 5 HALT Register 1st Val 2nd Val 3rd Val 4th Val 5th Val 6th Val 7th Val 8th Val R0 R1 R2 R3 0000 0002 R4 R5 0000 R6 R7 Solution Register 1st Val 2nd Val 3rd Val 4th Val 5th Val 6th Val 7th Val 8th Val R0 R1 R2 R3 0 2 1 0 R4 0 2 1 0 0 2 1 0 R5 0 3 5 7 A B C R6 17 R7 Page 5 of 7 6 24 points Debugging Recall that in homework 6 we wrote a program to compare 2 numbers The following program does something similar but instead of comparing 2 numbers we compare 2 strings The two strings are stored at memory locations x4000 and x5000 and are null terminated that is end with x0000 You may assume that the strings are of the same length The program was intended to have R1 be 0 if the two strings are equal and 1 otherwise However the program has 4 errors and does not behave as expected Identify and correct the errors in the code give the address for each error and the correction in Hex and the pseudo code Memory Address x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C x300D x300E x300F Hex Value x5260 x240C x260C x6880 x6A80 x0406 x14A1 x16E1 x993F x1922 x1905 x0BF7 x1261 xF025 x4000 x5000 Translation R1 R1 AND 0 R2 MEM PC 12 R3 MEM PC 12 R4 MEM R2 0 R5 MEM R2 0 BRz 6 R2 R2 1 R3 R3 1 R4 NOT R4 R4 R4 2 R4 R4 R5 BRnp 9 R1 R1 1 HALT JSRR R0 R0 R0 AND R0 Address x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C x300D x300E x300F Hex Value x5260 x240C x260C x6880 x6AC0 x0407 x14A1 x16E1 x993F x1921 x1905 x05F7 x1261 Page 6 xF025 x4000 x5000 Translation R1 R1 AND 0 R2 MEM PC 12 R3 MEM PC 12 R4 MEM R2 0 R5 MEM R3 0 BRz 7 R2 R2 1 R3 R3 1 R4 NOT R4 R4 R4 1 R4 R4 R5 BRz 9 R1 R1 1 of 7 HALT JSRR R0 R0 R0 AND R0 Solution Figure 1 Instruction Set from ItCS 2nd edition A 3 The Instruction Set 15 14 13 12 11 10 9 ADD 8 7 6 5 0001 DR SR1 0 0001 DR SR1 1 0101 DR SR1 0 AND 0101 DR SR1 1 BR 0000 JMP 1100 JSR 0100 1 JSRR 0100 0 ADD AND LD n 00 SR2 imm5 00 …
View Full Document