Anda di halaman 1dari 6

ECE 190 Midterm Exam

HoChiMinh City University of Technology


Sunday 18 July 2010
Name:
Student ID # :

Be sure your exam booklet has 6 pages (3 sheets).


You have THREE HOURS to complete this exam.
Write your name at the top of each page.
This is a closed book exam.
You may not use a calculator.
You are allowed two handwritten A4 sheets of notes (both sides).
Absolutely no interaction between students is allowed.
Show all of your work.
Be sure to clearly indicate any assumptions that you make.
Dont panic, and good luck!

(An LC-3 ISA encoding + RTL chart is included in the test.)

Problem 1

25 points

_______________________________

Problem 2

30 points

_______________________________

Problem 3

20 points

_______________________________

Problem 4

25 points

_______________________________

Total

100 points

_______________________________

Page 2

Name: ____________________________________________

Problem 1 (25 points): Short Answer


Part A (5 points): After a user presses the letter k, the KBDR contains the value for k
(x6B), and the KBSR contains the value x8000. Assuming that R2=xFE02 (the KBDRs
address) and that the LC-3 then executes LDR R1,R2,#0, fill in the table below with
correct values after the instruction finishes execution.
Register
Value
R1
R2
MAR
MDR
KBSR
Part B (5 points): What might happen if a program reads from the Display Data Register
without checking the Display Status Registers ready bit?

Part C (5 points): Explain why R7 must be caller-saved for any LC-3 subroutine.
(Recall that caller-saved implies that the subroutine can change the value.)

Consider the following code snippet


LOOP

AND
R1, R1,
ADD
R0, R0,
BRn
DONE
ADD
R0, R0,
ADD
R1, R1,
BRnzp LOOP
HALT

#0
#0
R0
#1

Part D (5 points): For what value or values of register R0 does the code not reach the
HALT instruction?

Part E (5 points): What does the code calculate in R1?

Page 3

Name: ____________________________________________

Problem 2 (30 points): Interpreting Binary Code


Part A (10 points): The contents of several memory locations are shown below. For each
location other than the TRAP, write a register transfer language (RTL) statement
corresponding to the instruction represented by the contents of that location. For example,
the word x907F is R0 NOT R1 in RTL. I will not grade the RTL syntax, but what
you write must convey the action taken by the LC-3 when processing each instruction.
Address

Content

x3015

x1DBF

x3016

x7180

x3017

xF020

x3018

x6F80

x3019

x7180

x301A

x11E0

RTL

TRAP x20, or GETC

Part B (10 points): The table below shows the values in certain registers before
execution of the code in Part A. Fill in the values for all registers after the six
instructions execute. Do NOT leave blank entries; you may write unknown if the
contents of a register cannot be known.
Register

Before

PC

x3015

MAR

unknown

MDR

unknown

R0

x5678

R1

x2345

R6

xC234

R7

xABCD

After

Part C (10 points): In 25 words or less, explain what the code in Part A does.

Page 4

Name: ____________________________________________

Problem 3 (20 points): Interpreting Assembly Code


This question focuses on the following subroutine written in LC-3 assembly language.
MY_FUNCTION ST
ST
READ_LOOP
GETC
OUT
LD
ADD
BRzp
BAD_KEY
AND
ADD
BRnzp
MAYBE_OK
ADD
BRn
LD
ADD
BRn
ADD
BRzp
ADD
DONE
LD
LD
RET
SAVE_R1
.BLKW
SAVE_R7
.BLKW
NEG_0
.FILL
DIFF0A
.FILL

R7,SAVE_R7
R1,SAVE_R1
R1,NEG_0
R0,R0,R1
MAYBE_OK
R0,R0,#0
R0,R0,#-1
DONE
R1,R0,#-10
DONE
R1,DIFF0A
R0,R0,R1
BAD_KEY
R1,R0,#-6
BAD_KEY
R0,R0,#10
R1,SAVE_R1
R7,SAVE_R7
1
1
xFFD0
xFFEF

; storage for saved register values


; the additive inverse of ASCII '0'
; ASCII '0' - ASCII 'A' (x-11)

Part A (5 points): Explain why the value in R7 is saved to memory and later copied back
into R7.

Part B (5 points): The subroutine returns a value in R0. What value is returned in R0 if
the user presses the letter A during the GETC trap?

Part C (5 points): How many different values can be returned in R0? (For example, if
R0 were always either 3 or 10 when the subroutine returned, the answer would be 2.)

Part D (5 points): Briefly explain what the subroutine does.

Page 5

Name: ____________________________________________

Problem 4 (25 points): C Programming


Consider the following C program.
#include <stdio.h>
int y = 5;
int foo (int a, int b)
{
int c = a / b;
c = c * 2;
y++;
while (0 < b) {
printf ("b = %d\n", --b);
}
if (10 > a--) {
return a;
} else {
return c;
}
}
int main ()
{
int x = foo (10, 3);
y++;
return 0;
}

Part A (10 points): Write down the output of this program.

Part B (10 points): What is the value of x right before main returns?

Part C (5 points): What is the value of y right before main returns?

NOTES: RTL corresponds to execution (after fetch!); JSRR not shown


ADD

0001

DR

SR1

0 00

SR2

ADD DR, SR1, SR2

LD

DR SR1 + SR2, Setcc


ADD

0001

DR

0101

DR

SR1

imm5

ADD DR, SR1, imm5

LDI

0101

DR

SR1

0 00

SR2

AND DR, SR1, SR2

LDR

0000

n z p

SR1

imm5

AND DR, SR1, imm5

LEA

1100

000

PCoffset9

BaseR

BR{nzp} PCoffset9

NOT

0100

000000

JMP BaseR

ST

1111

PCoffset11

0000

PCoffset9

LDI DR, PCoffset9

0110

DR

BaseR

offset6

LDR DR, BaseR, offset6

1110

DR

PCoffset9

LEA DR, PCoffset9

1001

DR

SR

111111

NOT DR, SR

0011

SR

PCoffset9

ST SR, PCoffset9

M[PC + SEXT(PCoffset9)] SR
JSR PCoffset11

STI

R7 PC, PC PC + SEXT(PCoffset11)
TRAP

DR

DR NOT SR, Setcc

PC BaseR
JSR

1010

DR PC + SEXT(PCoffset9), Setcc

((n AND N) OR (z AND Z) OR (p AND P)):


PC PC + SEXT(PCoffset9)
JMP

LD DR, PCoffset9

DR M[BaseR + SEXT(offset6)], Setcc

DR SR1 AND SEXT(imm5), Setcc


BR

PCoffset9

DR M[M[PC + SEXT(PCoffset9)]], Setcc

DR SR1 AND SR2, Setcc


AND

DR

DR M[PC + SEXT(PCoffset9)], Setcc

DR SR1 + SEXT(imm5), Setcc


AND

0010

trapvect8

R7 PC, PC M[ZEXT(trapvect8)]

1011

SR

PCoffset9

STI SR, PCoffset9

M[M[PC + SEXT(PCoffset9)]] SR
TRAP trapvect8

STR

0111

SR

BaseR

offset6

M[BaseR + SEXT(offset6)] SR

STR SR, BaseR, offset6

Anda mungkin juga menyukai