flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > For loop [EXAMPLE]

Author
Thread Post new topic Reply to topic
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka
I wrote a For...EndFor macros based on this post.
I think it's better then old macro.
Code:
macro For param*
{
local endfor,for
macro end_for
\{
endfor:
\}
match variable==start To stop,param
\{
display "A"
var equ variable
mov [variable],start
for:
_for equ for
cmp [variable],stop
\}
ja endfor
}

macro EndFor
{
add [var],1
jmp _for
end_for
purge end_for
restore _for
restore for
restore end_for
restore var
}    
And here's example of usage:
Code:
include 'win32ax.inc'

.code

  start:
        For i=1 To 2
         invoke  MessageBox,HWND_DESKTOP,"i",invoke GetCommandLine,MB_OK
        For j=1 To 3
        invoke  MessageBox,HWND_DESKTOP,"j",invoke GetCommandLine,MB_OK
        EndFor
        EndFor
        invoke  ExitProcess,0
.end start
i dd ?
j dd ?    

#Edit


Last edited by Kazyaka on 01 Feb 2013, 16:18; edited 3 times in total
Post 01 Feb 2013, 15:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
Just quickly I notice that the EndFor macro addresses [i] directly. I assume you should be using [variable] instead.
Post 01 Feb 2013, 15:42
View user's profile Send private message Visit poster's website Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka
revolution wrote:
Just quickly I notice that the EndFor macro addresses [i] directly. I assume you should be using [variable] instead.
Oh, really. How could I not notice this? Thanks for fast reply.
Btw. shoudn't it be added to Macro solutions instead old for loop?
Post 01 Feb 2013, 16:08
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
Kazyaka: It's great to see that beginner macro writers like you are learning, but I see serious problems here.

As a general rule, one should never use automatic [] when referring to parameters in language runtime macros like .if/.while/.for, etc. This prevents a direct number or register from being sent (i becomes [i], r becomes [r] and [r] becomes [[r]] which is an error):

* It changes FASM's "ideal mode" syntax: address/[value]. Same in TASM and disassemblers
* Will all of your macros do this or just For? Not If or While? Note that For is slightly higher-level than If and While
* Inside For and everywhere else, you will be referring to the index as [i]
* This address/[value] inconsistency will cause endless bugs. Any C/C++ programmers reading? Imagine if you changed the address/*value notation in some cases but not others. It would cause Hell!
* FASM's .if/.while macros don't do this and for good reasons. By the way, you might want to test your macros first before you think they should replace the ones included with FASM Wink

Additional problems: No error checking? Assuming unsigned, "ja" should be "jae" for correct count. "add [var],1" should be "inc var". No reason to re/define "macro end_for" for every appearance of For. Try this instead:
Code:
macro For [p] {
common
local ..start, ..end
?START equ ..start
?END equ ..end
define ?s 0
match =0 i==x =To n, ?s p \{
 define ?s 1
 ?INDEX equ i
 mov i, x
 ?START:
 cmp i, n
 jae ?END
\}
if ?s eq 0
 'Syntax error' For
end if
}

macro EndFor {
inc ?INDEX
jmp ?START
?END:
restore ?START, ?END, ?INDEX
}    
Naming convention is unimportant - .for, Loop, FOR, etc - but I hope programmers can eventually come to an agreement on the language semantics.
Post 19 Mar 2013, 08:54
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.