CS 316 - Programming Assignment 5

This assignment has three parts. The first part is practice in reading the assembly language produced by a compiler. The problem asks you to use information from the assembly language listing of a C program to explain how C programs implement "copy constructor" like behavior.

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

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

This assignment is due on Tuesday, March 11.

Part 1.) The first problem uses this C program, copy-constructors.c. Notice that this program includes a function that takes a struct as an input and returns a struct. In the main() function there is a call to this function. Use an assembly language listing to explain how the C runtime implements the call-by-value of the struct parameter and how the struct return value is handled. After you figure out what is going on, annotate a copy of the assembly language listing to explain the code that the compiler produced. You may also find it useful to sketch a picture of the runtime stack and its stack frames. Use any one of the following gcc or cl compiler commands to get an assembly language listing of the program (you can also use the disassembler window in the Visual Studio debugger). Use whichever one you find most useful

gcc -S copy-constructors.c
gcc -S -fverbose-asm copy-constructors.c
gcc -c -fverbose-asm -Wa,-a,-adhlns=copy-constructors.asm copy-constructors.c
cl /c /nologo /Facopy-constructors.asm copy-constructors.c
cl /c /nologo /FAs /Facopy-constructors.asm copy-constructors.c
cl /c /nologo /FAc /Facopy-constructors.asm copy-constructors.c
cl /c /nologo /FAsc /Facopy-constructors.asm copy-constructors.c

Part 2.) For this problem you must use the MinGW gcc compiler. This problem is about this C program, tail-factorial.c. This program is a tail recursive version of the factorial function. Use the command

c:\mingw\bin\gcc -o tail-factorial.exe tail-factorial.c
to compile the program without "tail recursion optimization". The program will crash when it runs. Use any one of the following three commands
c:\mingw\bin\gcc -S tail-factorial.c
c:\mingw\bin\gcc -S -fverbose-asm tail-factorial.c
c:\mingw\bin\gcc -c -fverbose-asm -Wa,-a,-adhlns=tail-factorial.asm tail-factorial.c
to get an assembly language listing of the program (each command produces a slightly different format; choose the one you like better).

Use the command

c:\mingw\bin\gcc -O2 -o tail-factorial.exe tail-factorial.c
to compile the program with "tail recursion optimization". Now the program can run without crashing. Use any one of the following three commands
c:\mingw\bin\gcc -O2 -S tail-factorial.c
c:\mingw\bin\gcc -O2 -S -fverbose-asm tail-factorial.c
c:\mingw\bin\gcc -O2 -c -fverbose-asm -Wa,-a,-adhlns=tail-factorial.asm tail-factorial.c
to get an assembly language listing of this program compiled with the optimization.

Use the two assembly language listings to figure 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. Use annotated copies of the assembly language listings to back up your explanations.

Part 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(). Explain what would go wrong if a compiler tried to implement tail recursion optimization as in the previous problem.

Turn in a zip file called CS316Hw5Surname.zip containing annotated assembly language listings that explain your answers to parts 1 and 2, your explanation for part 3, and also the original files contained in h5.zip. This assignment is due Tuesday, March 11.


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


compliments and criticisms