Homework Assignment 8
CS 316, Spring 2003
Due on Thursday, May 8, 2003

This assignment has four problems about calling conventions and parameter passing in the C programming language.

1.) You should do this first exercise using Microsoft's Visual C/C++ compiler (though you can do part of the exercise using the LCC-Win32 compiler). Download the C program calling-conventions.c. Compile the program and look carefully at the assembly language listing from the compiler. Use the assembly language listing to document exactly how each of the three function definitions is different and exactly how each of the three function calls is different. What are some advantages and disadvantages for each of these calling conventions? You will probably find the following documentation from MSDN useful. Calling Conventions Overview

2.) You can do this exercise using either the LCC-Win32 compiler or Microsoft's Visual C/C++ compiler, but I will explain the assignment with respect to LCC-Win32. Download the two C source files calling-conventions2a.c and calling-conventions2b.c. These two files are supposed to be separate compilation units for a single C program. In other words, these two files are supposed to be compiled separately but their obj files are supposed to be linked together to form a single executable file. However, the linker will refuse to link the two obj files into an executable. Using the assembly language listings for the two source files, explain in detail exactly why the linker cannot link the two obj files. Also, explain exactly what would go wrong if the linker did link the two files together.

Here is how you use the LCC-Win32 linker to try to link the above two obj files. I will assume that you have LCC-Win32 installed to work with TextPad as explained in these installation instructions. First we need to make a slight change in the configuration of the TextPad tool for the lcc linker. In TextPad, click on the "Configure -> Preferences..." menu item. In the left pane of the preferences window, click on the little plus sign next to the "Tools" item in the tree view. Then click on the name of the tool for the lcc linker. In the right pane, where you can configure lcc's linker tool, click on the check box named "Prompt for parameters". Then click on the "OK" button (which closes the Preferences window). Now use the lcc compiler to compile each source file separately (so there is an obj file for each source file). Now make sure that you have the calling-conventions2a.c source file in TextPad's edit window, so that it is the active file. Use the Tools menu to call the lcc linker. A small dialog box will appear with a single text field where you can add parameters to the lcc linker's command line. At the end of the command line, add the name calling-conventions2b.obj. This tells the linker to try and use the file calling-conventions2b.obj to resolve any unresolved symbols from the file calling-conventions2a.obj. Be sure to look carefully at the error message returned by the linker.

3.) You can do this exercise using either the LCC-Win32 compiler or Microsoft's Visual C/C++ compiler. Download the two C source files parameter-passing-a.c and parameter-passing-b.c. These two files are separate compilation units for a single C program. Compile each source file, link the two object files together, then run the resulting executable. Answer the questions given in the file parameter-passing-a.c.

4.) You can do this exercise using either the LCC-Win32 compiler or Microsoft's Visual C/C++ compiler (but this exercise is probably easier to do with Visual C/C++ because of the debugger). Download the C source mischief.c. Follow the instruction contained in the source code file. The main idea is that you are to write a function that modifies the return address stored in its stack frame so that the function returns somewhere other than it is supposed to (this is the main idea behind "buffer overflow attacks" or "smashing the stack").


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


compliments and criticisms