Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1177

Appendix C: Notes on C


merging of repeated expressions and items of code in a program, rewriting of program flow into
a more efficient form, and so on — these are covered as individual topics in this appendix.
❑ Code generation— The last phase is concerned exclusively with the generation of the actual
assembler code for the target processor. However, this doesnot yetproduce an executable
binary file, but instead, it produces a text file with assembler instructions that is converted
into binary machine code by further external programs (assemblers and possibly linkers). In
principle, the assembler code has the same form as the code of the final program but can still be
read by humans (not by machines) even if the power of the individual commands has reached
machine level.

To provide you with a general overview of the various compiler steps involved, this appendix uses a
classical example — the ‘‘Hello, World‘‘ program.


#include<stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

The program does nothing more than output the lineHello, World!and is typically the first program
discussed in any C textbook. On IA-32 systems, the following assembler code is generated for further
processing by the assembler and linker:


.file "hello.c"
.section .rodata
.LC0:
.string "Hello, World!\n"
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"

If you are already familiar with assembler programming, you may be surprised by the somewhat strange
form of the code. The GNU assembler employs the AT&T syntax instead of the more-widespread
and therefore better-known Intel/Microsoft variant. Of course, both alternatives implement the same
functionality but use different arrangements of source and destination registers and different forms of
constant addressing. Section C.1.7 provides abrief description of these syntax elements.

Free download pdf