Linux Kernel Architecture

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

Appendix C: Notes on C


Non-optimized assembler output looks like this:

.file "calc.c"
.section .rodata
.LC0:
.string "x, y, z: %d, %d, %d\n"
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $10, -4(%ebp)
movl -4(%ebp), %eax
addl $42, %eax
movl %eax, -8(%ebp)
movl -8(%ebp), %edx
movl %edx, %eax
addl %eax, %eax
addl %edx, %eax
sall $3, %eax
subl %edx, %eax
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
movl %eax, 12(%esp)
movl -8(%ebp), %eax
movl %eax, 8(%esp)
movl -4(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"

The value of the individual assignments is not clear at the outset but must first be computed (by means of
addition and multiplication). The results achieved in the various program runs do not differ because the
same initial values are always used. If optimization is switched off, the C code is compiled into assembler
code in a relative straightforward way. Two computations are performed and three variables are supplied
with values. If optimization is switched on, an additional constant appears in the assembler output: the
exact result of the computation (which in this case is 1196 ).

The optimized assembler code looks like this:

.file "calc.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "x, y, z: %d, %d, %d\n"
Free download pdf