GCC generates assembly equivalent to continuing statement in C

When the continuation operator is used inside a loop in C code, GCC creates a new label with the nop instruction just before the end of the loop and jumps to it rather than going to the end of the loop itself. For example, the following C code

for (i=0; i<10; i++) {
    puts("blah\n");
    if (i < 10) continue;
    puts("This shouldn't be printed.\n");
}

creates the following ASM equivalent (using gcc -S):

    movl    $0, 28(%esp)
    jmp L2
L5:
    movl    $LC0, (%esp)
    call    _puts
    cmpl    $9, 28(%esp)
    jle L7
L3:
    movl    $LC1, (%esp)
    call    _puts
    jmp L4
L7:
    nop
L4:
    incl    28(%esp)
L2:
    cmpl    $9, 28(%esp)
    jle L5

(the if (i <10) part is inserted so that the compiler does not "optimize" the section by deleting everything that follows the continue statement)

My question is: why not go straight to L4? IMO, we could also jump onto L4, am I missing something?

+5
source share
2 answers

, , - . , gcc (-O1 ), , .

+4

, - . , nop .

, , nop - .

+1

All Articles