Can somebody tell how to implement the "FOR" assember statement, cos i really dont get it.

Thanks
Posted on 2002-06-11 14:50:04 by IrOn
I'm a newbie so many other ppl may show you a better way but I have came up with this


some_type_of_label:

invoke MessageBox, NULL, offset message_str, offset title_str, MB_OK
dec i
cmp i, 0
jnz some_type_of_label

I've also attached the file so you could see the whole thing, hopefully the attach thingie works :)
Posted on 2002-06-11 16:55:31 by gorshing
The masm32 library actually enables you to use psuedo hll syntax like "if/elseif/endif" statements. Check out the masm32.hlp (or whatever it's called) in the "help" folder in the "masm32" dir. Maybe there's even a menuitem for it under the "help" menu in qeditor as well. I don't remember.

But basically it goes like this:


.if eax == 0
;then do whatever it is that you want to do
.elseif eax != 0
;then whatever you want here
.endif


Of course the same thing can be done with straight asm syntax like:


cmp eax,0
je eaxiszero
jne eaxisnotzero
jmp _end

eaxiszero:
;do whatever you want here
jmp _end

eaxisnotzero:
;do whatever you want here
jmp _end

_end:
ret ;or whatever



Hope that clears it up.


edit: Oops I forgot to say that there's no "for loop" in asm. But there is also hll style syntax for "while" and "repeat until" style loops. And you can always just create your own little loop by just having a counter with a comparison statement with a conditional jump.
Posted on 2002-06-11 17:56:50 by Will
What kind of FOR are we talking about?

=============================

There's the C/C++ FOR:

for (expr1; expr2; expr3) statement

which translates to

expr1;
while (expr2) { statement; expr3; }

=============================

Then there's the Basic/Pascal/Fortran FOR

FOR v = first TO last
statements
END FOR

which translates to

v = first;
while (v <= last) { statements; v = v + 1; }
Posted on 2002-06-11 18:56:45 by tenkey



some_type_of_label:

invoke MessageBox, NULL, offset message_str, offset title_str, MB_OK
dec i
cmp i, 0
jnz some_type_of_label



just one or two comments:

you can use anonymous labels (which i just found the joy of - they're not mentioned in my old books :) )

and when you decrement something, it will trigger the zero flag if the decremented value becomes zero. you might also want to decrement a register instead of some memory location, if its a small loop and as the situation dictates.

you end up /w something like:


@@:
invoke MessageBox, NULL, offset message_str, offset title_str, MB_OK
dec ebx
jnz @B
Posted on 2002-06-11 21:42:14 by jademtech

Can somebody tell how to implement the "FOR" assember statement, cos i really dont get it.

Thanks


There is two types of FOR type loops in MASM32. The first is what everyone has been getting at and is most typical in the C/C++ style, but it is not directly translated as 'for' in ASM. Instead you get some form of CONDITIONAL WHILE LOOP, which in essence is the same as a C++ for loop.

The second, which i think you asking about, is somewhat different and easy to missuderstand. In MASM32 there is a PRE-PROCESSOR 'FOR' statement, which is simular, but not the same. Its purpose is to direct the assembler in how to assemble code. Its rarely used, but can be an asset from time to time.

One (lame) example i can think of is telling the assembler you want to delete a bunch of GDI objects in global memory: this would involve alot of typing "invoke DeleteObject, xxx". You can do this in a FOR loop and have the compiler unroll the loop, by placing the lines of code during pre-comipile:


FOR arg, <hRedBrush, hFont1, hFont2, hBitmap1, hBitMap2, hPenGreen, hGrayBrush>
invoke DeleteObject, &arg
ENDM


When told to assemble, MASM will then generate the following, before converting to binary:


invoke DeleteObject, hRedBrush
invoke DeleteObject, hFont1
invoke DeleteObject, hFont2
invoke DeleteObject, hBitmap1
invoke DeleteObject, hBitMap2
invoke DeleteObject, hPenGreen
invoke DeleteObject, hGrayBrush


Thing to remember is this FOR loop is not static, and will unroll into code as shown..

You have to have a < > text style argument list for this directive, but you can make a numeric, pre-processor for loop using the preprocessor WHILE statement. A good example is stated solving
NOP-erator's Problem here.

Hope this helps answer you question..
:alright:
NaN
Posted on 2002-06-11 23:02:02 by NaN