Computer Sciences Dept.

CS/ECE 252 Introduction to Computer Engineering

Fall 2011 Section 1
Instructor Guri Sohi
TAs Newsha Ardalani and Rebecca Lam

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

Homework 7 // Due at lecture Mon, Nov 21

Primary contact for this homework: Newsha Ardalani [newsha 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 pages and staple multiple pages together.

Important notes:

  1. All the codes handed in should be commented, answers without comments would be penalized.
  2. Use the "homework7" DropBox in Learn@UW to hand in the problems 4 and 5 electronically. You have to submit the .asm files in the format of q#.asm, i.e the file you submit for problem 4 should be named as q4.asm. Since your codes are tested automatically, it is important to stick to this naming convention, otherwise you would lose the credit, even if your code is working correctly. All team members need to submit all the files, Write the names of all the group members above the main code for all the submissions.
  3. You can submit your codes for problem 4 and 5 as many times as you want untill 11:00 AM, after that time we will consider your latest submissions for grading.

Problem 1 (5 points)

An LC-3 program is provided below:

 
	.ORIG x3000
	LD    R0, ASCII
	LD    R1, NEG
AGAIN  	LDI   R2, DSR
	BRzp  AGAIN
	STI   R0, DDR
	ADD   R0, R0, #1
	ADD   R3, R0, R1
	BRn   AGAIN           
	HALT
ASCII   .FILL  x0031
NEG     .FILL  xFFBF                      
DSR     .FILL  xFE04              ; Address of DSR
DDR     .FILL  xFE06              ; Address of DDR
        .END
  1. What is the purpose of "STI R0, DDR"? (1 point)
  2. What is the purpose of reading the Display Status Register (DSR)? (1 points)
  3. What does DSR bit 14 do, though we are not using in this program? (1 point)
  4. What does this program do? (2 points)

Problem 2 (3 points)

We want the following program fragment to shift R5 to the left by five bits, but there is one error in this code.

 
	
	.ORIG x3000
	AND  R2, R2, #0
	ADD  R2, R2, #5
LOOP 	BRz  DONE
	ADD  R2, R2, #-1
	ADD  R5, R5, R5
	BR   LOOP
DONE 	HALT
   	.END
  1. How many times does the instruction labled LOOP get executed?(1 points)
  2. What is wrong with this program?(1 point)
  3. How to fix it?(1 point)

Problem 3 (4 points)

What does the following program do? Explain in not more than 15 words please.

 

	.ORIG x3000
	AND R1, R1, #0		
	ADD R2, R1, #15		; R2 = 15
	ADD R3, R1, #1		; R3 = 1	
	LDI R0, IN_ADDR		; R0 = Mem[IN_ADDR]

L0 	AND R4, R0, R3		; check the bit that R3 has a one at, it starts from the LSB
	BRz L1			; Branch L1 if it is zero
	ADD R1, R1, #1		; otherwise increment R1, so the LSB is set to one

L1 	ADD R1, R1, R1		; shift R1 one bit to the left
	ADD R3, R3, R3		; shift R3 one bit to the left	
	ADD R2, R2, #-1		; decrement counter
	BRp L0			; until reach zero
	AND R4, R0, R3		; check the next bit
	BRz L2			; Branch L2 if it is zero
	ADD R1, R1, #1		; otherwise increment R1	
L2 	ST R1, OUTPUT		; store R1 in OUTPUT
	HALT
IN_ADDR .FILL x4000
OUTPUT .BLKW x1
	.END

Problem 4 (6 points)

The LC-3 has no circular shift instruction. In circular shifting, the bits are rotated as if the left and right ends of the register were joined. For example, the binary string 00110011 when right-shifted by one bit moves every bit one position to the right and the value that is shifted in on the left is whatever value was shifted out on the right, which results in 10011001.
The program we want you to design should take in a 16-bit value at memory location M1 that contains the number to shift and a value at M2 that contains the number of bits to shift.The 16-bit result should be stored in M3. The value at M4 indicates the direction of the shift(0 for left-shift and 1 for right shift). For instance, suppose [M1] = 0x000F and [M2] = 0x0002 and [M4] = 0x0001. After execution of the program, [M3] = 0xC003.
Make sure to use the template code provided (q4.zip) for this problem and use the tests provided to test your code. To check the functionality of your Left Shift, type the following line in the Pennsim command line:

 script TestLeftShift.txt 
If your test passes successfully, you have to see the message "TRUE(check M3 x003C)" in the last line of command output window. Any other message that includes "FALSE(check M3 x003C)", indicates your test is failed. This test is worth 2 points.
To run the functionality of your Right Shift, type the following line in the Pennsim command line:
 script TestRightShift.txt 
If your test passes successfully, you have to see the message "TRUE(check M3 x3C00)" in the last line of command output window. Any other message that includes "FALSE(check M3 x3C00)", indicates your test is failed. This test is worth 4 points.
You need to only submit the q4.asm.

Problem 5 (12 points)

Finally! Let's get to writing a real assembly program, you will build a working project from the ground up. For this question, you will implement a simple digital countdown counter with the capablity to set the time. You will write a program that takes a hexadecimal digit from input (keyboard) and represents the number on the screen in the seven-segment display character representation format. After a certain amount of time, it will decrement the counter and continue this process until it gets to zero, then it will ask the user to enter another digit. The user can choose to continue by entering any character other than "z" or choose to quit by entering the "z" character.

Essentially your program should support the following functionalities:

  1. The program starts by asking the user to "Enter a digit between 0-F".
  2. After reading the character from input, before starting any operation, we should check the input character for the terminating character (z in your case). If it is, output the terminating string on the screen ("Finished successfully, Thank you for trying!") and stop the program. Otherwise continue to the next step.
  3. Don't hurry! If the input character is not the terminating charcter, you are not yet allowed to start any operation. We should check first for the validity of the input character. The input character is considered valid if it is in the range of either [0,9] or [A-F] or [a-f]. If it is NOT valid, the "Bad input, Try again!" string should be represented on the screen and the user should be asked to "Enter a digit between 0-F".
  4. Now that the input charcater is valid you have to display it on the screen in RED. Make sure you read the background information at the end of this assigmnet to learn how to display something on the video output. To save you time and for consistency among your final outputs, we have provided you the bit patterns of all hexadecimal digits, so make sure to use them for representation. Furthermore, make sure to represent the number in the center of the screen (Since the screen size is 128*124 pixels, the center of your number should be the point (64,62) and since the size of the bit patterns provided are 21*16 pixels, the coordinate of the leftmost upper corner of your number should be (56,52)).
  5. After a certain amount of time, the number shown on the screen should decrement by one, and this process should continue until it gets to zero. The DELAY subroutine is given to you in the template. Use this subroutine for the time interval between two consecutive numbers.
  6. After the counter gets to zero, we should ask the user to "Input a digit between 0-9". As mentioned in the second step, this process should continue until the user enters the terminating character. Otherwise the "Finished successfully, Thank you for trying!" string should be represented on the screen.

Important files: All of the files you'll need for this assignment are available in a q5.zip zip archive.

A Word on Testing: Two tests are provided for this assignment, we will first run your program with test1, if it passes, assuming you have comments on your code, you will get all the points, otherwise we will run test2 for checking the functionality of each of your subroutines. The point distribution is as follows:

  1. Your code assembles and commented(1 point)
  2. SETSTARTPOINT (1 point)
  3. CHECKFORMAT (2 points)
  4. CONVERTtoINT (2 points)
  5. DISPLAY (3 points)
  6. Display counting down (3 points)

To run the tests for q5, type the following line into the Pennsim command line:

script test#.txt(put the TestNumber instead of #)

If your program is called something other than q5.asm you should either change its name or edit the name within test#.txt, but make sure to rename it back to q5.asm when hand it in electronically. Your tests are considered failed, if you see some messages having FALSE keyword on the output command window.

Background Information

Video console interface: The video console is directly linked to a region of addressable memory, namely xC000 to xFDFF. Every row is 128 dots (also called pixels) across, and there are 124 rows in total, which gives us a 128x124 screen to work with. Each pixel is linked to one memory address, so xC000 is the pixel on the top left corner, xC001 is the pixel to the right of it, and so on. Since 128 is x0080 in hex, xC000 + x0080 = xC080 is the location of the leftmost pixel on the second row from the top, and xC000 + 2*x0080 = xC100 would be the next pixel down. In general, you can use the following formula to calculate which address of a pixel:

 
    address = xC000 + (y_coordinate * 128) + x_coordinate

Color encoding: Each location holds a 16-bit value which determines the color of that pixel. The high-order bit is ignored, and the remaining 15 bits specify the color, using a 5-bit values for each of red [14:10], green [9:5], and blue [4:0]. The codes of the colors you would need for your program (BLACK and RED) is given to you in the code provided, however if you are curious to know the value for some other common colors, here are a few of them:

Pixel Code Color
x7FFF White
x0000 Black
x7C00 Red
x03E0 Green
x001F Blue
x3466 Puce

 

Coordinate systems: This code uses just the video console coordinates. This system is zero based (that is, the top-most row is row 0 and the left-most column is column 0).

 
Computer Sciences | UW Home