flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Function definition [SOLVED]

Author
Thread Post new topic Reply to topic
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka 27 Jan 2013, 23:13
@Edit:

After a short discussion I put here three ways to write macro which can define a funcion. If you don't know what I mean then feel free to read below posts.

First
Second
Thrid


Last edited by Kazyaka on 29 Jan 2013, 21:58; edited 2 times in total
Post 27 Jan 2013, 23:13
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 27 Jan 2013, 23:22
Kazyaka,

Those proc/endp/stdcall macros will fit nicely: without function's arguments they behave exactly as you want. Use them instead of Func/EndFunc/PutFunc correspondingly.
Post 27 Jan 2013, 23:22
View user's profile Send private message Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka 28 Jan 2013, 10:43
baldr wrote:
Kazyaka,

Those proc/endp/stdcall macros will fit nicely: without function's arguments they behave exactly as you want. Use them instead of Func/EndFunc/PutFunc correspondingly.


Nice idea, but it does too many useless operations for my needs.

After a long time I made something like in FASM Documentation.
Here's my macro:
Code:
    macro Func name*
     {
       macro name {
            name:
     }

    EndFunc fix }    
Example of use:
Code:
Func MyFunc
inc ebx
  invoke  MessageBox,HWND_DESKTOP,"My Func!",invoke GetCommandLine,MB_OK
  ret
EndFunc    
I tested and it works correctly. What do you think about this?
Post 28 Jan 2013, 10:43
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 28 Jan 2013, 13:11
Kazyaka,

Your original post was too vague, now I understand: you want to make something like inline functionality. Then PutFunc name { name } (as you've used it in example) will expand macro created with Func/EndFunc at the place of invocation (simple MyFunc wil do that too: it's macroinstuction by definition).

There is a problem though — ret inside Func/EndFunc definition probably will break program's execution path at the place of invocation.

As a side note, why do you need something like MASM syntax MACRO/ENDM? Can you explain your idea?
Post 28 Jan 2013, 13:11
View user's profile Send private message Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka 28 Jan 2013, 14:01
I often create large projects with many files. It'll simpler to me when I've macro like a Func/EndFunc.

As for program's break - I tested and it worked correctly for me.
Take a look:

main.asm
Code:
macro Func name*,[params]
{
  macro name
  {
  name:
}

EndFunc fix }

include 'win32ax.inc'
include 'OneOfMyProjectFiles.inc'
.code

  start:

        invoke  MessageBox,HWND_DESKTOP,"Hi! I'm the examplae program!",invoke GetCommandLine,MB_OK
        stdcall MyFunc
        invoke  ExitProcess,0

        MyFunc
.end start    
OneOfMyProjectFiles.inc
Code:
Func MyFunc
  invoke  MessageBox,HWND_DESKTOP,"My Func!",invoke GetCommandLine,MB_OK
  ret
EndFunc    

Try this and post me what's going to happen.
Post 28 Jan 2013, 14:01
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 28 Jan 2013, 21:12
Kazyaka,

So your includes define macros which expand to function definition, then in main source you invoke those macros in appropriate places, right?

I've contrived this:
Code:
        include "Win32AX.Inc"

macro .proc [args*] {
common
  macro .code [.args] \{
  \common
    .code .args
    proc args
}

.endp fix endp }

macro MB text*, caption* { invoke MessageBox, HWND_DESKTOP, text, caption, MB_OK }

        .proc   Hello
        MB "Hello", invoke GetCommandLine
        ret
        .endp

        .proc   Goodbye
        MB "Goodbye", invoke GetCommandLine
        ret
        .endp

        .code
start:  MB "Starting", ".proc"
        stdcall Hello
        stdcall Goodbye
        ret

        .end    start    
It uses macro stack to store each function's definition, then .code invocation unrolls entire list after original .code expansion. Not thoroughly tested though, I just wrote that.
Post 28 Jan 2013, 21:12
View user's profile Send private message Reply with quote
Spool



Joined: 08 Jan 2013
Posts: 151
Spool 29 Jan 2013, 06:58
[ Post removed by author. ]


Last edited by Spool on 17 Mar 2013, 04:51; edited 1 time in total
Post 29 Jan 2013, 06:58
View user's profile Send private message Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka 29 Jan 2013, 09:44
baldr,
Quote:
So your includes define macros which expand to function definition, then in main source you invoke those macros in appropriate places, right?
My English isn't good, but I see that you know what I mean. That's right.
Quote:
Not thoroughly tested though, I just wrote that.
I just tried it and everything is OK. But I don't understand one thing. Your macro uses proc and endp. This makes sense? My Func and EndFunc are much simpler and faster.
Don't you think so?

Spool,
Yes, we can, but I'll stay with my macros. By the way this thread is a collection of useful functions.
Post 29 Jan 2013, 09:44
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 29 Jan 2013, 17:08
Kazyaka,

.proc/.endp delegate all stuff to proc/endp just in case you may want to use features of proc/ret, such as calling convention, registers' save/restore and typed parameters.

You may send PM in russian if you want to.
Post 29 Jan 2013, 17:08
View user's profile Send private message Reply with quote
Kazyaka



Joined: 10 Oct 2011
Posts: 62
Location: Earth
Kazyaka 29 Jan 2013, 21:48
baldr,

Thanks for help. You've explained me everything what I should know about this.

I think thread is solved and can be close. I'm going to put all solutions in the first post.
Post 29 Jan 2013, 21:48
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.