flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > (simple macros) why doesn't this work?

Author
Thread Post new topic Reply to topic
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
I've just started playing with macros a little, but am confused by the following:

In one macro, I do this:
Code:
        if ~ arg eq
          common
          local size 
          size = 0
          reverse
            pushd arg
            size = size + 4
        end if
    

and that compiles and works.

Now, in another macro, I do this:
Code:
        if ~ arg eq
          common
          local ofs
          ofs = 8
          forward
            label arg at ebp+ofs
            ofs = ofs + 4
        end if
    

so very similar, but here fasm gives me:

Code:
fasmacros.inc [86] Proc [14]:
 end if
error: unexpected instruction.
    


now if I comment out both the "if" and the "end if" lines, it compiles, but that's not what I want.

I'm surprised the second code doesn't compile when the first one does. What am I missing?
Post 06 Apr 2009, 21:15
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
Doesn't the IF and END IF need to be in common blocks?
(They are evaluated at assembly stage, iirc.)
Post 06 Apr 2009, 21:19
View user's profile Send private message Visit poster's website Reply with quote
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
Thanks bitRAKE, that was it: turns out there was a common somewhere above the "if"-line. So apparently common/forward/reverse create some kind of context, and if your "if" is in the common context, your "end if" must also be in a common context, I hadn't gotten that from reading the manual.
Post 06 Apr 2009, 21:36
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
This is what's happen with your macro
Code:
macro aMacro arg, [arg] {
        if ~ arg eq
          common
          local ofs
          ofs = 8
          forward
            label arg at ebp+ofs
            ofs = ofs + 4
        end if
}

aMacro one, two, three
; Assembler receives the following:

        if ~ one eq
          ofs?1 = 8
            label two at ebp+ofs?1
            ofs?1 = ofs?1 + 4
        end if
            label three at ebp+ofs?1
            ofs?1 = ofs?1 + 4
        end if
    


Note: "ofs?1" is a placeholder for the local symbol generated by the preprocessor, I don't remember now how the names are generated but is something like that.
Post 06 Apr 2009, 21:47
View user's profile Send private message Reply with quote
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
Is that the actual preprocessor-output? And if so, how did you get fasm to output that? But it looks kinda strange to me with the two "end if"s, I'm not sure how to interpret it. Was my explanation in the reply to bitRAKE correct, or not?

And just to be sure: my intention with the "if" was to check if [arg] isn't empty, ie at least one argument has been passed to the macro. So the "if" will only be executed once, right?
Post 06 Apr 2009, 22:02
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
FORWARD/REVERSE copy the text block a number of times based on parameters.
COMMON copy the text block once.

IF requires a matching END IF, so this limits usage.

There is no context - MACRO is just text paste. Very Happy
buzzkill wrote:
And just to be sure: my intention with the "if" was to check if [arg] isn't empty, ie at least one argument has been passed to the macro. So the "if" will only be executed once, right?
Yeah, you want them both in a COMMON block.
Post 06 Apr 2009, 22:22
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: 4633
Location: Argentina
LocoDelAssembly
Well, this is the output using the old FASMPRE tool:
Code:
;macro aMacro[arg]
;{
; common
; if~arg eq
; common
; local ofs
; ofs=8
; forward
; label arg at ebp+ofs
; ofs=ofs+4
; end if
;}

;aMacro one,two,three,four
        
        
        
        
        
        
        
        
        
        if~one,two,three,four eq
        
        
        ; ofs?0000000
        ofs?0000000=8
        
        
        label one at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label two at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label three at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label four at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
                   


Now the interesting part is that if I remove the first "common" then this happens:
Code:
;macro aMacro[arg]
;{
; if~arg eq
; common
; local ofs
; ofs=8
; forward
; label arg at ebp+ofs
; ofs=ofs+4
; end if
;}

;aMacro one,two,three,four
        
        if~one eq
        
        
        if~two eq
        
        
        if~three eq
        
        
        if~four eq
        
        
        ; ofs?0000000
        ofs?0000000=8
        
        
        label one at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label two at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label three at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if
        
        
        label four at ebp+ofs?0000000
        ofs?0000000=ofs?0000000+4
        end if         


Looks like the first "anonymous" block was a forward block.

Note that you can also use the prepsrc debug tool also, but the problem with it is that you need a compilable source first while FASMPRE (unfortunately outdated) needs only a source that the preprocessor can handle with no syntax errors.


Last edited by LocoDelAssembly on 06 Apr 2009, 22:40; edited 1 time in total
Post 06 Apr 2009, 22:33
View user's profile Send private message Reply with quote
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
Thanks for the explanation, guys. LocoDelAssembly: from what I read on the forum, fasmpre was only released for windows, so I can't use it. Maybe there will come an updated fasmpre for all platforms, because, as you say, prepsrc does you no good while you're still struggling to get your stuff compiled Smile
Post 06 Apr 2009, 22:39
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Oh didn't remember that. I now wonder if would be possible for fasm to still generate the fas file covering only up to the stage fasm completed successfully. Would that be possible or that would make the fas file unparseable?

BTW, I have edited my previous post because I was referencing the wrong parameter, now I changed to a macro with a single "vararg".
Post 06 Apr 2009, 22:46
View user's profile Send private message Reply with quote
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
LocoDelAssembly wrote:
I now wonder if would be possible for fasm to still generate the fas file covering only up to the stage fasm completed successfully. Would that be possible or that would make the fas file unparseable?


I don't think that would generate a valid fas file.
But maybe there is a need for one tool to help with macros/preprocessing, seeing as I myself have in the past brought up the possibility of a fasm switch to print preprocessor output (-E a la gcc), you (and probably others) use fasmpre which is not available for all platforms and apparently outdated, and then there's prepsrc which only works on compilable sources. I know when I started messing with nasm macros, I used the preprocessor output a lot when I ran into trouble.
Post 06 Apr 2009, 23:42
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
I considered to make fasm generate fas file even in case of error, as long as the preprocessor finished sucessfully - then fas would contain only the preprocessed source, and the other structures will be empty. I'm not sure if I'll get this for 1.68, though.
Post 07 Apr 2009, 05:14
View user's profile Send private message Visit poster's website Reply with quote
buzzkill



Joined: 15 Mar 2009
Posts: 111
Location: the nether lands
buzzkill
Tomasz Grysztar wrote:
I considered to make fasm generate fas file even in case of error, as long as the preprocessor finished sucessfully - then fas would contain only the preprocessed source, and the other structures will be empty. I'm not sure if I'll get this for 1.68, though.


No hurry Tomasz, it's not a priority of course, but I think it could be a valuable help for people who are learning fasm preprocessor (which is more elaborate then other assembler's I've used).
Post 07 Apr 2009, 22:59
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.

Powered by rwasa.