Hi,
I am curious to know if anyone can tell me which way of looping is more efficient?


Like this,


.while (eax < 5)
inc eax
.endw


or like this,


again:
mov ecx,5
loop again

?????

Is one way better than the other? Is one faster than the other?
Thank You,
Barry
Posted on 2002-12-09 05:59:46 by bgong68
If the value of eax is independant, and you just want to itterate a loop n times, it is better on the whole to approach zero. The zero flag will be set by the decrement (or increment if you decide to go from -5 to 0), and thus move the need for a cmp.

On the whole a loop is bad, its an instruction which has been left behind in terms of silicon optimisation. Its just there for comleteness (ie we can still run 286 code on a Pentium 4), and isn't generally the fastest option.

Mirno
Posted on 2002-12-09 06:31:54 by Mirno
; Loop #1

.while (eax < 5)
...
inc eax
.endw
The following loops forever:
; Loop #2

again:
mov ecx,5
loop again
Version 3 is slower than version 4 on some Intel processors:
; Loop #3

mov ecx,5
again:
...
loop again
; Loop #4

mov ecx,5
again:
...
dec ecx
jnz again
Posted on 2002-12-09 15:25:53 by tenkey
Hi Mirno,
Thanks for taking a look, and explaining.
Barry
Posted on 2002-12-10 05:42:48 by bgong68
Hi Tenkey,
I forgot to add loop 4 to my question. I am happy you brought it up. In your opinion which loop would you prefer, loop 1 or loop 4?
Barry
Posted on 2002-12-10 05:45:31 by bgong68
Barry,

Personnally, loop #4...
I used it a lot to do reverse buffer filling...
It is fine as you do not need to cmp at each iteration as dec/sub modify flags...

If your loop is time critical, you can try to unroll it... but the results aren't always much better, especially when the loop is already well optimized...
Posted on 2002-12-10 05:58:13 by JCP
Hi Readiosys,
I am always curious if there will be a big difference in speed when using the .if, .while, and other directives instead of cmp with jmp?
Thanks,
Barry
Posted on 2002-12-11 05:47:13 by bgong68
Barry,

MASM .if .while, etc directives aren't very well optimized...

It will not do the best job you could perform if you coded the loop manually...

Personally, I never use these as if I'm writing in assembly, it is to have 100% control over my source code...
Otherwise, it is better to use a HLL langage like C with high optimizing compilers.
If you find these directives convenient, you can always use them where speed doesn't matter (ie, messages loops), but personally, I have no readability problems with the "real code" (especially in message loops, it is then very similar to a message cracker).
It is sometimes more convient if you only need to execute a bit of code on a small condition...

example:



.if eax == 5
mov eax, 1
.endif

@@:


If you code it manually, you have to do something like.




cmp eax, 5
jne @F
mov eax, 1

@@:


you can even use a cmov (doesn't work on all x86 CPUs)

It is a bit clearer, especially if you have several lines of code on this condition, but as you can see it is not so difficult to not use the MASM directives... it just gets a bit more confusing if you have if/else conditions...

Imho, MASM directives are fine to learn the langage, because you do not need to care with conditions and jump, but conditionnal jumps are an important part of the langage and shouldn't imho be hidden by the assembler features...
Indent your code to make the program logic clearer and real code usage won't look so bad and so hard.
Posted on 2002-12-11 07:02:39 by JCP
Hi Readiosys,
Thanks for giving some guidance. I must admit though, I like using the directives, but I will make an effort to learn to use cmp and jmp.
Barry
Posted on 2002-12-11 21:01:38 by bgong68