Homework Assignment 5
CS 316, Spring 2003
Due on Tuesday, March 4, 2003

This assignment has three problems. The first problem asks you to use information from the assembly language listing of a C program to determine what the C program is doing.

The second problem asks you to use the assembly language listing of a C program to explain what "tail recursion optimization" does.

The third problem asks you to analyze a C program that has a tail recursive function and explain why the compiler cannot use "tail recursion optimization".

1.) For this problem you MUST use the LccWin32 C compiler. Download this C program, ascii.c. Compile (using lcc) and run the program and notice its output. Now make the change of CONSTANT1 from 222 to 223. Compile and run the program again. Now the program is in an infinite loop. Use the assembly language listing of the program produced by lcc (called ascii.asm) to explain in very careful detail why the program is now in an infinite loop. The exact cause of the infinite loop has to do with how the compiler put the local variables on the stack. To help you figure this out, add printf statements to the program to watch the value of cp. What is the address of cp itself?

Now make a second change in the program, change the expression cp-c+33 to cp-c+32 (keeping CONSTANT1 at 223). Now the infinite loop is gone. Why? Explain in detail making reference to how the local variables are stored on the stack.

2.) For this problem you MUST use the MinGW gcc compiler. Download this C program tail-factorial.c. This program is a tail recursive version of the factorial function. At a command prompt, use the command

c:\mingw\bin\gcc tail-factorial.c
to compile the program without "tail recursion optimization". The executable is called a.exe (the program will crash when it runs). Use the command
c:\mingw\bin\gcc -S tail-factorial.c
to get the assembly language listing (called tail-factorial.s) of the program.

Now use the command

c:\mingw\bin\gcc -O2 tail-factorial.c
to compile the program with "tail recursion optimization". Now the program can run without crashing (remember, the executable is called a.exe). Use the command
c:\mingw\bin\gcc -S -O2 tail-factorial.c
to get the assembly language listing of this program compiled with the optimization.

Use the two assembly language listings to explain why the first compiled version of the program crashed and why the second one does not. Explain as carefully as you can how the compiler changed the compiled version of the program to make it "properly" tail recursive.

3.) Download this C program weird-tail-recursion.c. You can compile and run this program with any C compiler. This program uses tail recursion but a compiler cannot use "tail recursion optimization" with this program. Explain why this is true. You do not need to look at the assembly language listing to answer this. Just study the C code carefully and think about how the program works. Draw a picture of what several stack frames look like after several recursive calls of the procedure create_nodes(). What would go wrong if a compiler tried to implement tail recursion optimization as in the previous problem?


Return to the main homework page.
Return to the CS 316 home page.


compliments and criticisms