Linux Kernel Architecture

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

Appendix C: Notes on C


status variable at the end of the loop body and then checking its value. If it is less than 3, the loop is
restarted (a branch is made to the beginning of the loop body); otherwise, the code following the loop is
executed. Without optimization, the generated assembler code looks like this:

.file "loop.c"
.section .rodata
.LC0:
.string "Pass: %d\n"
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $0, -4(%ebp)
.L2:
cmpl $2, -4(%ebp)
jle .L5
jmp .L3
.L5:
movl -4(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
leal -4(%ebp), %eax
incl (%eax)
jmp .L2
.L3:
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2.1"

If the number of loop passes is small, the code is often executed more quickly if the assembler code of
the loop body is written to the output fileseveral times in succession. There is then no need to compare the
status variable with the end value, and the conditional branch can be dispensed with. If optimizedloop
unrollingis used, the GCC generates the following code:

.file "loop.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Pass: %d\n"
.text
.p2align 4,,15
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
Free download pdf