6.004
http://6004.csail.mit.edu/currentsemester/[12/1/2009 9:21:59 PM]
Fall 2009
Announcements
Handouts
Tutorial problems
Past Quizzes
On-line assignments
Course info
Course description
Course objectives
Course calendar
Lab hours
Courseware
Suggestions
Suggestion Box
Previous terms
Lab Queue
WIKI
(NEW! Lab hin
Stacks and procedures
indicates problems that have been selected for discussion in section, time permitting.
Problem 1.
Consider the following implementation of an algorithm for finding the greatest common
divisor of two integers:
int gcd(int a,int b) {
if (a == b) return a;
if (a > b) return gcd(a-b,b);
return gcd(a,b-a);
}
The C compiler has compiled this procedure into the following code for an unpipelined Beta processor:
gcd:
PUSH (LP)
PUSH (BP)
MOVE (SP, BP)
PUSH (R1)
PUSH (R2)
LD (BP, -12, R0)
LD (BP, -16, R1)
CMPEQ (R0, R1, R2)
BT (R2, L1)
CMPLE (R0, R1, R2)
BT (R2, L2)
PUSH (R1)
SUB (R0, R1, R2)
PUSH (R2)
BR (gcd, LP)
DEALLOCATE (2)
BR (L1)
L2:
SUB (R1, R0, R2)
PUSH (R2)
PUSH (R0)
BR (gcd, LP)
DEALLOCATE (2)
L1:
POP (R2)
POP (R1)
MOVE (BP, SP)
POP (BP)
POP (LP)
JMP (LP)
A. The program above contains the instruction LD(BP,-16,R1). Explain what the compiler was trying
to do when in generated this instruction.
The compiler is loading the value of the second argument ("b") so it could compute "a == b".
B. What are the contents of the memory location holding the instruction BR(L1)?
BR(L1) is encoded as 0x77FF0007. Remember that PUSH is actually a macro that expands into
two
Beta instructions, so there are 7 instructions between L1 and L2.
C. When the instruction labeled "L1" is executed, what is the best characterization of the contents of
R0?