experchange > asm

T. Ment (02-24-20, 07:30 PM)
Say you have

xor cx, cx ; set cx=0
a1: nop ; body of loop
nop
loop a1

Does the body of the loop run 64k times, or 64k-1?

It was not obvious to me, I had to analyze it. Anyone want to guess, or
already know?
Herbert Kleebauer (02-24-20, 08:35 PM)
On 24.02.2020 18:30, T. Ment wrote:
> Say you have
> xor cx, cx ; set cx=0
> a1: nop ; body of loop
> nop
> loop a1
> Does the body of the loop run 64k times, or 64k-1?


Why didn't you read the manual?

LOOP (Loop While ECX Not Zero) is a conditional transfer that automatically
decrements the ECX register before testing ECX for the branch condition. If
ECX is non-zero, the program branches to the target label specified in the
instruction. The LOOP instruction causes the repetition of a code section
until the operation of the LOOP instruction decrements ECX to a value of
zero. If LOOP finds ECX=0, control transfers to the instruction immediately
following the LOOP instruction. If the value of ECX is initially zero, then
the LOOP executes 2^(32) times.
JJ (02-24-20, 08:39 PM)
On Mon, 24 Feb 2020 17:30:24 +0000, T. Ment wrote:
> Say you have
> xor cx, cx ; set cx=0
> a1: nop ; body of loop
> nop
> loop a1
> Does the body of the loop run 64k times, or 64k-1?
> It was not obvious to me, I had to analyze it. Anyone want to guess, or
> already know?


`loop` won't loop when CX is one prior the execution, so that'll be 65535.
But because the body is executed before the first `loop` instruction
execution, add it by one, so it's 65536. Otherwise with code like below,
it'll be only 65535.

xor cx, cx ; set cx=0
jmp a2
a1: nop ; body of loop
nop
a2: loop a1
T. Ment (02-24-20, 08:59 PM)
On Mon, 24 Feb 2020 19:35:44 +0100, Herbert Kleebauer wrote:

> Why didn't you read the manual?


You can't trust everything printed in a manual. Except the Bible.
T. Ment (02-24-20, 09:16 PM)
On Tue, 25 Feb 2020 01:39:16 +0700, JJ wrote:

>`loop` won't loop when CX is one prior the execution


Right. You can see that with a debugger. That proves cx is decremented
before it's tested.

Thus the initial value of cx is how many times the BODY will run. If you
set cx = 65535, that will be 64k-1. To get one more iteration (64k), add
1, which rolls cx over to 0. Thus 0 is equivalent to 64k for purposes of
the loop instruction.

I can remember the proof. Just reading it in a manual, I'll forget that
in a few weeks. It may seem trivial, but the wrong answer can produce an
off by one error, depending on what you are trying to do.
wolfgang kern (02-27-20, 11:30 AM)
On 24.02.2020 18:30, T. Ment wrote:
> Say you have
> xor cx, cx ; set cx=0
> a1: nop ; body of loop
> nop
> loop a1
> Does the body of the loop run 64k times, or 64k-1?
> It was not obvious to me, I had to analyze it. Anyone want to guess, or
> already know?


LOOP with cx=0 iterates 65536
REP with cx=0 falls through
__
wolfgang
T. Ment (02-27-20, 03:58 PM)
On Thu, 27 Feb 2020 10:30:24 +0100, wolfgang kern wrote:

> LOOP with cx=0 iterates 65536
> REP with cx=0 falls through


I didn't think of that. Good to know the difference.
Similar Threads