Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: I prefer my indexes to start at:

by bliako (Monsignor)
on Sep 09, 2022 at 08:55 UTC ( [id://11146778]=note: print w/replies, xml ) Need Help??


in reply to I prefer my indexes to start at:

I have just noticed that my keyboard's horizontal row of numbers starts from 1. I don't remember seeing any keyboard with a row of: 0-1-2...-9. But my keyboard's keypad has zero next to 1 (at the bottom as a huge button).

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) ?

bw, bliako

Replies are listed 'Best First'.
Re^2: I prefer my indexes to start at:
by afoken (Chancellor) on Sep 09, 2022 at 19:24 UTC
    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". ;-)
Re^2: I prefer my indexes to start at:
by afoken (Chancellor) on Sep 09, 2022 at 18:44 UTC
Re^2: I prefer my indexes to start at:
by LanX (Saint) on Sep 09, 2022 at 21:01 UTC
    AFAIR there always was a dedicated command for comparing with zero - for all flavors of "comparison" - because it's quite easy to implement and happens sufficiently often (e.g. when decrementing down).

    And this command would also be shorter in length, since it doesn't need to read a constant from memory to compare with. And shorter length will also add up to the speed gain.

    Now even if you used the longer command to compare with 0, a good assembler could try to optimize (think constant folding) a "comparison to a constant 0" to the shorter and faster "zero-compare"

    I learned several assemblers, mainly MOS 6502 and Motorola 68000, but that was too long ago to tell you now where I've experienced this and give the correct mnemonics.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146778]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-03-28 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found