1

The question in the book shows the following loop:

    mov ecx, -1
forD: .
      .                ; loop body
      .
      loop forD

The question asks "how many times is each loop body executed?"

The answer in the back of the book says 4,294,967,295, but why? What's the significance of this number? Is this supposed to be a never-ending-loop?

phuclv
  • 37,963
  • 15
  • 156
  • 475
user3418284
  • 63
  • 1
  • 8
  • If you wanted to write a never-ending loop, you'd `jmp forD` to unconditionally branch, instead of using a conditional branch on a loop counter. The `loop` instruction is only useful for code-size optimization in a few special cases, and on Intel CPUs is slower than a normal dec/jnz – Peter Cordes Jul 09 '19 at 06:32

2 Answers2

3

loop works as follows:

  1. decrement (e)cx by 1
  2. check if it is 0
  3. if not, jump to the specified offset/label
  4. if 0, continue with the next instruction

-1 equals to 4294967295 unsigned, which in turn results in that number of loop iterations.

There are some catches using loop:

  • Loop first decreases the counter register.Putting there 0 will not result in zero repetitions but in 4294967296, because the first decreasing will result in -1 in (e)cx. Accordingly, putting 1 there will result in zero repetitions. Please note that this still executes the looped instructions once - if the loop destination is before the loop instruction.

  • Loop is a 2byte opcode. First byte is E2 for the instruction itself, so there is only one byte left for jump destination offset. That offset is signed, so you can only loop within -128..127 bytes distance.

nico gawenda
  • 3,648
  • 3
  • 25
  • 38
2

-1 is 0xFFFFFFFF in 32-bit 2's complement, which is 4294967295 (232 - 1) in unsigned decimal

The loop time depends on the loop body. But in current CPUs you can run a short loop several billion times within a few seconds or less

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • So if the -1 was, for example the other questions in the text: 10, 1, and 0, then the loop would repeat 10, 1, and 0 time respectively? – user3418284 Apr 01 '14 at 03:26
  • yes. You should read the function of the loop instruction in Intel's manual – phuclv Apr 01 '14 at 03:29
  • 1
    The question is about the iteration count, not performance. But yes, even CPUs where [the `loop` instruction is slow](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) (everything except recent AMD) can run it in a *few* seconds, vs. about 1 second for a normal `dec ecx/jnz` loop on a 4.3GHz CPU. – Peter Cordes Jul 09 '19 at 06:29