Wasn't it at some stage, with x86 assembly at least, that a comparison with zero (jump-zero instruction) was faster than comparing with any arbitrary integer? This could have had (aeons ago) an effect in looping through an (edit: zero-index)-array (and preferring the backwards loop) ?

It actually depends on the machine implementation. A forward loop from 0 to N-1 may end up with assembler code similar to this pseudo-code:

mov r1,0 // sometimes, xor r1,r1 needs less program space and/ +or time mov r2,N loop: // do something with r1 inc r1 // may or may not set flags, depending on the impleme +ntation cmp r1,r2 // often imlemented as sub r1,r2 but without actually + writing the result back to the register, just setting flags jne loop // if implemented as sub r1,r2, jne (jump if not rqua +l) is equal to jnz (jump if not zero)

If the machine allows compare with a constant, one register may be sufficient:

xor r1,r1 loop: // do something with r1 inc r1 cmp r1,N jne loop

The same loop running backwards from N down to 1 may be more efficient IF the decrement operation sets the zero flag:

mov r1,N loop: // do something with r1 dec r1 // must set/clear zero flag depending on value of r1 jnz loop

On the Z80, there is a special instruction DJNZ (decrement B and jump if non-zero) for exactly this purpose. It does not even affect the flags, it's all handled internally. The 8051 also has a DJNZ that can work on memory or a choosen register. The x86 processors have LOOP/LOOPE/LOOPNE. LOOP is like DJNZ using CX, ECX or RCX, LOOPE and LOOPNE additionally check the zero flag.

x86 processors have the REP/REPZ/REPNZ prefixes for a similar purpose. It repeats the follwing string instruction until CX/ECX/RCX is zero. REPZ also aborts if the zero flag is cleared, REPNZ also aborts if the zero flag is set. String operations are INS (read from I/O port to memory), MOVS (copy memory, i.e. memcpy, strcpy), OUTS (write from memory to I/O port), LODS (read from memory to accumulator), STOS (write accumulator to memory, i.e. memset), CMPS (compare memory, i.e. strcmp, memcmp), SCAS (compare memory to accumulator, i.e. strchr, strlen).

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^2: I prefer my indexes to start at: by afoken
in thread I prefer my indexes to start at: by Arunbear

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.