Linux Kernel Architecture

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

Appendix C: Notes on C


movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $23, -4(%ebp)
cmpl $9, -4(%ebp)
jg .L2
movl $.LC0, (%esp)
call printf
jmp .L3
.L2:
movl $.LC1, (%esp)
call printf
.L3:
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"

Because the value of 23 assigned toxcannot change prior to theifquery, the result of the query is
obvious — the second program branch (theelseclause) always executes, and this renders explicit com-
putation to determine whetherxis less than or greater than 23 superfluous. The code for the first query
is therefore a dead program section because it can never be reached. Therefore, the compiler need not
compile the corresponding statements. But there is also a further benefit because the string constantis
less than 10!no longer needs to be stored in the object file. In addition to speeding program execution,
optimization also reduces the size of the generated code. Omission of the character string from the object
file is a relatively new optimization feature only supported by GCC Version 3 and higher.


The optimized assembler code looks like this:


.file "dead.c"
.section .rodata.str1.32,"aMS",@progbits,1
.align 32
.LC1:
.string "x is greater than or equal to 10!"
.text
.p2align 4,,15
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $.LC1, (%esp)
call puts
movl %ebp, %esp
popl %ebp
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"
Free download pdf