10

What's the difference between the assembly instructions LOOP, LOOPE and LOOPNE?

4
  • 2
    Don't use these instructions; they are very slow on modern CPUs. Instead use branching by hand. Commented Nov 23, 2009 at 8:22
  • 1
    @Alex Strange: do you have any evidence to support your statement? Thanks. Commented Oct 26, 2010 at 11:23
  • 1
    @Timotei Dolean: See the instruction tables in agner.org/optimize. A CPU textbook that discusses microcoding (and hopefully some do) will explain the reasoning. Commented Oct 26, 2010 at 22:25
  • @alexstrange: related: Why is the loop instruction slow? Couldn't Intel have implemented it efficiently? has some uop counts and throughput numbers for loop on various recent microarchitectures, and some of the history behind how we ended up in this catch-22 situation of: nobody uses it because it's slow / not worth making faster because nobody uses it. If it was fast, it would often save code size, and be great for adc loops (especially on CPUs with partial-flag stalls like Nehalem and earlier.) Commented May 28, 2018 at 5:51

4 Answers 4

21

LOOP decrements ecx and checks if ecx is not zero, if that condition is met it jumps at specified label, otherwise falls through.

LOOPE decrements ecx and checks that ecx is not zero and ZF is set - if these conditions are met, it jumps at label, otherwise falls through.

LOOPNE is same as LOOPE except that it requires ZF to be not set (i.e be zero) to do the jump.

Sign up to request clarification or add additional context in comments.

5 Comments

Also not asked I'd like to point out that all LOOP instructions are much slower than the DEC ECX / JNZ counterpart. This is intended as LOOP should nowadays only be used for delay calibration loops used for hardware-drivers and the like.
@NilsPipenbrinck: On which processors is it slower? What's your source?
@JanusTroelsen, its slower from the 80486 onwards. On the lastest processors it's a lot slower. Source: agner.org/optimize manual #2.
@sharptooth, speaking of LOOPE, how after decrementing can ECX be non zero and ZF set? Does LOOPE not affect the ZF flag?
Answering my own question. After checking it in gdb I can confirm that none of the loop (LOOP, LOOPE, LOOPNE) instruction affect the ZF flag when they decrement the ECX counter. Now it makes sense.
5

Time for a Google Books Reference

EDIT: Synopsis from link: LOOPE and LOOPNE are essentially LOOP instructions with one additional check. LOOPE loops "while zero flag," meaning it will loop as long as zero flag ZF is one and the increment is not reached, and LOOPNE loops "while not zero flag," meaning it continues the loop as long as ZF is zero and the increment is not reached. Keep in mind that neither of these instructions inherently affect the status of ZF.

1 Comment

I believe that it is best to not only provide a link, but quote relevant material from the source, should the link ever become invalid.
1

The LOOP instructions, as well as JCXZ/JECXZ are a bit slow; however, they still have their place in modern code.

High speed is not always a concern in loops. For example, if we are executing a loop only once during program init and the iteration count is small, the time required will not be noticed.

Another example is a loop where Windows API functions are called; the time spent in the API call probably makes the LOOP execution time trivial. Again, this applies when the iteration count is small.

Consider these instructions as "another tool in your toolbox"; use the right tool for the job ;)

Comments

0

Have you tried looking it up in an instruction set reference, for example in this one by Intel?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.