A Minimal C Program

 < Day Day Up > 



A Minimal C++ Program

The following source code is an example of the smallest C++ program you can write.

int main(){ }

The program doesn’t do much yet everything is there. Every C++ program must have one, and only one, main() function. The main() function must return an integer value. The value returned by the main() function is a result code intended for use by the operating system. The result code gives some indication of how the program terminated. Readers with some experience will note the absence of a return statement in the body of main(). A return statement is not necessary because the compiler does the job for you by default. You can compile and run this minimal program but on most computers you’d receive no indication it had executed.

Example 5.1 gives a disassembled version of the minimal main() function targeted for a PowerPC processor using Metrowerks CodeWarrior’s disassembly feature.

Listing 5.1: Disassembled Minimalmain() Function

start example
Names:         1: .main         2: main         3: TOC Hunk:   Kind=HUNK_GLOBAL_CODE    Align=4  Class=PR  Name=".main"(1)  Size=8 00000000: 38600000  li          r3,0 00000004: 4E800020  blr Hunk:   Kind=HUNK_GLOBAL_IDATA   Align=4  Class=DS  Name="main"(2)  Size=8 00000000: 00 00 00 00 00 00 00 00                         ‘........’ XRef:   Kind=HUNK_XREF_32BIT     Offset=$00000000  Class=PR  Name=".main"(1) XRef:   Kind=HUNK_XREF_32BIT     Offset=$00000004  Class=TC0 Name="TOC"(3) Hunk:   Kind=HUNK_GLOBAL_IDATA   Align=4  Class=TC0 Name="TOC"(3)  Size=0
end example

If you feel intimidated by example 5.1 don’t be. It looks complicated because to understand everything shown requires some knowledge of the PowerPC processor and the Macintosh G4 system architecture, but for now, a quick explanation of what’s going on will suffice.

Example 5.1 contains five sections but I will only discuss the first two sections for now. Starting from the top, the first section is a name table containing three entries: .main, main, and TOC. (TOC stands for Table of Contents) The next section, prefixed with the label Hunk: is the code section. Its class is PR, meaning it is a Read/Write Control Section and its name is .main. In this section there appear two instructions. The first instruction, li, located at offset 00000000, is an extended PowerPC mnemonic meaning Load Immediate. Further to the right of the li instruction are two operands, r3, which stands for register 3, and 0 which is the integer value zero. When this line is executed the integer value zero will be loaded into register 3.

The next line contains the extended mnemonic blr which stands for Branch Link Register. The link register is a special register in the PowerPC processor that will contain the memory address of the next instruction of the calling routine. When this line is executed the program will return, or jump back to the program that called it. The fact that the calling routine requires the result code to be located in register 3 upon program termination is a matter of protocol.

Putting the two instructions together yields the following behavior from the minimal main() function: When the program is run, the value zero is loaded into general purpose register 3 and the program jumps back from whence it came. The calling program may or may not make use of the result code.

Disassembly is a Great Learning Tool

A great way to dig deeper into the workings of your computer and the C++ language is to disassemble your source code. Example 5-1 was generated with CodeWarrior’s disassembly feature. To set the desired output format for disassembled code listings from the Edit menu select the PPC Std C++ Console Settings... item as shown in Figure 5-1.

click to expand
Figure 5.1: Selecting Std C++ Console Settings

This will bring up the PPC Std C++ Console Settings dialog box shown in figure 5-2. In the Code Generation sec-tion select the PPC Disassembler and select the options shown. It is also a good idea to select the other options and then disassemble the source code to see what effects each option has on code generation.

click to expand
Figure 5.2: PPC Std C++ Console Settings Dialog

If you are targeting a PC then the options you will see when editing the Std C++ Console Set-tings will be different from those shown here. Regardless, the concepts are the same.

Once you have selected the desired disassembler options save the settings and exit the Std C++ Console Settings dialog. You are now ready to dis-assemble a source file.

Select the source file you wish to disassemble from those listed in your project window. For this example I created a project called Minimal Program, created one file called main.cpp that contains the simple main() function and located it in the Sources group. The Minimal Program project window is shown in figure 5-3 with the main.cpp file highlighted.

click to expand
Figure 5.3: Minimal_Program Project Window

Next choose Disassemble from the Project menu as shown in figure 5-4.

click to expand
Figure 5.4: Selecting Disassemble from the Project Menu

Disassembling the main.cpp file results in the output shown in example 5-1 above. That’s all there is to it! Use the disassemble feature to explore your code to get a better idea of how it works on the inside.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net