stacks - 6.004 Stacks and procedures Fall 2009...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
6.004[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?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.004[12/1/2009 9:21:59 PM] L1 is just at the beginning of the procedure return sequence, so at that point R0 should contain the value to be returned to the caller. D. Looking at the code, a student suggests that both DEALLOCATE instructions could be eliminated since deallocation is performed implicitly by the MOVE(BP,SP) instruction in the exit sequence. After calling gcd, would it be possible to tell if the DEALLOCATE instructions had been removed? Yes, it would be possible to tell: even though the stack is reset correctly by the MOVE(BP,SP) instruction, that happens after the POP(R1) and POP(R2) instructions, so they will not restore the values of R1 and R2 from the values pushed onto the stack during the entry sequence. E. How many words of stack are needed to execute gcd(24,16)? Don't forget to include the stack space occupied by the arguments in the initial call. gcd(24,16) => gcd(8,16) => gcd(8,8), at which point no more recursive calls are made. So three nested procedure calls are executed, each with a 6-word stack frame (2 args, LP, BP, saved R1, saved R2), for a total of 18 words of stack space. F. During execution of gcd(28,70), the Beta processor is halted and the contents of portions of the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2010 for the course ECE 585 taught by Professor Faust during the Fall '08 term at Portland State.

Page1 / 17

stacks - 6.004 Stacks and procedures Fall 2009...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online