{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs313-2006-t2-problemset3

# cs313-2006-t2-problemset3 - CPSC 313 06W Term 2 Problem...

This preview shows pages 1–2. Sign up to view the full content.

CPSC 313 06W Term 2 Problem Set #3 Due: Sunday, February 4, 2007 at 11:59 PM (thirteen-hour grace period) 1. Write in C a function int gcd(int x, int y); that returns the greatest common divisor of its two arguments. You may assume that they are both positive. Your function should compute the gcd using subtraction only. The algorithm is: if x == y, gcd(x, y) = x if x > y gcd(x, y) = gcd(x - y, y) if x < y gcd(x, y) = gcd(x, y - x) Write four versions of the algorithm: a recursive version a version that uses a for loop a version that uses a while loop a version that uses a do . . . while loop Compile each version with gcc -S -O2. (a) Turn in (on paper) all 4 versions of your .c files and their corresponding, gcc generated, .s files. (b) Describe the differences in the generated code of the four versions. Don’t worry about trivial syntactic differences (like one version chooses register %ebx to hold some value while another version chooses a different register. You should look for differences that matter (semantic differences) such as: i. Differences in the number of registers used inside the loop ii. Differences in the number of branch instructions inside the loop Also note versions that are identical except for trivial syntactic differences.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

cs313-2006-t2-problemset3 - CPSC 313 06W Term 2 Problem...

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

View Full Document
Ask a homework question - tutors are online