News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

[.while]

Started by ic2, October 14, 2006, 08:52:21 AM

Previous topic - Next topic

ic2

Sorry i miss spelled your name Vortex.  Shantanu Gadgil and i has nothing on that.  I know this could MEAN war :)

I just re-read everyone comments and comments i missed and i do owe Shantanu Gadgil an apology.  Shantanu Gadgil i apologies 85% as of now.

Ratch

ic2,

     The only reason I use the HLL constructs in MASM is because it makes the source look cleaner.  That's  because all those ''trash tags" needed for conditional jumps are missing.  Otherwise I can code a conditional relation almost as fast.  It helps tremendously when reading the source code to indent your conditonal relations and loops.  And, if you want to see what the output code for HLL is in the listing, without using the very verbose /Sa option, use the /Sg option along with /Sn.  Ratch

PBrennick

ic2,

.while arguing
    ; It is good to see friends stay friends and issues get resolved.
    mov    arguing, 0
.endw

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Shantanu Gadgil

To ic2,
No problems!  :U :U

As Paul said, I hope after all this talk the programming issue actually got resolved!!!  :bg

Regards,
Shantanu
To ret is human, to jmp divine!

donkey

A last comment to add to the talk here then I will leave this topic alone. Many of the posts I have read here (for example Vortex's macro) perform the compare at the end of the loop, this is incorrect. If the test is being performed at the end of the loop then it is a REPEAT loop, a WHILE loop tests at the top of the loop. Not understanding this or misinterpretting the function of different loop types will only lead to confusion (and bugs) later. This is by the way a side effect of using high level constructs for simple loops, the level of abstraction can lead to not understanding loops and their appropriate applications.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

Edgar,

The compare at the end of the loop is correct because the macro simulates the do & while construct in the C language. My intend was to create a quick solution, not to copy the functionnality of MASM's WHILE statement.

#include <stdio.h>

void main(int argc,char *argv[])
{
  int var=0;
  do{
    ++var;
  }while(var<45);
  printf("%d",var);
}

TNick


drizz

Quote from: donkey on October 15, 2006, 07:35:04 AM
Many of the posts I have read here (for example Vortex's macro) perform the compare at the end of the loop, this is incorrect. If the test is being performed at the end of the loop then it is a REPEAT loop, a WHILE loop tests at the top of the loop.

some [.while] code
.while eax<somenumber
;dosomething
inc eax
.endw


OPTIMIZED while with a compare at start and no useless jmp:
cmp eax,somenuber
jae @@endw
@@while
inc eax
cmp eax,somenuber
jb @@while
@@endw:


HLL combo that does absolutely the same:
.if eax<somenuber
.repeat
inc eax
.until eax >= somenuber
.endif


the reply was not neccessarily aimed at you donkey, but at all.


the biggest problem i think is people dont really know or are unsure what code HLL constructs
genereate therefore i agree to what asmrixstar said about putting INT 3 before and enabling Ollydbg
as JIT.
The truth cannot be learned ... it can only be recognized.

donkey

Hi Vortex,

The DO/WHILE loop is a REPEAT loop not a WHILE loop, it is a misnomer, the usage and rules regarding WHILE and REPEAT have been well documented for quite a long time and are consistent in every programming language. The post that started this thread was asking for a MASM .WHILE loop which is a proper WHILE, providing an example of a REPEAT loop does not answer his question.

http://research.mupad.de/doc/31/eng/stdlib_repwi.html
http://www.elated.com/tutorials/programming/asp/loops/
http://www.cprogramming.com/tutorial/lesson3.html

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

Edgar,

Using a WHILE or REPEAT loop does not matter here. MASM provides you a powerfull macro engine to code your own HLL constructs if you are not satisfied with the default WHILE statement.

donkey

Quote from: Vortex on October 15, 2006, 07:50:53 PM
Edgar,

Using a WHILE or REPEAT loop does not matter here. MASM provides you a powerfull macro engine to code your own HLL constructs if you are not satisfied with the default WHILE statement.

Hi Vortex,

Yes, it does matter here, he was looking to replace a MASM WHILE loop that scanned forward through a PE header, if the loop was allowed to execute on invalid data it could cause his application to crash depending on the values it encounters. A while loop will not execute under those circumstances, a REPEAT loop will, it is a very important distinction that should be understood by newer programmers, fudlling up the works by showing C's badly named termination to a DO/UNTIL loop is counter-productive.

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Ratch

To the Ineffable All,

     Let me describe the .REPEAT and .WHILE as I understand them.  A .REPEAT always allows one trip through the code before it checks the codition.  If the end conditions exists before the .REPEAT is executed, too bad, one free trip through the loop.

    Now MASM implements a .WHILE  by putting the condition check at the bottom of the loop.  BUT, it first jumps down to do the condition check. Therefore the loop will not be executed even once if the exit condition exists.  It would appear that it is more efficient to put the check at the top of the loop so it can fall through, until you realize that when the exit condition exists, a jump out is necessary.  So a the MASM way of putting the condition check at the bottom of the loop requires a jump at the start of execution, and the other way of putting the check at the top of the loop requires a jump out at the end.  Since I am a MASM freak, I don't know anything about DO-WHILE or DO-REPEAT. Ratch

PBrennick

Donkey,
Thank you for your help. Personally, in my mind, it is always correct to do the condition testing first and then branch accordingly. I will always do it that way as it seems logical to me. I very seldom use while loops and I never use repeat loops.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

ic2

#28
This was the post that hutch say no - no to.
So it's out of here.

Shantanu Gadgil

#29
Some clarifications about the various loop constructs:
repeat-until is present in Pascal, but there is no such thing in C (C has the do-while thingy)

There are very clear differences between the do-while, while and the repeat/until loop constructs.

The while is what can be called as a top-check loop, allows you to decide whether to enter the loop at all or not.
The do-while and repeat-until are bottom check loops, allowing you to execute the loop atleast once.

There is also a very obvious difference between the do-while and the repeat-until loops; one is a positive check and the other is a negative check.

do-while:

do stuff (keep looping)
WHILE condition is TRUE (meaning: if condition is TRUE, loop, else EXIT loop)


repeat-until:

do stuff (keep looping)
UNTIL condition is TRUE (meaning: if condition is TRUE, EXIT loop, else loop


The word UNTIL can be a bit confusing!!!  :bg :bg

HTH,
Regards,
Shantanu
To ret is human, to jmp divine!