(Just as a note, the count-down condition is normally (i = NUMBER; i--; ), the condition you have gives different results: i is 1 larger than one expects.)
> Without optimizations turned on, gcc translates both of them to cmpl operations followed by a jne
Why would you expect anything different? It isn't doing any optimisations, so it is doing the naive method of transliterating the C to ASM.
The actual test is when one turns optimisations on. The code generated by GCC -O3 for your two conditions is (respectively):
.L21:
addl $1, %eax
cmpl %edx, %eax
jne .L21
and
.L13:
subl $1, %eax
jne .L13
And for the condition I gave above
.L7:
subl $1, %eax
cmpl $-1, %eax
jne .L7
You can make your own conclusions from that, but clearly your count-down method uses fewer instructions.
(Also, one can test for 0 extremely quickly: just check that all the bits are 0.)
You have to add these suffixes "l" or "w" to all your instructions to tell the compiler how big they are, but of course if you're using EAX you're talking about a 32-bit register; if you wanted to talk about a 16-bit register, you'd say AX, or AL (or even AH) for 8-bit. An assembler that isn't smart enough to figure out something that simple and obvious is a sorry tool indeed.
And then there's the fact that operands are backwards. "subl $1,eax" translates to "eax -= 1" in C-like syntax. Why switch the order of operands? It's completely unnecessary and confusing. It's like someone inventing a programming language where integer literals were negative if specified with no sign, and only positive if you put a + on them -- technically possible, but the sort of thing that serves no useful purpose and defies all prior conventions, common sense, and sanity.
And putting dollar signs on constants and percent signs on registers? I can't even imagine any possible explanation for that, other than a lazy parser author who didn't care about making extra work for their users. It's the type of corner-cutting that you'd expect to find in some prototype thrown together by a single person in a single evening.
Please, for the sake of your own sanity and those around you, use Intel syntax instead!
> Without optimizations turned on, gcc translates both of them to cmpl operations followed by a jne
Why would you expect anything different? It isn't doing any optimisations, so it is doing the naive method of transliterating the C to ASM.
The actual test is when one turns optimisations on. The code generated by GCC -O3 for your two conditions is (respectively):
and And for the condition I gave above You can make your own conclusions from that, but clearly your count-down method uses fewer instructions.(Also, one can test for 0 extremely quickly: just check that all the bits are 0.)