ece 190 midterm exam 2 - national center for ... · ece 190 midterm exam 2 spring 2011 practice...

13
ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Name: NetID: Programming studio section (mark only the one you attend): DCL 440 DCL 520 9:00 AM [ ] AD4 [ ] AD6 12:00 PM [ ] AD5 [ ] AD8 1:00 PM [ ] AD3 [ ] AD7 2:00 PM [ ] AD9 3:00 PM [ ] AD1 [ ] AD2 Be sure your exam booklet has 8 pages. Do not tear the exam booklet apart. Write your name at the top of each page. This is a closed book exam. You may not use a calculator. You are allowed one handwritten 8.5 x 11" sheet of notes. Absolutely no interaction between students is allowed. Be sure to clearly indicate any assumptions that you make. Don’t panic, and good luck! Problem 1 10 points _______________________________ Problem 2 10 points _______________________________ Problem 3 5 points _______________________________ Problem 4 5 points _______________________________ Problem 5 10 points _______________________________ Total 40 points _______________________________

Upload: hadiep

Post on 28-Apr-2018

246 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

ECE 190 Midterm Exam 2 Spring 2011

Practice Exam

Name: NetID:

Programming studio section (mark only the one you attend):

DCL 440 DCL 520

9:00 AM [ ] AD4 [ ] AD6

12:00 PM [ ] AD5 [ ] AD8

1:00 PM [ ] AD3 [ ] AD7

2:00 PM [ ] AD9

3:00 PM [ ] AD1 [ ] AD2

Be sure your exam booklet has 8 pages.

Do not tear the exam booklet apart.

Write your name at the top of each page.

This is a closed book exam.

You may not use a calculator.

You are allowed one handwritten 8.5 x 11" sheet of notes.

Absolutely no interaction between students is allowed.

Be sure to clearly indicate any assumptions that you make.

Don’t panic, and good luck!

Problem 1 10 points _______________________________

Problem 2 10 points _______________________________

Problem 3 5 points _______________________________

Problem 4 5 points _______________________________

Problem 5 10 points _______________________________

Total 40 points _______________________________

Page 2: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 2 Name: ___________________________________________

Problem 1 (10 points): FSM Example problem 1: Design FSM and combinational circuit necessary to implement the FSM logic for

controlling traffic danger sign described in section 3.6.4 of the textbook.

Part A (1 point): Draw state diagram.

Part B (2 points): Fill in state transition and output truth tables.

Part C (2 points): Using Programmable Logic Array, design combinational logic circuit that implements

the traffic danger sign controller logic.

Example problems 2 & 3: Solve problems 3.41 and 3.43 from the textbook.

Page 3: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 3 Name: ___________________________________________

Problem 2 (10 points): Von-Neumann model

The following LC-3 program fragment, represented as three hexadecimal numbers, is stored in

memory at the indicated locations and the following values are stored in registers:

address instruction register Value

… R0 x000A

x3000 x903F R1 x000B

x3001 x1021 R2 x000C

x3002 x1201 R3 x000D

… R4 x000E

Part A (6 point): Assuming PC is initially set to x3000, trace the execution of three steps of the

given program segment and fill in the table below. Write down values stored in PC, IR, MAR,

MDR, N, Z, and P registers at the end of the instruction cycle. Values for PC, IR, MAR, and

MDR should be written in hexadecimal. Values for N, Z, and P should be written in binary.

PC IR MAR MDR N Z P

Part B (2 point): How many memory accesses will take place during the execution of this

program segment?

Answer: _______________________

Part C (2 point): What hexadecimal value will be stored in R1 once all three instructions are

executed?

Answer: _______________________

Page 4: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 4 Name: ___________________________________________

Problem 3 (5 points): LC-3 ISA

A TA spilled a cup of coffee on his LC-3 computer. As a result, the computer was damaged in

some ways. Explain how this damage will manifest itself in computer‟s ability to execute some

instructions.

Part A (1 points): If MSB of IR is always stuck at one, which LC-3 instructions will be affected

and how?

List instructions that will be affected: ______________________________________________

Explain what will happen to them.

Part B (2 points): If the MSB of the global bus is shorted to ground, what impact would that

have on executions of RET?

Part C (2 point): Computer skips every other instruction. What damage to its PC register might

cause this?

Page 5: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 5 Name: ___________________________________________

Problem 4 (5 points): LC-3 I/O

Example problem 1: Draw a detailed flowchart describing the sequence of steps for sending an ASCII

value to LC-3 output display. You can assume that ASCII value of a character to be displayed is stored in

register R0. Your flowchart should show all interactions with DDR, DSR, R0 and any additional

registers you use.

Example problem 2: Draw a detailed flowchart describing the sequence of steps for receiving input from

LC-3 keyboard device. You can assume ASCII value of the character to be received from the keyboard is

to be stored in register R0. Your flowchart should show all interactions with KBDR, KBSR, R0

and any additional registers you use.

Example problem 3: Draw a detailed flowchart for the procedure of sending a string of

characters stored in LC-3 memory to the LC-3 output display. You can assume the starting

address of the string to be printed is already pre-loaded in register R0. Your flowchart should

show all interactions with DDR, DSR, R0, LC-3 memory and any additional registers you use.

Page 6: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 6 Name: ___________________________________________

Problem 5 (10 points): Stack

The follow snippet of code uses a stack to determine if a string input has balanced parentheses.

A string has balanced parentheses if there is one left-parenthesis for each right-parenthesis and if

there is no right-parenthesis before any unclosed left-parenthesis. The code should branch to

location PASS if the string has balanced parentheses, otherwise it should branch to FAIL. The

code uses 3 subroutines which manipulate a stack:

PUSH: pushes the contents of R0 onto the stack

POP: pops the top element off of the stack and saves the element in R0

EMPTY: loads R0 with x0001 if the stack is empty, otherwise loads R0 with x0000

The subroutines are callee-saved and the stack is initially empty.

LEA R1, INPUT ;load address of string

ADD R1, R1, #-1 LD R2, LPAREN_NEG

LD R3, RPAREN_NEG

LOOP

ADD R1, R1, #1

LDR R0, R1, #0 ;load next char from string

BRz DONE ;done if we reach null char

ADD R4, R2, R0 ;check if char is left parenthesis

BRnp NOT_LPAREN

JSR PUSH ;push onto stack if left parenthesis

BRnzp LOOP

NOT_LPAREN

ADD R4, R3, R0 ;check if char is right parenthesis

BRnp LOOP ;go back to loop if not right parenthesis

JSR EMPTY ;check if the stack is empty

BR___ ___________ ;PASS or FAIL if stack is empty

JSR POP

BRnzp LOOP

DONE

JSR EMPTY

BR___ ___________ ;PASS or FAIL

BR___ ___________ ;PASS or FAIL

LPAREN_NEG .FILL xFFD8 ; 2’s complement negative of ascii code for ‘(’

RPAREN_NEG .FILL xFFD7 ; 2’s complement negative of ascii code for ‘)’

For problems that ask you to draw the stack, assume that the stack starts at the bottom of

the table and grows upward. If a location on the stack contains an ASCII character, you may

just draw that character (you do NOT need to put the hex value of that character).

Page 7: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 7 Name: ___________________________________________

Part A (2 points): For INPUT string “((a+(b)))”, fill in the contents of the stack at the moment

the character „b‟ is loaded in the first line of LOOP.

Part B (2 points): For INPUT string “((a+(b)))”, fill in the contents of the stack at the moment

the program branches to either PASS or FAIL.

Part C (2 points): For INPUT string “(((a)+(b)) – (c)”, fill in the contents of the stack at the

moment the character „b‟ is loaded in the first line of LOOP.

Part D (2 points): For INPUT string “(((a)+(b)) – (c)”, fill in the contents of the stack at the

moment the program branches to either PASS or FAIL.

Part A Part B Part C Part D

Part E (2 points): Given your results, fill in the code with the missing BR condition codes and

branch locations (the three missing branches either jump to PASS or FAIL).

Page 8: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

Page: 8 Name: ___________________________________________

Page 9: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

1

ECE 190 Midterm Exam 2 Spring 2011

Practice Exam

Notes: Programming part of the exam will consist of three problems. Each problem will be

graded separately. We will provide skeleton codes for each problem.

Problem 1 (10 points): Warm-up.

After reading the MP3 handout Kirti discovered that the 16 x 16 "Life" board was not big

enough to hold the extremely fun PULSAR pattern. Sam was so disappointed in our failure that

he has asked you to write the new gold code for the CALC_ADDR routine.

Implement a subroutine that multiplies an input by 17 using only 5 non-looping/branching

instructions.

Inputs: R0 (will always be between 0 and 16, no need to do a modulus or any other looping

operation to ensure the input is in range)

Outputs: R1 = 17 * R0 + x5000

Description:

Callee saves all registers (does not count against 5 instruction limit).

Uses 5 non-looping instructions to perform a mult. by 17.

The table starts at x5000 (adding x5000 to 17 * R0 does not count against the 5 instruction

limit).

Use the following skeleton code is provided to get you started:

; “main” function

.ORIG x3000

AND R0, R0, #1

ADD R0, R0, #5 ; R0<-5 as an example

JSR CALC_ADDR

HALT

;

CALC_ADDR ; your code starts here

RET ; end of CALC_ADDR function

; any data fields to be allocated here

;

.END

Page 10: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

2

Problem 2 (5 points): Debugging.

This program is given a 200x200 matrix of cells, one cell per memory location starting at x5000

in row-major layout, and prints out a message indicating if there is a cell containing xFFFF. The

program, however, has several bugs. Find and fix them!

.orig x4000

; r0: address of matrix cell

; r1: size

; r2: size * size

; r3: addition counter in multiply

; r4: data in matrix cell

ld r0, base_address

ld r1, size

; r2 <- r1 * r1

add r3, r1, #0

multiply brz exit_multiply

add r2, r2, r1

add r3, r3, #-1

br multiply

exit_multiply

; find cell containing 0xffff in matrix

iterate add r2, r2, #-1

brn exit_iterate

ldr r4, r0, #0

add r0, r0, #1

add r4, r4, #1

brnp iterate

ld r0, success

puts

halt

exit_iterate

ld r0, failure

puts

halt

size .fill #200

base_address .fill x5000

success .stringz "Found it!\n"

failure .stringz "Didn't find it.\n"

Page 11: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

3

Problem 2 (20 points): Problem solving.

For this assignment you will need to implement a program that determines all the prime numbers

between 0 and 255 (including 0 and 255). To do this we will use the ancient algorithm called the

Sieve of Eratosthenes. In a standard programming exam we would give you a PDF reference

page instead of a URL, but since this is just a practice exam, here is a URL with a detailed

description of the algorithm: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Your program will be responsible for creating a look up table (LUT) starting at x5000. Each

entry in the table represents if the number is prime (x0001) or not (x0000). For example, since

the number 11 is prime the memory at x500B would contain x0001.

Specifics

1. Skeleton code will be provided on the real exam, to access it in your working directory

you would normally do:

Open a terminal, do NOT type 'ece190'

Execute 'cd exam2.2'

Execute 'ls -l' and you will see 'iter.asm' and 'sieve.asm'. If you do not see that file

please contact your TA immediately.

But since this is not a real exam, the source code is embedded in this file below. You can

copy-paste it into three separate files yourself.

2. The LUT starts at x5000 and ends at x50FF. Do not modify any entries above x50FF.

3. All functions should be calle saved.

Required Decomposition

1. exam2.2.asm

Inputs: None

Output: None

Description: Already implemented. Calls ITER and then prints out the resulting table.

; Register Table

; R0 - Output register

; R1 - Loop variable

; R2 - Pointer into prime table

; R3 - Holds x0030

.orig x3000

MAIN

; Call ITER

LD R7, ITER

JSRR R7

; Print string

LEA R0, STRING

PUTS

; Loop through and print the prime numbers table

LD R1, LOOP

LD R2, PRIME

Page 12: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

4

LD R3, ASCII

LOOP_START

; Condition code check

ADD R1, R1, #0

BRn LOOP_END

; Load data from table

LDR R0, R2, #0

; Convert to ASCII

ADD R0, R0, R3

; Print out

OUT

; Print space

LD R0, SPACE

OUT

; Every 16 entries, print newline

AND R4, R1, #15

BRnp NO_PRINT

; Print new line

AND R0, R0, #0

ADD R0, R0, #10

OUT

NO_PRINT

; Loop update

ADD R1, R1, #-1

BRnzp LOOP_START

LOOP_END

HALT

ASCII .fill x0030

SPACE .fill x0020

LOOP .fill x00FF

ITER .fill x4000

SIEVE .fill x4200

PRIME .fill x5000

STRING .stringz "\nPRIME NUMBERS TABLE\n"

.end

2. iter.asm: Starts at x4000, function called ITER.

Inputs: None

Outputs: None

Description: Loops from 2-255 and for each iteration implements a call to SIEVE.

Must use a loop and must implement a call to SIEVE using JSRR to receive credit.

; Register Table

; R0 -

Page 13: ECE 190 Midterm Exam 2 - National Center for ... · ECE 190 Midterm Exam 2 Spring 2011 Practice Exam Notes: ... After reading the MP3 handout Kirti discovered that the 16 x 16 "Life"

5

; R1 -

; R2 -

; R3 -

; R4 -

; R5 -

; R6 -

; R7 -

.orig x4000

ITER

RET

LOOP .fill x00FF

SIEVE .fill x4200

.end

3. sieve.asm: Starts at x4100, function called SIEVE.

Inputs: R0 - ITER's loop variable.

Outputs: None

Description: Sets every R0-th entry in the LUT to x0001.

o Example: If R0 was equal to x0002 it would set x5002, x5004, ..., x5000 + n *

R0 (where x5000 + n * R0 < x5100 and n > 0).

Must use a loop to receive credit.

; Register Table

; R0 -

; R1 -

; R2 -

; R3 -

; R4 -

; R5 -

; R6 -

; R7 -

.orig x4200

SIEVE

RET

LOOP .fill x00FF

PRIME .fill x5000

PRIME_END .fill x50FF

.end