MIT6 004s09 Quiz03 Sol
MIT6 004s09 Quiz03 Sol
MIT6 004s09 Quiz03 Sol
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y
NOTE: Reference material and scratch copies of code appear on the backs of quiz pages.
(A) (2 points) A student tries to optimize his Beta assembly program by replacing a line
containing
ADDC(R0, 3*4+5, R1)
by
ADDC(R0, 17, R1)
Is the resulting binary program smaller? Does it run faster?
(C) What value will be found in the low 16 bits of the BEQ instruction resulting from the
following assembly language snippet?
. = 0x100
0x0000
16-bit offset portion of above BEQ instruction: ____________________
(D) Can every SUBC instruction be replaced by an equivalent ADDC instruction with the
constant negated? If so, answer “YES”; if not, give an example of a SUBC
instruction that can’t be replaced by an ADDC.
The wfps procedure determines whether a string of left and right parentheses is well balanced,
much as your Turing machine of Lab 4 did. Below is the code for the wfps (“well-formed paren
string”) procedure in C, as well as its translation to Beta assembly code. This code is reproduced
on the backs of the following two pages for your use and/or annotation.
int STR[100];
// string of parens
STR: . = .+4*100
int wfps(int i,
// current index in STR
wfps: PUSH(LP)
int n)
// LPARENs to balance
PUSH(BP)
{ int c = STR[i];
// next character
MOVE(SP, BP)
int new_n;
// next value of n
ALLOCATE(1)
if (c == 0)
// if end of string,
PUSH(R1)
return (n == 0);
// return 1 iff n == 0
else if (c == 1)
// on LEFT PAREN,
LD(BP, -12, R0)
new_n = n+1;
// increment n
MULC(R0, 4, R0)
else {
// else must be RPAREN
LD(R0, STR, R1)
}
LD(BP, -16, R0)
CMPEQC(R0, 0, R0)
wfps expects to find a string of parentheses in the integer array stored at STR. The
string is encoded as a series of 32-bit integers having values of
rtn: POP(R1)
These integers are stored in consecutive 32-bit locations starting at the address
JMP(LP)
STR.
1. The first, i, is the index of the start of the part of STR that this call of
BF(R0, rpar)
four characters and start examining STR starting at the fifth character (the
character at STR[4]).
rpar: LD(BP, -16, R0)
wfps returns 1 if the part of STR being examined represents a string of balanced par: PUSH(R0)
parentheses if n additional left parentheses are prepended to its left, and returns 0 LD(BP, -12, R0)
PUSH(R0)
Note that the compiler may use some simple optimizations to simplify the BR(wfps, LP)
BR(rtn)
(A) (3 points) In the space below, fill in the binary value of the instruction stored at the
location tagged ‘more:’ in the above assembly-language program.
1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(B) (1 point) Is the value of the variable c from the C program stored in the local stack
frame? If so, give its (signed) offset from BP; else write “NO”.
BP+0
Stack offset of variable c, or “NO”: _________________
(C) (1 point) Is the value of the variable new_n from the C program stored in the local
stack frame? If so, give its (signed) offset from BP; else write “NO”.
NO or BP+8
Stack offset of variable new_n, or “NO”: _________________
(D) (2 points) What is the missing C source code represented by xxxxx in the given C
program?
new_n = n - 1
The procedure wfps is called from an external procedure and its execution is interrupted during a
recursive call to wfps, just prior to the execution of the instruction labeled ‘rtn:’. The contents
of a region of memory are shown to below on the left. At this point, SP contains 0x1D8, and BP
contains 0x1D0.
(E) (1 point) What are the arguments to the most recent active call to wfps?
188: 7
18C: 4A8 2
Most recent arguments (HEX): i=________; 0
n=________
190: 0
194: 0 (F) (1 point) What are the arguments to the original call to wfps?
198: 458
19C: D4 0
Original arguments (HEX): i=________; 0
n=________
1A0: 1
1A4: D8 (G) (1 point) What value is in R0 at this point?
1A8: 1
1AC: 1
0
Contents of R0 (HEX): ________
1B0: 3B8
1B4: 1A0 (H) (1 point) How many parens (left and right) are in the string stored at STR
1B8: 2 (starting at index 0)? Give a number, or “CAN’T TELL” if the number
can’t be determined from the given information.
1BC: 1
1C0: 0 CAN’T TELL
Length of string, or “CAN’T TELL”: _______________
1C4: 2
1C8: 3B8 (I) (1 point) What is the hex address of the instruction tagged par:?
1CC: 1B8
BP->1D0: 2 39C
Address of par (HEX): ________
1D4: 2
SP->1D8: 0 (J) (1 point) What is the hex address of the BR instruction that called wfps
originally?
454
Address of original call (HEX): ________
Following is an incomplete table listing control signals for several instructions on an unpipelined
Beta. You may wish to consult the Beta diagram on the back of the previous page and the
instruction set summary on the back of the first page.
The operations listed include two existing instructions and two proposed additions to the Beta
instruction set:
In the following table, φ represents a “don’t care” or unspecified value; Z is the value (0 or 1)
output by the 32-input NOR in the unpipelined Beta diagram. Your job is to complete the table by
filling in each unshaded entry. In each case, enter an opcode, a value, an expression, or φ as
appropriate.
JMP φ 1 φ 0 0 φ 2 φ 0
BEQ φ 1 φ 0 0 φ Z φ 0
LDX A+B 1 0 2 0 0 0 0 0
MVZC A+B Z 1 1 0 φ 0 0 0
(Complete the above table)
END OF QUIZ!
(phew!)