flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to define a table of function pointers using macros?

Author
Thread Post new topic Reply to topic
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
I am interested in using macros to help automate the creating of a table of function pointers.

The usage would be like this:

Code:
function 'function1'
   ... some asm code ...
   ret

function 'function2'
    .... some asm code
    ret
    


... somewhere later, declare the function table
function_table

The 'function' macros might be over several source files; what I want is that they allow me to use 'function_table' later, which will create an array with the function names and offsets.

Is something like this possible with FASM macro language?
Post 26 Dec 2004, 06:18
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Of course it is possible, with only limitation that the pointer table must stay after the last function.
The main idea is to have one globa symbolic costant and on every "functions" to add the function name to the constant:
Code:
   FunctionList equ FunctionList, functionName
    


At the end you will have FunctionList containing all functions names separated with commas. Then you have to define second macro "BuildFunctionTable" and to pass to it this list:
Code:
macro BuildFunctionsTable [func] {
forward
  dd func
}
    


This is very simplified description. You can look at "globals.inc" macroses from the project Fresh - they use similar technique to make global data definitions.

Regards.
Post 26 Dec 2004, 08:12
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
Hi, JohnFound -

Since you posted that response, I had gone to using Perl to extract the information the way I want. But now I am revisiting the problem, and I am getting more questions than answers Sad

The attached file 'test.asm' illustrates what I am trying to do, and the problem I am having.

Basically, I have a macro "addtolist [a,b]" which I call like "addtolist 9,'nine'". It is supposed to append the values to some list (an EQU).

Then I try to process that list using the 'table' macro, which is declared as "table [xt,nm]" but things do not work as I expect (at all). For one thing, the types of the values passed in seem wrong to me.

I would appreciate any help you can provide me.

Thanks!


Description:
Download
Filename: test.asm
Filesize: 941 Bytes
Downloaded: 139 Time(s)

Post 27 Mar 2005, 23:00
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
Arggh!

My table macro was like this:

Code:
macro mlist nam,xt { macrolist fix macrolist,xt,nam }
    


And with 1.62, this doesn't work any more Sad

What is intended is that whenever 'mlist' is invoked, it adds 'name,xt' to the 'macrolist'. So that when 'macrolist' is passed to a table builder macro, it can create the proper table from it.

What is the right thing to do with the new 'match', or can I still use 'fix' just in some different way?
Post 15 Jun 2005, 16:25
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Read the updated manual, especially the section 2.3.7 which contains exactly the information you need.
Post 15 Jun 2005, 19:23
View user's profile Send private message Visit poster's website Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
Indeed, it works! Thanks.
Post 15 Jun 2005, 19:47
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
Now I would like to add some more information to the list. Basically, I want to know how big the function is. Using something like this:

function 'name', _internal_name
mov ebx, eax
...etc ....
endfunction

What I want is that 'endfunction' should figure out how big the function was (in bytes) and append that information to the function list. But sadly, I'm encountering resistance Sad

Any ideas?
Post 10 Sep 2005, 01:12
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
Code:
macro endp name {
 endp
 sizeof.#name = $-name
}

; usage: instead of lone endp add function name after it
proc    function1
        ; normal code here
endp    function1

        mov     eax, sizeof.function1
    
Post 11 Sep 2005, 13:26
View user's profile Send private message Visit poster's website Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
ronware
Thanks, I ended up doing something like that. I was hoping for something more elegant - where the current word was saved in the 'proc' macro, and reused in the 'end' macro. But I had trouble getting that part to work correctly.
Post 15 Sep 2005, 21:42
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
blueowl2



Joined: 30 Oct 2005
Posts: 4
blueowl2
JohnFound wrote:
Of course it is possible, with only limitation that the pointer table must stay after the last function.
The main idea is to have one globa symbolic costant and on every "functions" to add the function name to the constant:
Code:
   FunctionList equ FunctionList, functionName
    


At the end you will have FunctionList containing all functions names separated with commas. Then you have to define second macro "BuildFunctionTable" and to pass to it this list:
Code:
macro BuildFunctionsTable [func] {
forward
  dd func
}
    


This is very simplified description. You can look at "globals.inc" macroses from the project Fresh - they use similar technique to make global data definitions.

Regards.


Should that code work? In my code there is a problem with that, namely that only one entry of the list will be written:

Code:
macro addtolabellist
{ local _label
  if defined labellist
  labellist equ labellist,_label
  else
  labellist equ _label
  end if
  _label: }

macro writelabellist [label]
{ forward
  dd label }

nop
addtolabellist
nop
addtolabellist
nop
addtolabellist

writelabellist labellist    


Any idea's? :)
Post 30 Oct 2005, 14:45
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Read section 2.3.7 of manual.
Post 30 Oct 2005, 15:03
View user's profile Send private message Visit poster's website Reply with quote
blueowl2



Joined: 30 Oct 2005
Posts: 4
blueowl2
[quote="Tomasz Grysztar"]Read section [url=http://flatassembler.net/docs.php?article=manual#2.3.7]2.3.7[/url] of manual.[/quote]

Yes, thankyou, I already read it in your previous post. I tried it with:

[code]macro addtolabellist
{ local _label
if defined labellist
labellist equ labellist,_label
else
labellist equ _label
end if
_label: }

nop
addtolabellist
nop
addtolabellist
nop
addtolabellist

macro writelabels { match aparam, labellist \{ writelabel aparam \} }
macro writelabel [tparam] { forward dd tparam }
writelabels[/code]

But it does not seem to replacing [i]aparam[/i] with the actual value of labellist.
Post 30 Oct 2005, 17:50
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Go back to reading it and note what it says about mixing preprocessor's and assembler's features.

Hint: use "match", not "if".
Post 30 Oct 2005, 21:55
View user's profile Send private message Visit poster's website Reply with quote
blueowl2



Joined: 30 Oct 2005
Posts: 4
blueowl2
Something like:

[code]V fix {
macro addtolabellist
\{ local _label
_label:
match any, labellist \\\{ labellist equ labellist,_label \\\}
match , labellist \\\{ labellist equ _label \\\} \}
}
V

nop
addtolabellist
nop
addtolabellist
nop
addtolabellist

macro writelabels { match aparam, labellist \{ writelabel aparam \} }
macro writelabel [tparam] { forward dd tparam }
writelabels[/code]

:)? Sorry, I guess I need more then a hint.
Post 02 Nov 2005, 19:02
View user's profile Send private message Reply with quote
blueowl2



Joined: 30 Oct 2005
Posts: 4
blueowl2
After a lot of trying, i got the following macro working. The question is if the "definelabel" macro is necessary, or could be integrated into addtolabellist. Thanks in any case. :)

[code]macro definelabel name { name#.list equ }

macro addtolabellist name
{ item = $
match any, name#.list \{ name#.list equ name#.list,item \}
match , name#.list \{ name#.list equ item \}
}

definelabel some
nop
addtolabellist some
nop
addtolabellist some
nop
addtolabellist some

macro writelabels { match aparam, some.list \{ writelabel aparam \} }
macro writelabel [tparam] { forward dd tparam }
writelabels[/code]
Post 03 Nov 2005, 14:21
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. Also on YouTube, Twitter.

Website powered by rwasa.