CMPSC 311, Introduction to Systems Programming

How are functions implemented in C and C++?



Remember the course mantra, System programming requires you to be aware of the resources that your program uses.

This is a lesson about reading assembly code (which may also include guessing what it means, if you've never seen anything like this before), and how functions are translated to assembly code, starting from C and C++.  The "resources" here are the names known to the program.  We are concerned about function overloading and default arguments in C++, which affect how function names are constructed.

There are some exercises at the end; the ones marked *** are essential to see if you understand what's going on with these programs.



Reading
Source code
Test systems
Compilers
Compiler options used here (see Makefile for the commands)
Assembly Language output -- various systems and compilers, optimized (.1) or not (.0)

system-compiler-language
             source     unopt.  (lines)   opt.    (lines)

imc-gcc-c89  foo.c      foo.0.s   (59)    foo.1.s   (58)
        c99 
foo.c      foo.0.s   (59)    foo.1.s   (58)
        cpp  foo.cpp    foo.0.s  (144)    foo.1.s  (136)
lnx-gcc-c89 
foo.c      foo.0.s   (13)    foo.1.s   (13)
        c99 
foo.c      foo.0.s   (13)    foo.1.s   (13)
        cpp 
foo.cpp    foo.0.s   (49)    foo.1.s   (49)
pmc-gcc-c89 
foo.c      foo.0.s   (17)    foo.1.s    (9)
        c99 
foo.c      foo.0.s   (17)    foo.1.s    (9)
        cpp 
foo.cpp    foo.0.s   (77)    foo.1.s   (38)
sun-gcc-c89 
foo.c      foo.0.s   (17)    foo.1.s   (13)
        c99 
foo.c      foo.0.s   (17)    foo.1.s   (13)
        cpp 
foo.cpp    foo.0.s   (63)    foo.1.s   (40)
sun-sun-c89 
foo.c      foo.0.s   (99)    foo.1.s   (98)
        c99 
foo.c      foo.0.s   (99)    foo.1.s   (98)
        cpp 
foo.cpp    foo.0.s  (148)    foo.1.s  (125)



Exercises

1.  Read through a few of the .s files above.  Which lines refer to the following features?  (At this point in the course, and with these examples, simply looking for patterns is probably the right approach.  For the Intel and AMD processors, CS:APP Ch. 3 explains the instructions and more.)
2.  Assemblers work by writing bytes into various sections of an object file.  Object files are linked together to form an executable file.  Loaders work by writing bytes from an executable file into various sections of memory.  The assembly language (in the .s files) specifies which section to write into next by using directives that look like .section.  Use the fgrep command to extract all lines of the .s files in which ".section" appears.  Explain what you see.
We haven't covered the grep command family yet, so here's the command
   fgrep .section */*.s
and its result.

3.  Use the grep command to extract all lines of the .s files in which "foo" appears.  Explain what you see.
We haven't covered the grep command family yet, so here's the command
   grep foo */*.s
and its result.

4.  *** Use the egrep command to extract only those lines of the .s files in which "foo" and ":" appear on the same line.  Explain what you see.  In particular, explain how function overloading is implemented in C++.
We haven't covered regular expressions yet, so here's the command
   egrep 'foo.*:' */*.s
and its result.

The quotes '' are necessary to prevent the command shell from expanding foo.*: as a wildcard file name.  We'll cover more details like this later in the course.

5.  *** Now consider the C++ program bar.cpp compiled with  g++ -S bar.cpp  producing bar.s (on Linux), and bar.s (on Solaris).  How are default arguments implemented in C++?  How does this example differ from the function overloading example foo.cpp?



Last revised, 7 Jan. 2012