flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro aliasing and forward references

Author
Thread Post new topic Reply to topic
nyrtzi



Joined: 08 Jul 2006
Posts: 192
Location: Off the scale in the third direction
nyrtzi 10 Oct 2009, 13:37
So one variant of the code looks like this...
(this is just a test... not real app code)

Code:
format elf

macro proc name
{
    local start
    macro name
    \{
        call start   
    \}
    macro endp 
    \{
        pop eax
        ret
        purge endp
    \}
    start:
    push eax
}

start:
    goo
    ret

proc goo
    nop
endp
    


When I try to assemble this FASM says: "error: illegal instruction." because "goo" isn't defined yet. So of course if I move the "proc goo ... endp" before "start:" this code will assemble without any problems.

So the question is that is there a way to make macro forward references like this work?

Of course I could always just do a "call goo" instead of plain "goo" but then again again aren't I using macros in the first place exactly because I want to avoid unnecessary typing?

The bigger problem and the actual main issue is that this doesn't work:

Code:
proc foo
    zoo
endp

proc zoo
    foo
endp
    


In this case re-ordering doesn't help at all.
Any ideas on how I can make this work i.e., to assemble properly?
Or should I just give up on the idea of being able to have macro aliases like these for procedure calls?
Post 10 Oct 2009, 13:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 10 Oct 2009, 14:04
You can't forward reference macros, rept, irp, struc, define. The preprocessor is single pass only.
Post 10 Oct 2009, 14:04
View user's profile Send private message Visit poster's website Reply with quote
nyrtzi



Joined: 08 Jul 2006
Posts: 192
Location: Off the scale in the third direction
nyrtzi 10 Oct 2009, 14:17
The preprocessor does only a single pass? Weird... oh well.

I guess I'll just have to use sed or some other tool to do some custom preprocessing and add that to my Makefile.

Thanks for the pointer.
Post 10 Oct 2009, 14:17
View user's profile Send private message Reply with quote
nyrtzi



Joined: 08 Jul 2006
Posts: 192
Location: Off the scale in the third direction
nyrtzi 10 Oct 2009, 18:11
Just for future reference in case someone is doing something similar...

The actual source code. Assumed to be in the src/ directory.

Code:
format elf

include '../obj/aliases.inc'

macro proc name
{
    macro endp 
    \{
        ; replace this line with cleanup code
        ret
        purge endp
    \}
    _#name:
    ; replace this line with initialization code
}

main:
    goo
    ret

proc goo
    ; do something
endp
    


Put this for example in a Makefile.

Code:
find src/ -iname '*.asm' | xargs awk '/^proc/ { printf("macro %s { call _%s }\n",$2,$2);  }' > obj/aliases.inc
    


This of course assumes that you have some kind of a shell environment available with 'find', 'xargs', 'awk' and 'make'. I'm doing this on a CentOS virtualbox...
Post 10 Oct 2009, 18:11
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.