C is a high-level language, meaning its statements are oftentimes far-removed from the final machine encoding of the program. C statements provide a more intuitive means of communicating a programmer's wishes to the machine, in contrast to assembly--a low-level language, where the programmer must spell out to the machine even the most minute detail of a program. This chapter will serve as a brief introduction to the C programming language; a primer for those who know very little or none, and a reference for those who are familiar. However, this is not meant as an extensive introduction, as there are numerous books devoted to the C programming language. This chapter also attempts to illustrate the similarities between assembly and C, as well as demonstrating techniques of mixing the two languages together.
There are two forms of the C calling convention described in this chapter: 16-bit and 32-bit. As the only C compiler available on the lab machines is the 32-bit GNU C Compiler, this chapter will mainly focus on the 32-bit C calling convention. However, Section 8.3 contains a brief discussion of the 16-bit C calling convention in comparison to the 32-bit calling convention described in this chapter.
Most of this discussion assumes the use of the GNU C compiler version 2.95, which is similar to compilers offered from other major sources, but does contain some unique features and syntaxes.
Just as the NASM assembler generates machine code from assembly statements, the GNU C compiler generates machine code from C statements. To compile a program using GNU C, use the following command at the DOS prompt:
gcc [-c] {filename}
Compiling with the -c option creates an object file only, which must be linked using the gcc utility without the -c option to the appropriate libraries or other object files to create the final executable. This will be necessary when creating mixed-language programs such as those with both assembly and C.
As the GNU C compiler generates 32-bit machine code, the assembly code linked with its object files needs to use 32-bit registers and instructions. The final executable will be a 32-bit protected mode program (see the protected mode tutorial for more details on protected mode and how it's used in ECE 291).