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 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.cto 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.cto 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.cto 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.cto 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
Turn in a zip file called |