>>33
also to add some application to the loops:
[ @ ~/host/prog/story ] $ cat st.c
#include <stdio.h>
void forloop(void)
{
int i;
for(i=0;i!=8;i+=2)
puts(">>33 is a faggot");
}
void whileloop(void)
{
int i = 0;
while(1){
puts(">>33 is a faggot");
i+=2;
if(i == 8)
break;
}
}
int main()
{
forloop();
whileloop();
}
gcc -S st.c
[ Mon Dec 26 12:19:33 ]
[ @ ~/host/prog/story ] $ cat st-O0.s
.file "st.c"
.section .rodata
.LC0:
.string ">>33 is a faggot"
.text
.globl forloop
.type forloop, @function
forloop:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -4(%rbp)
jmp .L2
.L3:
movl $.LC0, %edi
call puts
addl $1, -4(%rbp)
.L2:
cmpl $8, -4(%rbp)
jne .L3
leave
ret
.cfi_endproc
.LFE0:
.size forloop, .-forloop
.globl whileloop
.type whileloop, @function
whileloop:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -4(%rbp)
.L7:
addl $2, -4(%rbp)
cmpl $8, -4(%rbp)
je .L10
.L6:
movl $.LC0, %edi
call puts
jmp .L7
.L10:
nop
.L9:
leave
ret
.cfi_endproc
.LFE1:
.size whileloop, .-whileloop
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
call forloop
call whileloop
leave
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
gcc -O3 -S st.c
[ Mon Dec 26 12:19:44 ]
[ @ ~/host/prog/story ] $ cat st.s
.file "st.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string ">>33 is a faggot"
.text
.p2align 4,,15
.globl whileloop
.type whileloop, @function
whileloop:
.LFB12:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
addq $8, %rsp
jmp puts
.cfi_endproc
.LFE12:
.size whileloop, .-whileloop
.p2align 4,,15
.globl forloop
.type forloop, @function
forloop:
.LFB11:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
addq $8, %rsp
jmp puts
.cfi_endproc
.LFE11:
.size forloop, .-forloop
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB13:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
call puts
movl $.LC0, %edi
addq $8, %rsp
jmp puts
.cfi_endproc
.LFE13:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
As you can see with O3 the compiler is smart enough to optimize both loops to not even do a loop or any jumps and just does 4 prints and then in the main method it just does 8 prints instead of even using call on those two methods.