flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Question on building macros dynamically

Author
Thread Post new topic Reply to topic
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 17:10
Hello.

Is there anybody to help me with macro constructions?
I'm trying to write a macro which will be used to create other macros from the list of supplied arguments. But during one of development phase I got stuck with a problem, which I'm still not capable to resolve.

Here is short excerpt of the working version:
Code:
macro MacroBuilder varName, macroName, action {
  macro varName#.#macroName \{
          action
      \}
}

macro DisplayOk {
    display 'Ok', 13, 10
}

MacroBuilder testVar, DisplayOk, DisplayOk

testVar.DisplayOk
    


Then I decided to replace one of the arguments with global constant:
Code:
macro MacroBuilder varName, macroName {
      macro varName#.#macroName \{
          Action
      \}
}

macro DisplayOk {
    display 'Ok', 13, 10
}

Action equ DisplayOk

MacroBuilder testVar, DisplayOk

testVar.DisplayOk
    

and right away I got first problem in the form of error message which said that Action is illegal instruction. After studying preprocessor dumps I realized that macro receives an argument by reference, not by value, so I added a match directive to do argument dereferencing which improved the situation (I use here HLL terms because I don't know more appropriate ones). So I got this code, which also worked:
Code:
macro MacroBuilder varName, macroName {
  macro varName#.#macroName \{
          match act, Action \\{
                        act
         \\}
  \}
}

macro DisplayOk {
    display 'Ok', 13, 10
}

Action equ DisplayOk

MacroBuilder testVar, DisplayOk

testVar.DisplayOk
    

But then I decided to remove macroName argument as well and replace it with global constant the same way as with action argument. Considering the previous experience with referenced argument the resulting code should look like this:
Code:
macro MacroBuilder varName {
      match name, MacroName \{
              macro varName#.#name \\{
                     match act, Action \\\{
                              act
                 \\\}
                \\}
  \}
}

macro DisplayOk {
    display 'Ok', 13, 10
}

Action          equ DisplayOk
MacroName      equ DisplayOk

MacroBuilder testVar

testVar.DisplayOk
    

but it doesn't work. I have played a little bit with fix directive, and it helped but only when I had it before macro declaration, which doesn't suit my purposes at all.
Is there another way to make it work as intended?

Thank you.
Post 13 Mar 2010, 17:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 13 Mar 2010, 17:15
You need to escape the hashes (#).
Code:
macro varName\#.\#name    
Post 13 Mar 2010, 17:15
View user's profile Send private message Visit poster's website Reply with quote
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 17:21
So hashes are also bound to current macro context. Good to know. Thank you revolution for such a quick answer. Smile
Post 13 Mar 2010, 17:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 13 Mar 2010, 17:24
You have to tell the preprocessor at what depth you need the hashes processed. You do this by prefixing the appropriate number of backslashes.
Post 13 Mar 2010, 17:24
View user's profile Send private message Visit poster's website Reply with quote
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 17:31
Yes, I understand. That is what I call "sensitive (or bound) to current macro context". I just didn't know that hashes are "sensitive" too. Thank you again. Smile
Post 13 Mar 2010, 17:31
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.