flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > problem with circularily referencing macros

Author
Thread Post new topic Reply to topic
MCD



Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 27 Oct 2007, 19:41
can someone explain me why this works:
Code:
macro    _push   arg {
  display "_push",0xA
       push    arg
}
macro  tmp     arg {
  display "tmp",0xA
 _push   arg
}
macro  push    arg {
display   "push",0xA
        _push   arg
}

;tmp       eax
push     eax
    

but this doesn't
Code:
macro        _push   arg {
  display "_push",0xA
       push    arg
}
macro  tmp     arg {
  display "tmp",0xA
 _push   arg
}
macro  push    arg {
display   "push",0xA
        _push   arg
}

tmp        eax
push     eax
    

I know that fasm does its best to try resolving macros that reference each other, but for some reason it doesn't manage to resolve the later one. The only thing that made the above example work is the following solution:
Code:
macro    _push   arg {
  display "_push",0xA
       push    arg
}
macro  tmp     arg {
  display "tmp",0xA
 _push   arg
}
macro  push_   arg {
  display "push_",0xA
       _push   arg
}
push   fix     push_

tmp        eax
push     eax
    

_________________
MCD - the inevitable return of the Mad Computer Doggy

-||__/
.|+-~
.|| ||


Last edited by MCD on 28 Oct 2007, 00:43; edited 1 time in total
Post 27 Oct 2007, 19:41
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 27 Oct 2007, 19:55
"tmp" works as supposed to. tmp uses "_push" macro, "_push" macro uses "push" macro, and it uses "_push". but since we already are in "_push" macro, FASM tries to assemble it as instruction
Post 27 Oct 2007, 19:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4022
Location: vpcmpistri
bitRAKE 28 Oct 2007, 00:25
FASM stops when it first notices the circular reference and if that isn't an appropriate end it errors. That is the only useful way to handle circular references, imho. The other way is to always error.

push
_push
push <--- ah, circular - try to stop

tmp
_push
push
_push <-- ah, circular - try to stop
Post 28 Oct 2007, 00:25
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 28 Oct 2007, 01:05
Quote:
FASM stops when it first notices the circular reference

no it doesn't. there is no circular reference in FASM, there is just macro overloading.

Code:
macro push x{
  display '1'
  push x
}
macro push x{
  display '2'
  push x
}
push 10    
Post 28 Oct 2007, 01:05
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4022
Location: vpcmpistri
bitRAKE 28 Oct 2007, 03:41
vid, How does FASM know when to stop?
Post 28 Oct 2007, 03:41
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 28 Oct 2007, 04:02
Works like a stack. Everytime you define a macro with the same name the last will be in the top of the stack. When you call the macro, the body gets expanded and before preprocessing it this imaginary stack pops out one definition. When the preprocessor doesn't finds any macro with the name because of either the macro does not exists at that point or the stack is empty, the preprocessor just passes the line to the assembler stage and this one will fail if the line is not an assembler instruction.

I forgot to mention, the imaginary stack is restored once the whole expansion process of your macro invocation is finished.
Post 28 Oct 2007, 04:02
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4022
Location: vpcmpistri
bitRAKE 28 Oct 2007, 04:34
That seems a clear definition of the overloading process, but we are talking about macros with different names. How can the same process apply to different named macros?
Post 28 Oct 2007, 04:34
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 28 Oct 2007, 12:37
same way as it applies to same-named macros. Play with this code:
Code:
macro a {
display 'a1'
}
macro b {
display 'b1'
a
}
macro a {
display 'a2'
b
}
macro b {
display 'b2'
a
}
b
    
Post 28 Oct 2007, 12:37
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4022
Location: vpcmpistri
bitRAKE 28 Oct 2007, 16:33
Why doesn't this work? Why should the B stack restore to include the new definition of B within the second A?
Code:
macro a {display 'a1'}
macro b {display 'b1'
  a}
macro a {display 'a2'
  b}
macro b {display 'b2'
  b
  a}

b    
...but this one works?
Code:
macro a {display 'a1'}
macro b {display 'b1'
  a}
macro a {display 'a2'
  a
  b}
macro b {display 'b2'
  a}

b    
Post 28 Oct 2007, 16:33
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 28 Oct 2007, 16:45
Code:
macro a {display 'a1'}  ;1
macro b {display 'b1'  ;2
  a} 
macro a {display 'a2'  ;3
  b} 
macro b {display 'b2'  ;4
  b 
  a} 

b  
    

okay, so we have "b". that uses topmost macro "b", that is number 4. Number 4 uses nested "b" again, that is macro number 2. It uses topmost macro "a", that is number 3. This calls "b" again, but there isn't any macro "b" (we are already in both 4 and 2, so these doesn't count)
Post 28 Oct 2007, 16:45
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.