Roger Kraft Purdue University Calumet ------------------------------------------------------------------------ A C program can have the following kinds of variables. 1) Global variables: a) default global variables i) initialized ii) uninitialized b) static global variables i) initialized ii) uninitialized c) external (extern) global variables 2) Local variables: a) automatic local variables i) initialized ii) uninitialized b) static local variables i) initialized ii) uninitialized c) formal parameters d) external local variables 3) Dynamic variables NOTE: For each of the above kinds of variables, you should be able to specify its scope and its lifetime. ------------------------------------------------------------------------ A running C program is made up of the following parts. 1) Code: a) your code b) library code c) operating system code 2) Data: a) initialized data segment b) uninitialized data segment c) read-only data segment d) stack e) heap NOTE: Each kind of C variable mentioned in the first part is stored in one of the 5 data areas listed just above. You should be able to specify which kind of variable is stored in which kind of data area. ------------------------------------------------------------------------- Here are the steps involved in converting a C source code file into a running program. Along with each step is listed some tools that you can use to observe the result of that step. Using these tools, you can take a simple C program and observe into which data area listed above each of the kinds of variables listed in the first part above gets stored. C Source Code -------> text editor | | | Compiler | | Assembly Source Code --> text editor | | | Assembler (built into the compiler) | | obj file ---------> Dumpbin (comes with Visual Studio) | PEview (www.magma.ca/~wjr/) | | Linker | | exe file ---------> Depends (www.dependencywalker.com) | PEBrowse (www.smidgeonsoft.com) | Dumpbin | PEview | | Loader | | runtime image ------> Visual Studio debugger Process Viewer (www.prcview.com) Process Explorer (www.sysinternals.com) PEBrowseDbg (www.smidgeonsoft.com) Process Walker (from MS Platform SDK Examples) API Monitor (www.rohitab.com) NOTE: One interesting thing to note is that at every step of the above process you can use at least one of the tools listed to get an "assembly language" listing of the result of that step. Take a simple program, look at its assembly language at each of these steps and notice how the assembly language is modified a bit at each step. In particular, each step resolves more symbols into addresses than the previous step. -------------------------------------------------------------------------------- Here are some command line tools (given in TextPad's syntax) that let you implement the steps described above. Tools are given for the MinGW and Microsoft Visual Studio compilers. 1.) To compile a C program and get its assembly language listing, use the following command lines. In each case, the compiler will produce an object file with the extension "obj" and the assembly language listing will be put in a file with the extension "asm". gcc -c -g -Wall -fverbose-asm -Wa,-a,-adhlns=$BaseName.asm -o $BaseName.obj $File cl /c /Od /Zi /W1 /nologo /FAsc /Fa$BaseName.asm /MDd $File 2.) To look at the assembly language listing contained in the obj file (which will not be quite the same as the assembly language listing produced by the compiler) use the following commands with lcc, gcc, and cl respectively. objdump -S -t $Basename.obj dumpbin /DISASM /SYMBOLS $BaseName.obj 3.) To link the obj file and produce an exe file, use the following commands. gcc -Wall -g -o $BaseName.exe $BaseName.obj link $BaseName.obj 4.) To look at the assembly language listing contained in the exe file use the following commands with lcc, gcc, and cl respectively. objdump -S -t $Basename.exe dumpbin /DISASM $BaseName.exe