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 5 should be named as q5.asm. Since your codes are tested automatically, it is important to stick to this naming convention, otherwise you would loose 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 as many times as you want untill the midnight, we will consider your latest submissions.

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)
    It represents the data in register R0 on monitor
  2. What is the purpose of reading the Display Status Register (DSR)? (1 points)
    The DDR may currently contain a value waiting to be output; if the DSR in not checked, that value may be overwritten and lost.
  3. What does DSR bit 14 do, but we are not using in this program? (1 point)
    Bit [14] is interrupt enable bit, which can be set or cleared by processor, depending on whether or not the processor wants to give the I/O device the right to request service.
  4. What does this program do? (2 points)
    It prints out the following string on monitor:
    123456789:;<=>?@

Problem 2 (3 points)

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

 
	
	.ORIG
	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)
    It runs until all the bits of R5 are shifted out which depends on the value in the R5 and more specifically where its first bit one from right is located, if R5 is zero or x8000, it runs twice (once to enter the loop, and once to return back and see the BRz is taken). If R5 has one in its LSB it runs 16 times. Generally, if the first one from the right happens at bit ith, it runs for (16-i+1) times.
  2. What is wrong with this program?(1 point)
    The loop counter is misplaced and the branch is taken according to the result of addition ADD R5, R5, R5 rather than the loop counter decrement
  3. How to fix it?(1 point)
    Chage the place of ADD R2, R2, #-1 with ADD R5, R5, R5

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 at position R3 has one in, 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, so the LSB is always zero after this shift
	ADD R3, R3, R3		; shift R3 one bit to the left	
	ADD R2, R2, #-1		; decrement counter
	BRp L0			; untill 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

It reverses the bit order of Mem[IN_ADDR] and write the result into the Mem[OUTPUT]

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 its bits 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] = 0x0001 and [M4] = 0x0001. After execution of the program, [M3] = 0x8007.
Make sure to use the template code provided (q4_sol) 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 set capablity, you have to write a program that takes a hexadecimal digit from input (key board) and represents the number on the screen in the seven-segment display character representation format, after a certain amount of time, decrements the counter and continues this prcess untill 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 enetring 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 charcater not to be the terminating character (z in your case), if it is, represent the terminating string on the screen ("Finished successfully, Thank you for trying!") and the program stops, otherwise continue to th 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], otherwise the "Bad input, Try again!" string should be represented on the screen and ask again the user 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 your time and for consistency among your final outputs, we have provided you the bitpatterns of all hexadecimal digits, 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 bitpatterns provided are 21*8 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 untill it gets to zero. The DELAY subroutine is given to you, use this subroutine for making 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 mentined in the second step, this process should continue untill 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 the assignment are available in a q5_sol zip archive.

A Word on Testing: Two tests are provided for this assignment, we will first run your program with test1, if it passes 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 (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 q6, type the following line into the Pennsim command line:

script test#.sc(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#.sc, 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