From: don on
What are the rules for determining the number of times that a loop will
occur given x and say you want
to test for x < y or x > y or x = y or x <> y
Where x would either start with 0 or 1 and you are going to be
incrementing x by 1 inside the loop either before the test or after the
test.....

In otherwords, I"m trying to figure out how to determine the number of times
that a loop would occcur based on the code.... I hope I"m making myself
clear......


From: Rod Pemberton on

"don" <don(a)panix.com> wrote in message
news:i08da4$8vd$1(a)reader1.panix.com...
> What are the rules for determining the number of times that a loop will
> occur given x and say you want
> to test for x < y or x > y or x = y or x <> y
> Where x would either start with 0 or 1 and you are going to be
> incrementing x by 1 inside the loop either before the test or after the
> test.....
>

I'm not going attempt signed values. I'm assuming y is constant.

For unsigned values, you usually use x<y when incrementing x upto y-1. x>y
is used for decrementing x down to y+1. You've stated you want to increment
x by 1 from 0 or 1, i.e., upto y, not decrement x. So, should we consider
x>y?

For unsigned values, if x starts with 0 and 1, x is incremented, x>y will be
always be false unless x=1, y=0. I.e., it won't loop - except for x=1, y=0
which will loop forever. x>y should used for decrementing, not
incrementing. I.e., x needs a starting value above y. y should be the low
or ending value, e.g., 0 or 1.

For unsigned values, x=y will only loop once, when x=y, but only if x=y
initially for the first (and only) loop. That's the only time the
comparison is true. So, I don't think we should consider x=y either.

With one exception, for unsigned values, x<>y is effectively the same as x<y
assuming x is unsigned, incremented, starts with 0 or 1. I.e., x can't be
greater than y - except for x=1, y=0 which will loop forever.

For unsigned values,

x<y, x starts at 0, test before, loop executes y times or none
x<y, x starts at 1, test before, loop executes y-1 times or none
x<y, x starts at 0, test after, loop executes y times or once - whichever is
greater
x<y, x starts at 1, test after, loop executes y-1 times or once - whichever
is greater

x>y, if x starts above y, x is decremented, should be similar...

> incrementing ... before the test or after the test.....

"inc... before the test", to me, indicates the test is at the start of the
loop
"inc... after the test", to me, indicates the test is at the end of the loop

The difference is that one tests upon entry to the loop, whereas the other
skips this initial test. I.e., a test at the end of the loop, allows the
loop to execute at least once. Because of the loop, the non-initial loop
tests occur in the same effective sequence.

> In otherwords, I"m trying to figure out how to determine the number of
times
> that a loop would occcur based on the code.... I hope I"m making myself
> clear......
>

You need to work it through. Make sure you know where the test is to
determine if the loop executes once automatically. Check the condition.
Check what happens to some values with that condition. Check what happens
at the boundaries, i.e., start values and ending values, with that
condition, so you don't end up with off-by-one errors.


Rod Pemberton


From: wolfgang kern on

"don" asked:

> What are the rules for determining the number of times that a loop will
> occur given x and say you want
> to test for x < y or x > y or x = y or x <> y
> Where x would either start with 0 or 1 and you are going to be
> incrementing x by 1 inside the loop either before the test or after the
> test.....

> In otherwords, I"m trying to figure out how to determine the number of
> times that a loop would occcur based on the code.... I hope I"m making
> myself clear......

Loops can terminate on several conditions and may use any register
or even variables in memory for the loop-count.

ie:(ecx=iterations);
MOV ecx,20h
L0:
code.. ;performs 32 times
LOOP L0 ;does: dec ecx |jnz L0 ;but w/o flags altered

but ie:
MOV ecx,20h
L0:
code.. ;runs min.1 to max.32 times
LOOPZ L0 ;loop terminates if 'code' produces 'NZ'


ie:(bl=iteration-1);imply zero
MOV ebx,400005h
L0:
code.. ;ie: MOV byte[ebx],20h ;implies [400000h]
DEC bl
JNS L0 ;BL=FFh after this

ie:(ranged loops);
MOV dx,1000 ;
L0:
code.. ;
INC dx
CMP dx,1400 ;
JC L0 ;400 iterations
;JS L0 ;400
;JNZ L0 ;400
;JBE L0 ;401

An often seen confusion on x86 CPUs is that we got two variants
for a zero in the counter register:

LOOP with a zero in (e|r)cx will run 2**(16|32|64) times
REP with a zero in (e|r)cx skips the string-instruction

but both iterate (e|r)cx-times if nonzero.
__
wolfgang