flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > recursion in macros

Author
Thread Post new topic Reply to topic
gandalf



Joined: 27 Feb 2009
Posts: 31
gandalf
Is it possible that I can't refer to the macro X inside of the macro X?
Post 01 Mar 2009, 18:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
Yes, it's true that you cannot access macro from within itself (read about it here, for example).

However it is possible to do macro recursion with a constructor-macro technique, but to show you how to do it, I would have to know more about what you're trying to achieve.
Post 01 Mar 2009, 18:42
View user's profile Send private message Visit poster's website Reply with quote
gandalf



Joined: 27 Feb 2009
Posts: 31
gandalf
Recursion is so important in rewriting systems that I think it should be made as easy as possible to use.
I suppose indirect recursion is ruled out as well.

My code is this:
Code:
macro match2 [args]
{
  common
      local done
  define done 0
       match , args
        \{
            define done 1
       \}
    match =| tail , args
        \{
            define toGo tail
            define done 1
       \}
    match =0 head tail , done args
      \{
            token equ token head
                match2 tail
 \}
}

token equ 
match2 eax,3|ebx|ecx
mov             token
    


What I was trying to do should be clear. This is just a small test. My final goal is to extend "match" and add support for regular expressions.
Post 01 Mar 2009, 20:53
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
Have a look at the MACRO/IF.INC from the standard set of Windows includes to see how such recursion is realised with fasm's preprocessor.

As for regular expressions, this is very unlike to be realizable with fasm's preprocessor because of the text tokenization it uses. I would suggest using some separate alternative preprocessor to get such features.
Post 01 Mar 2009, 20:58
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
This is how to apply that aforementioned constructor-macro trick into your sources to make the recursion work:
Code:
macro def_match2
{
  macro match2 [args]
  \{
        \common
        \local done
        define done 0 
        match , args 
        \\{
                define done 1 
        \\}
        match =| tail , args 
        \\{
                define toGo tail 
                define done 1 
        \\}
        match =0 head tail , done args 
        \\{
                token equ token head
                def_match2
                match2 tail 
        \\}
  \}
}
def_match2

token equ  
match2 eax,3|ebx|ecx 
mov             token    
Post 01 Mar 2009, 21:03
View user's profile Send private message Visit poster's website Reply with quote
gandalf



Joined: 27 Feb 2009
Posts: 31
gandalf
Could you explain why it works?
The modification doesn't seem significant.

Is there a way to see the result of macro expansions?
Post 02 Mar 2009, 18:50
View user's profile Send private message Reply with quote
gandalf



Joined: 27 Feb 2009
Posts: 31
gandalf
gandalf wrote:
Could you explain why it works?
The modification doesn't seem significant.


Sorry! I missed a very important thing Smile
Post 02 Mar 2009, 18:54
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
gandalf wrote:
Could you explain why it works?
The modification doesn't seem significant.

The "def_match2" macro defines the "match2" macro so that it can be called then. It's called once to define "match2" globally, and then it's called from inside the "match2" macro to make it possible to call the same "match2" from there.
However I forgot to purge the macro definition in the example above. To keep the things clean, the recursive entry part should look like:
Code:
                def_match2 
                match2 tail 
                purge match2    


gandalf wrote:
Is there a way to see the result of macro expansions?

Use "-s" switch with command line fasm to generate the symbolic info file, and the process it with "prepsrc" tool (from the TOOLS directory) to get the preprocessed source.
Post 02 Mar 2009, 18:55
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
Use "-s" switch with command line fasm to generate the symbolic info file, and the process it with "prepsrc" tool (from the TOOLS directory) to get the preprocessed source.

or, use the "display" directive to see result
Post 02 Mar 2009, 23:54
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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.