Computer Sciences Dept.

CS/ECE 252 Introduction to Computer Engineering

Fall 2014 Section 2
Instructor Guri Sohi
TAs Lisa Ossian, Sujith Surendran, Minsub Shin

URL: http://www.cs.wisc.edu/~sohi/cs252/Fall2014/

Homework 6 [Due at lecture on Fri, Nov 7]

Primary contact for this homework: Sujith Surendran [sujiths at cs dot wisc dot edu]

You must do this homework alone. Please handin the copy of the homework listing your section number, full name (as appear in Learn@UW) and UW ID. You must staple all pages of your homework together to receive full credit

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. Your programs should start at address x3000 and end with a HALT instruction (0xF025).

Submission guidelines:

  1. For your your code submit only one archive file (*.zip) to the folder homework6.
  2. Name the file with the following convention: hw6.zip
  3. Your archive file should contain the following (The files MUST be named exactly like this):
    1. hw6_p3.txt - Binary code for problem 3
    2. 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.

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., 1:20 PM) on Friday, November 7. 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 If R2 = 0, go to Halt
0x3005 0110 0110 0100 0000 R3 = Mem[R1]
0x3006 0101 1000 1110 0001 R4 = R3 AND x0001
0x3007 0000 0010 0000 0001 If p flag set, branch to 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 Branch always to 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 0100 Data Value 4
0x3010 0000 0000 0000 0101 Data Value 5
0x3011 0000 0000 0000 0110 Data Value 6

  1. (3 Points) Fill in the comments column with a summary of what each instruction does.
  2. (1 Point) How many times does the instruction at address x3004 execute?
    5 times
  3. (1 Point) What is the value of R0 before the instruction at x300C is executed?
    6
  4. (1 Point) Describe what this program does in 1-2 sentences (ie, specify how the value at R0 relates to the Data Values after the Halt instruction)
    This program adds all even numbers between memory locations x300D and x3010, and stores the result 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 "x5000" that contains the number which will have its bits shifted and a value at memory location "x5001" that contains the number of bits to shift and stores the 16-bit result into memory location "x5002". For example, if M[x5000] = 0x1102 and M[x5001] = 0x0002, then after the program completes M[x5002] = 0x4408 (which is x1102 left shifted by 2).

  1. (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 0000 0000 0110  ; Load value at x5000 into R0
    (b)1010 0010 0000 0110  ; Load value at x5001 into R1
    (c)0001 0000 0000 0000  ; Left shift R0 by 1 bit
    (d)0001 0010 0111 1111  ; Decrement R1
    (e)0000 0011 1111 1101  ; Branch if positive to LOOP
    (f)1011 0000 0000 0011  ; Store the value of R0 to x5002
       1111 0000 0010 0101 ; HALT
       0101 0000 0000 0000 ; DATA1: x5000
       0101 0000 0000 0001 ; DATA2: x5001
       0101 0000 0000 0010 ; DATA3: x5002
    
  2. (2 Points) Suppose the above program had to execute correctly (i.e., produce the correct result in memory location x5002 after the execution of the program) for the following constraints on the input (values at memory locations x5000 and x5001 before execution of the program):
    • The memory location x5000 can have any value.
    • The integer value at x5001 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 x5002).
    If the input at x5001 is 0, the code will give an incorrect output.

Problem 3 (8 points)

Write a short LC-3 program in binary (i.e., machine language) that compares the 2 numbers in memory locations x5000 and x5001. It then puts the larger number among the two into location x5002. 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 x5000 and x5001 to be x0001 and x0002 respectively. Your screenshot should show the final value in x5002 at the HALT instruction. Turn in the screenshot as a hard copy.

Algorithm can be found here
Script file can be found here
Binary file can be found here

Problem 4 (10 points)

  1. (4 Points) Consider an algorithm which counts the number of times the ASCII characters 'c' and 's' occur in a string that is stored starting at location x5000. The count for ‘c’ should be stored at the location x4000, count for ‘s’ should be stored at location x4001. Show the algorithm as a flowchart by decomposing it into its basic constructs.
    Note: The first character of the string is stored at memory location 0x5000. The last character of the string is the NULL character (having an ASCII value 0x00). One character is stored in each memory location (the lower order 8 bits represent the ASCII character and higher order 8 bits are 0).

  2. (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 x4000 and x4001 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.. Also mention on the screenshot which string you have stored starting at x5000 while testing your code.
Script file can be found here
Binary file can be found here

 
Computer Sciences | UW Home